Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e9397c

Browse files
committed
Use stdlib mimetypes instead of hardcoding them.
The stdlib provides all necessary mimetypes (see _default_mime_types), except for application/emf -- but we don't have an emf backend anymore. This also makes the dict (relatively) easily extensible by the end user, who could call `mimetypes.init(...)` themselves to add entries to the mapping.
1 parent 48bc2be commit 2e9397c

File tree

2 files changed

+10
-34
lines changed

2 files changed

+10
-34
lines changed

examples/user_interfaces/embedding_webagg_sgskip.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
Embedding WebAgg
44
================
55
6-
This example demonstrates how to embed matplotlib WebAgg interactive
7-
plotting in your own web application and framework. It is not
8-
necessary to do all this if you merely want to display a plot in a
9-
browser or use matplotlib's built-in Tornado-based server "on the
10-
side".
6+
This example demonstrates how to embed Matplotlib WebAgg interactive plotting
7+
in your own web application and framework. It is not necessary to do all this
8+
if you merely want to display a plot in a browser or use Matplotlib's built-in
9+
Tornado-based server "on the side".
1110
1211
The framework being used must support web sockets.
1312
"""
1413

1514
import io
15+
import mimetypes
1616

1717
try:
1818
import tornado
@@ -138,20 +138,8 @@ class Download(tornado.web.RequestHandler):
138138

139139
def get(self, fmt):
140140
manager = self.application.manager
141-
142-
mimetypes = {
143-
'ps': 'application/postscript',
144-
'eps': 'application/postscript',
145-
'pdf': 'application/pdf',
146-
'svg': 'image/svg+xml',
147-
'png': 'image/png',
148-
'jpeg': 'image/jpeg',
149-
'tif': 'image/tiff',
150-
'emf': 'application/emf'
151-
}
152-
153-
self.set_header('Content-Type', mimetypes.get(fmt, 'binary'))
154-
141+
self.set_header(
142+
'Content-Type', mimetypes.types_map.get(fmt, 'binary'))
155143
buff = io.BytesIO()
156144
manager.canvas.figure.savefig(buff, format=fmt)
157145
self.write(buff.getvalue())

lib/matplotlib/backends/backend_webagg.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import errno
1616
from io import BytesIO
1717
import json
18+
import mimetypes
1819
from pathlib import Path
1920
import random
2021
import sys
@@ -113,21 +114,8 @@ class Download(tornado.web.RequestHandler):
113114
def get(self, fignum, fmt):
114115
fignum = int(fignum)
115116
manager = Gcf.get_fig_manager(fignum)
116-
117-
# TODO: Move this to a central location
118-
mimetypes = {
119-
'ps': 'application/postscript',
120-
'eps': 'application/postscript',
121-
'pdf': 'application/pdf',
122-
'svg': 'image/svg+xml',
123-
'png': 'image/png',
124-
'jpeg': 'image/jpeg',
125-
'tif': 'image/tiff',
126-
'emf': 'application/emf'
127-
}
128-
129-
self.set_header('Content-Type', mimetypes.get(fmt, 'binary'))
130-
117+
self.set_header(
118+
'Content-Type', mimetypes.types_map.get(fmt, 'binary'))
131119
buff = BytesIO()
132120
manager.canvas.figure.savefig(buff, format=fmt)
133121
self.write(buff.getvalue())

0 commit comments

Comments
 (0)