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