Show More
@@ -1,85 +1,85 b'' | |||||
1 | import os |
|
1 | import os | |
2 |
|
2 | |||
3 | from tornado import web |
|
3 | from tornado import web | |
4 |
|
4 | |||
5 | from ..base.handlers import IPythonHandler |
|
5 | from ..base.handlers import IPythonHandler | |
6 | from IPython.nbformat.current import to_notebook_json |
|
6 | from IPython.nbformat.current import to_notebook_json | |
7 | from IPython.nbconvert.exporters.export import exporter_map |
|
7 | from IPython.nbconvert.exporters.export import exporter_map | |
8 | from IPython.utils import tz |
|
8 | from IPython.utils import tz | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | def has_resource_files(resources): |
|
11 | def has_resource_files(resources): | |
12 | output_files_dir = resources.get('output_files_dir', "") |
|
12 | output_files_dir = resources.get('output_files_dir', "") | |
13 | return bool(os.path.isdir(output_files_dir) and \ |
|
13 | return bool(os.path.isdir(output_files_dir) and \ | |
14 | os.listdir(output_files_dir)) |
|
14 | os.listdir(output_files_dir)) | |
15 |
|
15 | |||
16 | class NbconvertFileHandler(IPythonHandler): |
|
16 | class NbconvertFileHandler(IPythonHandler): | |
17 |
|
17 | |||
18 | SUPPORTED_METHODS = ('GET',) |
|
18 | SUPPORTED_METHODS = ('GET',) | |
19 |
|
19 | |||
20 | @web.authenticated |
|
20 | @web.authenticated | |
21 | def get(self, format, path='', name=None): |
|
21 | def get(self, format, path='', name=None): | |
22 | exporter = exporter_map[format]() |
|
22 | exporter = exporter_map[format](config=self.config) | |
23 |
|
23 | |||
24 | path = path.strip('/') |
|
24 | path = path.strip('/') | |
25 | os_path = self.notebook_manager.get_os_path(name, path) |
|
25 | os_path = self.notebook_manager.get_os_path(name, path) | |
26 | if not os.path.isfile(os_path): |
|
26 | if not os.path.isfile(os_path): | |
27 | raise web.HTTPError(404, u'Notebook does not exist: %s' % name) |
|
27 | raise web.HTTPError(404, u'Notebook does not exist: %s' % name) | |
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 |
|
31 | |||
32 | # Force download if requested |
|
32 | # Force download if requested | |
33 | if self.get_argument('download', 'false').lower() == 'true': |
|
33 | if self.get_argument('download', 'false').lower() == 'true': | |
34 | filename = os.path.splitext(name)[0] + '.' + exporter.file_extension |
|
34 | filename = os.path.splitext(name)[0] + '.' + exporter.file_extension | |
35 | self.set_header('Content-Disposition', |
|
35 | self.set_header('Content-Disposition', | |
36 | 'attachment; filename="%s"' % filename) |
|
36 | 'attachment; filename="%s"' % filename) | |
37 |
|
37 | |||
38 | # MIME type |
|
38 | # MIME type | |
39 | if exporter.output_mimetype: |
|
39 | if exporter.output_mimetype: | |
40 | self.set_header('Content-Type', |
|
40 | self.set_header('Content-Type', | |
41 | '%s; charset=utf-8' % exporter.output_mimetype) |
|
41 | '%s; charset=utf-8' % exporter.output_mimetype) | |
42 |
|
42 | |||
43 | output, resources = exporter.from_filename(os_path) |
|
43 | output, resources = exporter.from_filename(os_path) | |
44 |
|
44 | |||
45 | # TODO: If there are resources, combine them into a zip file |
|
45 | # TODO: If there are resources, combine them into a zip file | |
46 | assert not has_resource_files(resources) |
|
46 | assert not has_resource_files(resources) | |
47 |
|
47 | |||
48 | self.finish(output) |
|
48 | self.finish(output) | |
49 |
|
49 | |||
50 | class NbconvertPostHandler(IPythonHandler): |
|
50 | class NbconvertPostHandler(IPythonHandler): | |
51 | SUPPORTED_METHODS = ('POST',) |
|
51 | SUPPORTED_METHODS = ('POST',) | |
52 |
|
52 | |||
53 | @web.authenticated |
|
53 | @web.authenticated | |
54 | def post(self, format): |
|
54 | def post(self, format): | |
55 | exporter = exporter_map[format]() |
|
55 | exporter = exporter_map[format](config=self.config) | |
56 |
|
56 | |||
57 | model = self.get_json_body() |
|
57 | model = self.get_json_body() | |
58 | nbnode = to_notebook_json(model['content']) |
|
58 | nbnode = to_notebook_json(model['content']) | |
59 |
|
59 | |||
60 | # MIME type |
|
60 | # MIME type | |
61 | if exporter.output_mimetype: |
|
61 | if exporter.output_mimetype: | |
62 | self.set_header('Content-Type', |
|
62 | self.set_header('Content-Type', | |
63 | '%s; charset=utf-8' % exporter.output_mimetype) |
|
63 | '%s; charset=utf-8' % exporter.output_mimetype) | |
64 |
|
64 | |||
65 | output, resources = exporter.from_notebook_node(nbnode) |
|
65 | output, resources = exporter.from_notebook_node(nbnode) | |
66 |
|
66 | |||
67 | # TODO: If there are resources, combine them into a zip file |
|
67 | # TODO: If there are resources, combine them into a zip file | |
68 | assert not has_resource_files(resources) |
|
68 | assert not has_resource_files(resources) | |
69 |
|
69 | |||
70 | self.finish(output) |
|
70 | self.finish(output) | |
71 |
|
71 | |||
72 | #----------------------------------------------------------------------------- |
|
72 | #----------------------------------------------------------------------------- | |
73 | # URL to handler mappings |
|
73 | # URL to handler mappings | |
74 | #----------------------------------------------------------------------------- |
|
74 | #----------------------------------------------------------------------------- | |
75 |
|
75 | |||
76 | _format_regex = r"(?P<format>\w+)" |
|
76 | _format_regex = r"(?P<format>\w+)" | |
77 | _path_regex = r"(?P<path>(?:/.*)*)" |
|
77 | _path_regex = r"(?P<path>(?:/.*)*)" | |
78 | _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)" |
|
78 | _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)" | |
79 | _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) |
|
79 | _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) | |
80 |
|
80 | |||
81 | default_handlers = [ |
|
81 | default_handlers = [ | |
82 | (r"/nbconvert/%s%s" % (_format_regex, _notebook_path_regex), |
|
82 | (r"/nbconvert/%s%s" % (_format_regex, _notebook_path_regex), | |
83 | NbconvertFileHandler), |
|
83 | NbconvertFileHandler), | |
84 | (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler), |
|
84 | (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler), | |
85 | ] No newline at end of file |
|
85 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now