Add MIME types to nbconvert exporters

pull/37/head
Thomas Kluyver 12 years ago
parent a3c2f1b5f9
commit 17c6af1fcf

@ -28,11 +28,17 @@ class NbconvertFileHandler(IPythonHandler):
info = os.stat(os_path)
self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
# Force download if requested
if self.get_argument('download', 'false').lower() == 'true':
filename = os.path.splitext(name)[0] + '.' + exporter.file_extension
self.set_header('Content-Disposition',
'attachment; filename="%s"' % filename)
# MIME type
if exporter.mime_type:
self.set_header('Content-Type', '%s; charset=utf-8' % exporter.mime_type)
output, resources = exporter.from_filename(os_path)
# TODO: If there are resources, combine them into a zip file
@ -49,8 +55,13 @@ class NbconvertPostHandler(IPythonHandler):
model = self.get_json_body()
nbnode = to_notebook_json(model['content'])
output, resources = exporter.from_notebook_node(nbnode)
# MIME type
if exporter.mime_type:
self.set_header('Content-Type', '%s; charset=utf-8' % exporter.mime_type)
output, resources = exporter.from_notebook_node(nbnode)
# TODO: If there are resources, combine them into a zip file
assert not has_resource_files(resources)

@ -61,10 +61,12 @@ class APITest(NotebookTestBase):
def test_from_file(self):
r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb')
self.assertEqual(r.status_code, 200)
self.assertIn(u'text/html', r.headers['Content-Type'])
self.assertIn(u'Created by test', r.text)
self.assertIn(u'print', r.text)
r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb')
self.assertIn(u'text/x-python', r.headers['Content-Type'])
self.assertIn(u'print(2*6)', r.text)
def test_from_file_404(self):
@ -74,8 +76,8 @@ class APITest(NotebookTestBase):
def test_from_file_download(self):
r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
content_disposition = r.headers['Content-Disposition']
assert 'attachment' in content_disposition
assert 'testnb.py' in content_disposition
self.assertIn('attachment', content_disposition)
self.assertIn('testnb.py', content_disposition)
def test_from_post(self):
nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
@ -83,8 +85,10 @@ class APITest(NotebookTestBase):
r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel)
self.assertEqual(r.status_code, 200)
self.assertIn(u'text/html', r.headers['Content-Type'])
self.assertIn(u'Created by test', r.text)
self.assertIn(u'print', r.text)
r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel)
self.assertIn(u'text/x-python', r.headers['Content-Type'])
self.assertIn(u'print(2*6)', r.text)

@ -32,3 +32,6 @@ class PythonExporter(TemplateExporter):
def _raw_mimetype_default(self):
return 'application/x-python'
mime_type = Unicode('text/x-python', config=True,
help="MIME type of the result file, for HTTP response headers."
)

Loading…
Cancel
Save