##// END OF EJS Templates
Add MIME types to nbconvert exporters
Thomas Kluyver -
Show More
@@ -28,11 +28,17 b' class NbconvertFileHandler(IPythonHandler):'
28 28
29 29 info = os.stat(os_path)
30 30 self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
31
32 # Force download if requested
31 33 if self.get_argument('download', 'false').lower() == 'true':
32 34 filename = os.path.splitext(name)[0] + '.' + exporter.file_extension
33 35 self.set_header('Content-Disposition',
34 36 'attachment; filename="%s"' % filename)
35 37
38 # MIME type
39 if exporter.mime_type:
40 self.set_header('Content-Type', '%s; charset=utf-8' % exporter.mime_type)
41
36 42 output, resources = exporter.from_filename(os_path)
37 43
38 44 # TODO: If there are resources, combine them into a zip file
@@ -49,8 +55,13 b' class NbconvertPostHandler(IPythonHandler):'
49 55
50 56 model = self.get_json_body()
51 57 nbnode = to_notebook_json(model['content'])
52 output, resources = exporter.from_notebook_node(nbnode)
53 58
59 # MIME type
60 if exporter.mime_type:
61 self.set_header('Content-Type', '%s; charset=utf-8' % exporter.mime_type)
62
63 output, resources = exporter.from_notebook_node(nbnode)
64
54 65 # TODO: If there are resources, combine them into a zip file
55 66 assert not has_resource_files(resources)
56 67
@@ -61,10 +61,12 b' class APITest(NotebookTestBase):'
61 61 def test_from_file(self):
62 62 r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb')
63 63 self.assertEqual(r.status_code, 200)
64 self.assertIn(u'text/html', r.headers['Content-Type'])
64 65 self.assertIn(u'Created by test', r.text)
65 66 self.assertIn(u'print', r.text)
66
67
67 68 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb')
69 self.assertIn(u'text/x-python', r.headers['Content-Type'])
68 70 self.assertIn(u'print(2*6)', r.text)
69 71
70 72 def test_from_file_404(self):
@@ -74,8 +76,8 b' class APITest(NotebookTestBase):'
74 76 def test_from_file_download(self):
75 77 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
76 78 content_disposition = r.headers['Content-Disposition']
77 assert 'attachment' in content_disposition
78 assert 'testnb.py' in content_disposition
79 self.assertIn('attachment', content_disposition)
80 self.assertIn('testnb.py', content_disposition)
79 81
80 82 def test_from_post(self):
81 83 nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
@@ -83,8 +85,10 b' class APITest(NotebookTestBase):'
83 85
84 86 r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel)
85 87 self.assertEqual(r.status_code, 200)
88 self.assertIn(u'text/html', r.headers['Content-Type'])
86 89 self.assertIn(u'Created by test', r.text)
87 90 self.assertIn(u'print', r.text)
88 91
89 92 r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel)
93 self.assertIn(u'text/x-python', r.headers['Content-Type'])
90 94 self.assertIn(u'print(2*6)', r.text) No newline at end of file
@@ -53,6 +53,10 b' class Exporter(LoggingConfigurable):'
53 53 help="Extension of the file that should be written to disk"
54 54 )
55 55
56 mime_type = Unicode('', config=True,
57 help="MIME type of the result file, for HTTP response headers."
58 )
59
56 60 #Configurability, allows the user to easily add filters and preprocessors.
57 61 preprocessors = List(config=True,
58 62 help="""List of preprocessors, by name or namespace, to enable.""")
@@ -36,6 +36,10 b' class HTMLExporter(TemplateExporter):'
36 36 help="Extension of the file that should be written to disk"
37 37 )
38 38
39 mime_type = Unicode('text/html', config=True,
40 help="MIME type of the result file, for HTTP response headers."
41 )
42
39 43 default_template = Unicode('full', config=True, help="""Flavor of the data
40 44 format to use. I.E. 'full' or 'basic'""")
41 45
@@ -40,6 +40,10 b' class LatexExporter(TemplateExporter):'
40 40 'tex', config=True,
41 41 help="Extension of the file that should be written to disk")
42 42
43 mime_type = Unicode('application/x-tex', config=True,
44 help="MIME type of the result file, for HTTP response headers."
45 )
46
43 47 default_template = Unicode('article', config=True, help="""Template of the
44 48 data format to use. I.E. 'article' or 'report'""")
45 49
@@ -36,6 +36,10 b' class MarkdownExporter(TemplateExporter):'
36 36 def _raw_mimetypes_default(self):
37 37 return ['text/markdown', 'text/html']
38 38
39 mime_type = Unicode('text/x-markdown', config=True,
40 help="MIME type of the result file, for HTTP response headers."
41 )
42
39 43 @property
40 44 def default_config(self):
41 45 c = Config({'ExtractOutputPreprocessor':{'enabled':True}})
@@ -32,3 +32,6 b' class PythonExporter(TemplateExporter):'
32 32 def _raw_mimetype_default(self):
33 33 return 'application/x-python'
34 34
35 mime_type = Unicode('text/x-python', config=True,
36 help="MIME type of the result file, for HTTP response headers."
37 )
@@ -32,7 +32,11 b' class RSTExporter(TemplateExporter):'
32 32
33 33 def _raw_mimetype_default(self):
34 34 return 'text/restructuredtext'
35
35
36 mime_type = Unicode('text/x-rst', config=True,
37 help="MIME type of the result file, for HTTP response headers."
38 )
39
36 40 @property
37 41 def default_config(self):
38 42 c = Config({'ExtractOutputPreprocessor':{'enabled':True}})
@@ -31,6 +31,10 b' class SlidesExporter(HTMLExporter):'
31 31 help="Extension of the file that should be written to disk"
32 32 )
33 33
34 mime_type = Unicode('text/html', config=True,
35 help="MIME type of the result file, for HTTP response headers."
36 )
37
34 38 default_template = Unicode('reveal', config=True, help="""Template of the
35 39 data format to use. I.E. 'reveal'""")
36 40
General Comments 0
You need to be logged in to leave comments. Login now