Skip to content

Use stdlib mimetypes instead of hardcoding them. #14218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions examples/user_interfaces/embedding_webagg_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Embedding WebAgg
================

This example demonstrates how to embed matplotlib WebAgg interactive
plotting in your own web application and framework. It is not
necessary to do all this if you merely want to display a plot in a
browser or use matplotlib's built-in Tornado-based server "on the
side".
This example demonstrates how to embed Matplotlib WebAgg interactive plotting
in your own web application and framework. It is not necessary to do all this
if you merely want to display a plot in a browser or use Matplotlib's built-in
Tornado-based server "on the side".

The framework being used must support web sockets.
"""

import io
import mimetypes

try:
import tornado
Expand Down Expand Up @@ -138,20 +138,8 @@ class Download(tornado.web.RequestHandler):

def get(self, fmt):
manager = self.application.manager

mimetypes = {
'ps': 'application/postscript',
'eps': 'application/postscript',
'pdf': 'application/pdf',
'svg': 'image/svg+xml',
'png': 'image/png',
'jpeg': 'image/jpeg',
'tif': 'image/tiff',
'emf': 'application/emf'
}

self.set_header('Content-Type', mimetypes.get(fmt, 'binary'))

self.set_header(
'Content-Type', mimetypes.types_map.get(fmt, 'binary'))
buff = io.BytesIO()
manager.canvas.figure.savefig(buff, format=fmt)
self.write(buff.getvalue())
Expand Down
18 changes: 3 additions & 15 deletions lib/matplotlib/backends/backend_webagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import errno
from io import BytesIO
import json
import mimetypes
from pathlib import Path
import random
import sys
Expand Down Expand Up @@ -113,21 +114,8 @@ class Download(tornado.web.RequestHandler):
def get(self, fignum, fmt):
fignum = int(fignum)
manager = Gcf.get_fig_manager(fignum)

# TODO: Move this to a central location
mimetypes = {
'ps': 'application/postscript',
'eps': 'application/postscript',
'pdf': 'application/pdf',
'svg': 'image/svg+xml',
'png': 'image/png',
'jpeg': 'image/jpeg',
'tif': 'image/tiff',
'emf': 'application/emf'
}

self.set_header('Content-Type', mimetypes.get(fmt, 'binary'))

self.set_header(
'Content-Type', mimetypes.types_map.get(fmt, 'binary'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback should be application/octet-stream, should it not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR leaves the old behavior, but I'd say that octet-stream is more correct, indeed.

buff = BytesIO()
manager.canvas.figure.savefig(buff, format=fmt)
self.write(buff.getvalue())
Expand Down