##// END OF EJS Templates
Describe nbconvert in notebook UI for whatsnew
Describe nbconvert in notebook UI for whatsnew

File last commit:

r13835:af0b973f
r13836:1663664e
Show More
handlers.py
99 lines | 3.2 KiB | text/x-python | PythonLexer
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 import json
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 import os
from tornado import web
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 from ..base.handlers import IPythonHandler, json_errors
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 from IPython.nbformat.current import to_notebook_json
from IPython.nbconvert.exporters.export import exporter_map
from IPython.utils import tz
def has_resource_files(resources):
output_files_dir = resources.get('output_files_dir', "")
return bool(os.path.isdir(output_files_dir) and \
os.listdir(output_files_dir))
class NbconvertFileHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET',)
@web.authenticated
def get(self, format, path='', name=None):
exporter = exporter_map[format]()
path = path.strip('/')
os_path = self.notebook_manager.get_os_path(name, path)
if not os.path.isfile(os_path):
raise web.HTTPError(404, u'Notebook does not exist: %s' % name)
info = os.stat(os_path)
self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830
# Force download if requested
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829 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)
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 # MIME type
Thomas Kluyver
Condense raw_mimetype and mime_type traitlets into output_mimetype
r13832 if exporter.output_mimetype:
self.set_header('Content-Type',
'%s; charset=utf-8' % exporter.output_mimetype)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 output, resources = exporter.from_filename(os_path)
# TODO: If there are resources, combine them into a zip file
assert not has_resource_files(resources)
self.finish(output)
class NbconvertPostHandler(IPythonHandler):
SUPPORTED_METHODS = ('POST',)
@web.authenticated
def post(self, format):
exporter = exporter_map[format]()
model = self.get_json_body()
nbnode = to_notebook_json(model['content'])
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 # MIME type
Thomas Kluyver
Condense raw_mimetype and mime_type traitlets into output_mimetype
r13832 if exporter.output_mimetype:
self.set_header('Content-Type',
'%s; charset=utf-8' % exporter.output_mimetype)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830
output, resources = exporter.from_notebook_node(nbnode)
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 # TODO: If there are resources, combine them into a zip file
assert not has_resource_files(resources)
self.finish(output)
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 class NbconvertRootHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET',)
@web.authenticated
@json_errors
def get(self):
res = {}
for format, exporter in exporter_map.items():
res[format] = info = {}
info['output_mimetype'] = exporter.output_mimetype
self.finish(json.dumps(res))
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 #-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_format_regex = r"(?P<format>\w+)"
_path_regex = r"(?P<path>(?:/.*)*)"
_notebook_name_regex = r"(?P<name>[^/]+\.ipynb)"
_notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex)
default_handlers = [
(r"/nbconvert/%s%s" % (_format_regex, _notebook_path_regex),
NbconvertFileHandler),
(r"/nbconvert/%s" % _format_regex, NbconvertPostHandler),
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 (r"/nbconvert", NbconvertRootHandler),
Thomas Kluyver
Add HTTP handlers for nbconvert
r13827 ]