2014-06-03 04:47:11 +08:00
|
|
|
"""Tornado handlers for nbconvert."""
|
|
|
|
|
|
|
|
# Copyright (c) IPython Development Team.
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
|
|
|
|
2013-12-20 08:07:27 +08:00
|
|
|
import io
|
2013-12-06 09:38:30 +08:00
|
|
|
import os
|
2013-12-20 08:07:27 +08:00
|
|
|
import zipfile
|
2013-12-06 09:38:30 +08:00
|
|
|
|
|
|
|
from tornado import web
|
|
|
|
|
2014-07-16 02:02:26 +08:00
|
|
|
from ..base.handlers import (
|
|
|
|
IPythonHandler, FilesRedirectHandler,
|
2014-12-20 04:42:28 +08:00
|
|
|
path_regex,
|
2014-07-16 02:02:26 +08:00
|
|
|
)
|
2014-10-24 06:35:01 +08:00
|
|
|
from IPython.nbformat import from_dict
|
2013-12-24 04:35:51 +08:00
|
|
|
|
2013-12-20 08:07:27 +08:00
|
|
|
from IPython.utils.py3compat import cast_bytes
|
2015-01-14 11:38:14 +08:00
|
|
|
from IPython.utils import text
|
2013-12-20 08:07:27 +08:00
|
|
|
|
|
|
|
def find_resource_files(output_files_dir):
|
|
|
|
files = []
|
|
|
|
for dirpath, dirnames, filenames in os.walk(output_files_dir):
|
|
|
|
files.extend([os.path.join(dirpath, f) for f in filenames])
|
|
|
|
return files
|
|
|
|
|
|
|
|
def respond_zip(handler, name, output, resources):
|
|
|
|
"""Zip up the output and resource files and respond with the zip file.
|
|
|
|
|
|
|
|
Returns True if it has served a zip file, False if there are no resource
|
|
|
|
files, in which case we serve the plain output file.
|
|
|
|
"""
|
|
|
|
# Check if we have resource files we need to zip
|
|
|
|
output_files = resources.get('outputs', None)
|
|
|
|
if not output_files:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Headers
|
|
|
|
zip_filename = os.path.splitext(name)[0] + '.zip'
|
|
|
|
handler.set_header('Content-Disposition',
|
|
|
|
'attachment; filename="%s"' % zip_filename)
|
|
|
|
handler.set_header('Content-Type', 'application/zip')
|
|
|
|
|
|
|
|
# Prepare the zip file
|
|
|
|
buffer = io.BytesIO()
|
|
|
|
zipf = zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED)
|
2014-11-21 09:34:39 +08:00
|
|
|
output_filename = os.path.splitext(name)[0] + resources['output_extension']
|
2013-12-20 08:07:27 +08:00
|
|
|
zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
|
|
|
|
for filename, data in output_files.items():
|
|
|
|
zipf.writestr(os.path.basename(filename), data)
|
|
|
|
zipf.close()
|
|
|
|
|
|
|
|
handler.finish(buffer.getvalue())
|
|
|
|
return True
|
2013-12-06 09:38:30 +08:00
|
|
|
|
2013-12-24 04:35:51 +08:00
|
|
|
def get_exporter(format, **kwargs):
|
|
|
|
"""get an exporter, raising appropriate errors"""
|
|
|
|
# if this fails, will raise 500
|
|
|
|
try:
|
|
|
|
from IPython.nbconvert.exporters.export import exporter_map
|
|
|
|
except ImportError as e:
|
|
|
|
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
|
|
|
|
|
|
|
|
try:
|
|
|
|
Exporter = exporter_map[format]
|
|
|
|
except KeyError:
|
|
|
|
# should this be 400?
|
|
|
|
raise web.HTTPError(404, u"No exporter for format: %s" % format)
|
|
|
|
|
2014-01-08 07:15:57 +08:00
|
|
|
try:
|
|
|
|
return Exporter(**kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
raise web.HTTPError(500, "Could not construct Exporter: %s" % e)
|
2013-12-24 04:35:51 +08:00
|
|
|
|
2013-12-06 09:38:30 +08:00
|
|
|
class NbconvertFileHandler(IPythonHandler):
|
|
|
|
|
|
|
|
SUPPORTED_METHODS = ('GET',)
|
|
|
|
|
|
|
|
@web.authenticated
|
2014-10-23 04:13:36 +08:00
|
|
|
def get(self, format, path):
|
2013-12-24 04:35:51 +08:00
|
|
|
|
2014-04-11 12:13:41 +08:00
|
|
|
exporter = get_exporter(format, config=self.config, log=self.log)
|
2013-12-06 09:38:30 +08:00
|
|
|
|
|
|
|
path = path.strip('/')
|
2014-11-12 06:51:21 +08:00
|
|
|
model = self.contents_manager.get(path=path)
|
2014-10-23 04:13:36 +08:00
|
|
|
name = model['name']
|
2014-12-20 04:42:28 +08:00
|
|
|
if model['type'] != 'notebook':
|
|
|
|
raise web.HTTPError(400, "Not a notebook: %s" % path)
|
2013-12-06 09:38:30 +08:00
|
|
|
|
2014-02-13 15:09:39 +08:00
|
|
|
self.set_header('Last-Modified', model['last_modified'])
|
2015-01-14 11:38:14 +08:00
|
|
|
|
2013-12-23 09:46:53 +08:00
|
|
|
try:
|
2015-01-14 11:38:14 +08:00
|
|
|
output, resources = exporter.from_notebook_node(
|
|
|
|
model['content'],
|
|
|
|
resources={
|
|
|
|
"metadata": {
|
|
|
|
"name": name[:name.rfind('.')],
|
|
|
|
"modified_date": (model['last_modified']
|
|
|
|
.strftime(text.date_format))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2013-12-23 09:46:53 +08:00
|
|
|
except Exception as e:
|
|
|
|
raise web.HTTPError(500, "nbconvert failed: %s" % e)
|
2013-12-20 08:07:27 +08:00
|
|
|
|
|
|
|
if respond_zip(self, name, output, resources):
|
|
|
|
return
|
|
|
|
|
2013-12-07 07:42:53 +08:00
|
|
|
# Force download if requested
|
2013-12-07 07:26:59 +08:00
|
|
|
if self.get_argument('download', 'false').lower() == 'true':
|
2014-11-21 09:34:39 +08:00
|
|
|
filename = os.path.splitext(name)[0] + resources['output_extension']
|
2013-12-07 07:26:59 +08:00
|
|
|
self.set_header('Content-Disposition',
|
2013-12-20 08:07:27 +08:00
|
|
|
'attachment; filename="%s"' % filename)
|
|
|
|
|
2013-12-07 07:42:53 +08:00
|
|
|
# MIME type
|
2013-12-10 04:17:56 +08:00
|
|
|
if exporter.output_mimetype:
|
|
|
|
self.set_header('Content-Type',
|
|
|
|
'%s; charset=utf-8' % exporter.output_mimetype)
|
2013-12-07 07:42:53 +08:00
|
|
|
|
2013-12-06 09:38:30 +08:00
|
|
|
self.finish(output)
|
|
|
|
|
|
|
|
class NbconvertPostHandler(IPythonHandler):
|
|
|
|
SUPPORTED_METHODS = ('POST',)
|
|
|
|
|
2014-10-23 04:13:36 +08:00
|
|
|
@web.authenticated
|
2013-12-06 09:38:30 +08:00
|
|
|
def post(self, format):
|
2013-12-24 04:35:51 +08:00
|
|
|
exporter = get_exporter(format, config=self.config)
|
2013-12-06 09:38:30 +08:00
|
|
|
|
|
|
|
model = self.get_json_body()
|
2014-07-28 11:15:30 +08:00
|
|
|
name = model.get('name', 'notebook.ipynb')
|
2014-10-10 07:25:52 +08:00
|
|
|
nbnode = from_dict(model['content'])
|
2013-12-23 09:46:53 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
output, resources = exporter.from_notebook_node(nbnode)
|
|
|
|
except Exception as e:
|
|
|
|
raise web.HTTPError(500, "nbconvert failed: %s" % e)
|
2013-12-20 08:07:27 +08:00
|
|
|
|
2014-07-28 11:15:30 +08:00
|
|
|
if respond_zip(self, name, output, resources):
|
2013-12-20 08:07:27 +08:00
|
|
|
return
|
|
|
|
|
2013-12-07 07:42:53 +08:00
|
|
|
# MIME type
|
2013-12-10 04:17:56 +08:00
|
|
|
if exporter.output_mimetype:
|
|
|
|
self.set_header('Content-Type',
|
2013-12-20 08:07:27 +08:00
|
|
|
'%s; charset=utf-8' % exporter.output_mimetype)
|
2013-12-07 07:42:53 +08:00
|
|
|
|
2013-12-06 09:38:30 +08:00
|
|
|
self.finish(output)
|
|
|
|
|
2014-07-16 02:02:26 +08:00
|
|
|
|
2013-12-06 09:38:30 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# URL to handler mappings
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
_format_regex = r"(?P<format>\w+)"
|
2013-12-20 05:01:00 +08:00
|
|
|
|
2013-12-06 09:38:30 +08:00
|
|
|
|
|
|
|
default_handlers = [
|
|
|
|
(r"/nbconvert/%s" % _format_regex, NbconvertPostHandler),
|
2014-12-20 04:42:28 +08:00
|
|
|
(r"/nbconvert/%s%s" % (_format_regex, path_regex),
|
|
|
|
NbconvertFileHandler),
|
2014-07-16 02:02:26 +08:00
|
|
|
(r"/nbconvert/html%s" % path_regex, FilesRedirectHandler),
|
2014-05-02 06:42:46 +08:00
|
|
|
]
|