##// END OF EJS Templates
Add HTTP handlers for nbconvert
Thomas Kluyver -
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,68 b''
1 import os
2
3 from tornado import web
4
5 from ..base.handlers import IPythonHandler
6 from IPython.nbformat.current import to_notebook_json
7 from IPython.nbconvert.exporters.export import exporter_map
8 from IPython.utils import tz
9
10
11 def has_resource_files(resources):
12 output_files_dir = resources.get('output_files_dir', "")
13 return bool(os.path.isdir(output_files_dir) and \
14 os.listdir(output_files_dir))
15
16 class NbconvertFileHandler(IPythonHandler):
17
18 SUPPORTED_METHODS = ('GET',)
19
20 @web.authenticated
21 def get(self, format, path='', name=None):
22 exporter = exporter_map[format]()
23
24 path = path.strip('/')
25 os_path = self.notebook_manager.get_os_path(name, path)
26 if not os.path.isfile(os_path):
27 raise web.HTTPError(404, u'Notebook does not exist: %s' % name)
28
29 info = os.stat(os_path)
30 self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
31
32 output, resources = exporter.from_filename(os_path)
33
34 # TODO: If there are resources, combine them into a zip file
35 assert not has_resource_files(resources)
36
37 self.finish(output)
38
39 class NbconvertPostHandler(IPythonHandler):
40 SUPPORTED_METHODS = ('POST',)
41
42 @web.authenticated
43 def post(self, format):
44 exporter = exporter_map[format]()
45
46 model = self.get_json_body()
47 nbnode = to_notebook_json(model['content'])
48 output, resources = exporter.from_notebook_node(nbnode)
49
50 # TODO: If there are resources, combine them into a zip file
51 assert not has_resource_files(resources)
52
53 self.finish(output)
54
55 #-----------------------------------------------------------------------------
56 # URL to handler mappings
57 #-----------------------------------------------------------------------------
58
59 _format_regex = r"(?P<format>\w+)"
60 _path_regex = r"(?P<path>(?:/.*)*)"
61 _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)"
62 _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex)
63
64 default_handlers = [
65 (r"/nbconvert/%s%s" % (_format_regex, _notebook_path_regex),
66 NbconvertFileHandler),
67 (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler),
68 ] No newline at end of file
@@ -192,6 +192,7 b' class NotebookWebApplication(web.Application):'
192 handlers.extend(load_handlers('auth.login'))
192 handlers.extend(load_handlers('auth.login'))
193 handlers.extend(load_handlers('auth.logout'))
193 handlers.extend(load_handlers('auth.logout'))
194 handlers.extend(load_handlers('notebook.handlers'))
194 handlers.extend(load_handlers('notebook.handlers'))
195 handlers.extend(load_handlers('nbconvert.handlers'))
195 handlers.extend(load_handlers('services.kernels.handlers'))
196 handlers.extend(load_handlers('services.kernels.handlers'))
196 handlers.extend(load_handlers('services.notebooks.handlers'))
197 handlers.extend(load_handlers('services.notebooks.handlers'))
197 handlers.extend(load_handlers('services.clusters.handlers'))
198 handlers.extend(load_handlers('services.clusters.handlers'))
@@ -178,7 +178,7 b' class FileNotebookManager(NotebookManager):'
178 return notebooks
178 return notebooks
179
179
180 def get_notebook_model(self, name, path='', content=True):
180 def get_notebook_model(self, name, path='', content=True):
181 """ Takes a path and name for a notebook and returns it's model
181 """ Takes a path and name for a notebook and returns its model
182
182
183 Parameters
183 Parameters
184 ----------
184 ----------
General Comments 0
You need to be logged in to leave comments. Login now