##// END OF EJS Templates
removing spurious import
Nicholas Bollweg -
Show More
@@ -1,162 +1,161 b''
1 1 """Tornado handlers for nbconvert."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import io
7 7 import os
8 8 import zipfile
9 import collections
10 9
11 10 from tornado import web
12 11
13 12 from ..base.handlers import (
14 13 IPythonHandler, FilesRedirectHandler,
15 14 path_regex,
16 15 )
17 16 from IPython.nbformat import from_dict
18 17
19 18 from IPython.utils.py3compat import cast_bytes
20 19 from IPython.utils import text
21 20
22 21 def find_resource_files(output_files_dir):
23 22 files = []
24 23 for dirpath, dirnames, filenames in os.walk(output_files_dir):
25 24 files.extend([os.path.join(dirpath, f) for f in filenames])
26 25 return files
27 26
28 27 def respond_zip(handler, name, output, resources):
29 28 """Zip up the output and resource files and respond with the zip file.
30 29
31 30 Returns True if it has served a zip file, False if there are no resource
32 31 files, in which case we serve the plain output file.
33 32 """
34 33 # Check if we have resource files we need to zip
35 34 output_files = resources.get('outputs', None)
36 35 if not output_files:
37 36 return False
38 37
39 38 # Headers
40 39 zip_filename = os.path.splitext(name)[0] + '.zip'
41 40 handler.set_header('Content-Disposition',
42 41 'attachment; filename="%s"' % zip_filename)
43 42 handler.set_header('Content-Type', 'application/zip')
44 43
45 44 # Prepare the zip file
46 45 buffer = io.BytesIO()
47 46 zipf = zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED)
48 47 output_filename = os.path.splitext(name)[0] + resources['output_extension']
49 48 zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
50 49 for filename, data in output_files.items():
51 50 zipf.writestr(os.path.basename(filename), data)
52 51 zipf.close()
53 52
54 53 handler.finish(buffer.getvalue())
55 54 return True
56 55
57 56 def get_exporter(format, **kwargs):
58 57 """get an exporter, raising appropriate errors"""
59 58 # if this fails, will raise 500
60 59 try:
61 60 from IPython.nbconvert.exporters.export import exporter_map
62 61 except ImportError as e:
63 62 raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
64 63
65 64 try:
66 65 Exporter = exporter_map[format]
67 66 except KeyError:
68 67 # should this be 400?
69 68 raise web.HTTPError(404, u"No exporter for format: %s" % format)
70 69
71 70 try:
72 71 return Exporter(**kwargs)
73 72 except Exception as e:
74 73 raise web.HTTPError(500, "Could not construct Exporter: %s" % e)
75 74
76 75 class NbconvertFileHandler(IPythonHandler):
77 76
78 77 SUPPORTED_METHODS = ('GET',)
79 78
80 79 @web.authenticated
81 80 def get(self, format, path):
82 81
83 82 exporter = get_exporter(format, config=self.config, log=self.log)
84 83
85 84 path = path.strip('/')
86 85 model = self.contents_manager.get(path=path)
87 86 name = model['name']
88 87 if model['type'] != 'notebook':
89 88 raise web.HTTPError(400, "Not a notebook: %s" % path)
90 89
91 90 self.set_header('Last-Modified', model['last_modified'])
92 91
93 92 try:
94 93 output, resources = exporter.from_notebook_node(
95 94 model['content'],
96 95 resources={
97 96 "metadata": {
98 97 "name": name[:name.rfind('.')],
99 98 "modified_date": (model['last_modified']
100 99 .strftime(text.date_format))
101 100 }
102 101 }
103 102 )
104 103 except Exception as e:
105 104 raise web.HTTPError(500, "nbconvert failed: %s" % e)
106 105
107 106 if respond_zip(self, name, output, resources):
108 107 return
109 108
110 109 # Force download if requested
111 110 if self.get_argument('download', 'false').lower() == 'true':
112 111 filename = os.path.splitext(name)[0] + resources['output_extension']
113 112 self.set_header('Content-Disposition',
114 113 'attachment; filename="%s"' % filename)
115 114
116 115 # MIME type
117 116 if exporter.output_mimetype:
118 117 self.set_header('Content-Type',
119 118 '%s; charset=utf-8' % exporter.output_mimetype)
120 119
121 120 self.finish(output)
122 121
123 122 class NbconvertPostHandler(IPythonHandler):
124 123 SUPPORTED_METHODS = ('POST',)
125 124
126 125 @web.authenticated
127 126 def post(self, format):
128 127 exporter = get_exporter(format, config=self.config)
129 128
130 129 model = self.get_json_body()
131 130 name = model.get('name', 'notebook.ipynb')
132 131 nbnode = from_dict(model['content'])
133 132
134 133 try:
135 134 output, resources = exporter.from_notebook_node(nbnode)
136 135 except Exception as e:
137 136 raise web.HTTPError(500, "nbconvert failed: %s" % e)
138 137
139 138 if respond_zip(self, name, output, resources):
140 139 return
141 140
142 141 # MIME type
143 142 if exporter.output_mimetype:
144 143 self.set_header('Content-Type',
145 144 '%s; charset=utf-8' % exporter.output_mimetype)
146 145
147 146 self.finish(output)
148 147
149 148
150 149 #-----------------------------------------------------------------------------
151 150 # URL to handler mappings
152 151 #-----------------------------------------------------------------------------
153 152
154 153 _format_regex = r"(?P<format>\w+)"
155 154
156 155
157 156 default_handlers = [
158 157 (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler),
159 158 (r"/nbconvert/%s%s" % (_format_regex, path_regex),
160 159 NbconvertFileHandler),
161 160 (r"/nbconvert/html%s" % path_regex, FilesRedirectHandler),
162 161 ]
General Comments 0
You need to be logged in to leave comments. Login now