Show More
@@ -53,7 +53,7 b' class NamedNotebookHandler(web.RequestHandler):' | |||||
53 | def get(self, notebook_id): |
|
53 | def get(self, notebook_id): | |
54 | nbm = self.application.notebook_manager |
|
54 | nbm = self.application.notebook_manager | |
55 | if not nbm.notebook_exists(notebook_id): |
|
55 | if not nbm.notebook_exists(notebook_id): | |
56 | raise web.HTTPError(404) |
|
56 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) | |
57 | self.render('notebook.html', notebook_id=notebook_id) |
|
57 | self.render('notebook.html', notebook_id=notebook_id) | |
58 |
|
58 | |||
59 |
|
59 | |||
@@ -311,7 +311,7 b' class RSTHandler(web.RequestHandler):' | |||||
311 |
|
311 | |||
312 | def post(self): |
|
312 | def post(self): | |
313 | if publish_string is None: |
|
313 | if publish_string is None: | |
314 | raise web.HTTPError(503) |
|
314 | raise web.HTTPError(503, u'docutils not available') | |
315 | body = self.request.body.strip() |
|
315 | body = self.request.body.strip() | |
316 | source = body |
|
316 | source = body | |
317 | # template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html') |
|
317 | # template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html') | |
@@ -326,7 +326,7 b' class RSTHandler(web.RequestHandler):' | |||||
326 | settings_overrides=defaults |
|
326 | settings_overrides=defaults | |
327 | ) |
|
327 | ) | |
328 | except: |
|
328 | except: | |
329 | raise web.HTTPError(400) |
|
329 | raise web.HTTPError(400, u'Invalid RST') | |
330 | print html |
|
330 | print html | |
331 | self.set_header('Content-Type', 'text/html') |
|
331 | self.set_header('Content-Type', 'text/html') | |
332 | self.finish(html) |
|
332 | self.finish(html) |
@@ -278,24 +278,20 b' class MappingKernelManager(KernelManager):' | |||||
278 |
|
278 | |||
279 | def kill_kernel(self, kernel_id): |
|
279 | def kill_kernel(self, kernel_id): | |
280 | """Kill a kernel and remove its notebook association.""" |
|
280 | """Kill a kernel and remove its notebook association.""" | |
281 | if kernel_id not in self: |
|
281 | self._check_kernel_id(kernel_id) | |
282 | raise web.HTTPError(404) |
|
|||
283 | super(MappingKernelManager, self).kill_kernel(kernel_id) |
|
282 | super(MappingKernelManager, self).kill_kernel(kernel_id) | |
284 | self.delete_mapping_for_kernel(kernel_id) |
|
283 | self.delete_mapping_for_kernel(kernel_id) | |
285 | self.log.info("Kernel killed: %s" % kernel_id) |
|
284 | self.log.info("Kernel killed: %s" % kernel_id) | |
286 |
|
285 | |||
287 | def interrupt_kernel(self, kernel_id): |
|
286 | def interrupt_kernel(self, kernel_id): | |
288 | """Interrupt a kernel.""" |
|
287 | """Interrupt a kernel.""" | |
289 | if kernel_id not in self: |
|
288 | self._check_kernel_id(kernel_id) | |
290 | raise web.HTTPError(404) |
|
|||
291 | super(MappingKernelManager, self).interrupt_kernel(kernel_id) |
|
289 | super(MappingKernelManager, self).interrupt_kernel(kernel_id) | |
292 | self.log.info("Kernel interrupted: %s" % kernel_id) |
|
290 | self.log.info("Kernel interrupted: %s" % kernel_id) | |
293 |
|
291 | |||
294 | def restart_kernel(self, kernel_id): |
|
292 | def restart_kernel(self, kernel_id): | |
295 | """Restart a kernel while keeping clients connected.""" |
|
293 | """Restart a kernel while keeping clients connected.""" | |
296 | if kernel_id not in self: |
|
294 | self._check_kernel_id(kernel_id) | |
297 | raise web.HTTPError(404) |
|
|||
298 |
|
||||
299 | # Get the notebook_id to preserve the kernel/notebook association. |
|
295 | # Get the notebook_id to preserve the kernel/notebook association. | |
300 | notebook_id = self.notebook_for_kernel(kernel_id) |
|
296 | notebook_id = self.notebook_for_kernel(kernel_id) | |
301 | # Create the new kernel first so we can move the clients over. |
|
297 | # Create the new kernel first so we can move the clients over. | |
@@ -309,17 +305,22 b' class MappingKernelManager(KernelManager):' | |||||
309 | return new_kernel_id |
|
305 | return new_kernel_id | |
310 |
|
306 | |||
311 | def create_iopub_stream(self, kernel_id): |
|
307 | def create_iopub_stream(self, kernel_id): | |
312 | if kernel_id not in self: |
|
308 | """Create a new iopub stream.""" | |
313 | raise web.HTTPError(404) |
|
309 | self._check_kernel_id(kernel_id) | |
314 | return super(MappingKernelManager, self).create_iopub_stream(kernel_id) |
|
310 | return super(MappingKernelManager, self).create_iopub_stream(kernel_id) | |
315 |
|
311 | |||
316 | def create_shell_stream(self, kernel_id): |
|
312 | def create_shell_stream(self, kernel_id): | |
317 | if kernel_id not in self: |
|
313 | """Create a new shell stream.""" | |
318 | raise web.HTTPError(404) |
|
314 | self._check_kernel_id(kernel_id) | |
319 | return super(MappingKernelManager, self).create_shell_stream(kernel_id) |
|
315 | return super(MappingKernelManager, self).create_shell_stream(kernel_id) | |
320 |
|
316 | |||
321 | def create_hb_stream(self, kernel_id): |
|
317 | def create_hb_stream(self, kernel_id): | |
322 | if kernel_id not in self: |
|
318 | """Create a new hb stream.""" | |
323 | raise web.HTTPError(404) |
|
319 | self._check_kernel_id(kernel_id) | |
324 | return super(MappingKernelManager, self).create_hb_stream(kernel_id) |
|
320 | return super(MappingKernelManager, self).create_hb_stream(kernel_id) | |
325 |
|
321 | |||
|
322 | def _check_kernel_id(self, kernel_id): | |||
|
323 | """Check a that a kernel_id exists and raise 404 if not.""" | |||
|
324 | if kernel_id not in self: | |||
|
325 | raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id) | |||
|
326 |
@@ -93,7 +93,7 b' class NotebookManager(LoggingConfigurable):' | |||||
93 | try: |
|
93 | try: | |
94 | name = self.mapping[notebook_id] |
|
94 | name = self.mapping[notebook_id] | |
95 | except KeyError: |
|
95 | except KeyError: | |
96 | raise web.HTTPError(404) |
|
96 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) | |
97 | return self.get_path_by_name(name) |
|
97 | return self.get_path_by_name(name) | |
98 |
|
98 | |||
99 | def get_path_by_name(self, name): |
|
99 | def get_path_by_name(self, name): | |
@@ -106,7 +106,7 b' class NotebookManager(LoggingConfigurable):' | |||||
106 | """Get the representation of a notebook in format by notebook_id.""" |
|
106 | """Get the representation of a notebook in format by notebook_id.""" | |
107 | format = unicode(format) |
|
107 | format = unicode(format) | |
108 | if format not in self.allowed_formats: |
|
108 | if format not in self.allowed_formats: | |
109 | raise web.HTTPError(415) |
|
109 | raise web.HTTPError(415, u'Invalid notebook format: %s' % format) | |
110 | last_modified, nb = self.get_notebook_object(notebook_id) |
|
110 | last_modified, nb = self.get_notebook_object(notebook_id) | |
111 | data = current.writes(nb, format) |
|
111 | data = current.writes(nb, format) | |
112 | name = nb.get('name','notebook') |
|
112 | name = nb.get('name','notebook') | |
@@ -116,7 +116,7 b' class NotebookManager(LoggingConfigurable):' | |||||
116 | """Get the NotebookNode representation of a notebook by notebook_id.""" |
|
116 | """Get the NotebookNode representation of a notebook by notebook_id.""" | |
117 | path = self.find_path(notebook_id) |
|
117 | path = self.find_path(notebook_id) | |
118 | if not os.path.isfile(path): |
|
118 | if not os.path.isfile(path): | |
119 | raise web.HTTPError(404) |
|
119 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) | |
120 | info = os.stat(path) |
|
120 | info = os.stat(path) | |
121 | last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime) |
|
121 | last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime) | |
122 | with open(path,'r') as f: |
|
122 | with open(path,'r') as f: | |
@@ -125,7 +125,7 b' class NotebookManager(LoggingConfigurable):' | |||||
125 | # v1 and v2 and json in the .ipynb files. |
|
125 | # v1 and v2 and json in the .ipynb files. | |
126 | nb = current.reads(s, u'json') |
|
126 | nb = current.reads(s, u'json') | |
127 | except: |
|
127 | except: | |
128 |
raise web.HTTPError( |
|
128 | raise web.HTTPError(500, u'Unreadable JSON notebook.') | |
129 | if 'name' not in nb: |
|
129 | if 'name' not in nb: | |
130 | nb.name = os.path.split(path)[-1].split(u'.')[0] |
|
130 | nb.name = os.path.split(path)[-1].split(u'.')[0] | |
131 | return last_modified, nb |
|
131 | return last_modified, nb | |
@@ -137,18 +137,18 b' class NotebookManager(LoggingConfigurable):' | |||||
137 | and the value in the data is updated to use that value. |
|
137 | and the value in the data is updated to use that value. | |
138 | """ |
|
138 | """ | |
139 | if format not in self.allowed_formats: |
|
139 | if format not in self.allowed_formats: | |
140 | raise web.HTTPError(415) |
|
140 | raise web.HTTPError(415, u'Invalid notebook format: %s' % format) | |
141 |
|
141 | |||
142 | try: |
|
142 | try: | |
143 | nb = current.reads(data, format) |
|
143 | nb = current.reads(data, format) | |
144 | except: |
|
144 | except: | |
145 | raise web.HTTPError(400) |
|
145 | raise web.HTTPError(400, u'Invalid JSON data') | |
146 |
|
146 | |||
147 | if name is None: |
|
147 | if name is None: | |
148 | try: |
|
148 | try: | |
149 | name = nb.metadata.name |
|
149 | name = nb.metadata.name | |
150 | except AttributeError: |
|
150 | except AttributeError: | |
151 | raise web.HTTPError(400) |
|
151 | raise web.HTTPError(400, u'Missing notebook name') | |
152 | nb.metadata.name = name |
|
152 | nb.metadata.name = name | |
153 |
|
153 | |||
154 | notebook_id = self.new_notebook_id(name) |
|
154 | notebook_id = self.new_notebook_id(name) | |
@@ -158,12 +158,12 b' class NotebookManager(LoggingConfigurable):' | |||||
158 | def save_notebook(self, notebook_id, data, name=None, format=u'json'): |
|
158 | def save_notebook(self, notebook_id, data, name=None, format=u'json'): | |
159 | """Save an existing notebook by notebook_id.""" |
|
159 | """Save an existing notebook by notebook_id.""" | |
160 | if format not in self.allowed_formats: |
|
160 | if format not in self.allowed_formats: | |
161 | raise web.HTTPError(415) |
|
161 | raise web.HTTPError(415, u'Invalid notebook format: %s' % format) | |
162 |
|
162 | |||
163 | try: |
|
163 | try: | |
164 | nb = current.reads(data, format) |
|
164 | nb = current.reads(data, format) | |
165 | except: |
|
165 | except: | |
166 | raise web.HTTPError(400) |
|
166 | raise web.HTTPError(400, u'Invalid JSON data') | |
167 |
|
167 | |||
168 | if name is not None: |
|
168 | if name is not None: | |
169 | nb.metadata.name = name |
|
169 | nb.metadata.name = name | |
@@ -172,18 +172,18 b' class NotebookManager(LoggingConfigurable):' | |||||
172 | def save_notebook_object(self, notebook_id, nb): |
|
172 | def save_notebook_object(self, notebook_id, nb): | |
173 | """Save an existing notebook object by notebook_id.""" |
|
173 | """Save an existing notebook object by notebook_id.""" | |
174 | if notebook_id not in self.mapping: |
|
174 | if notebook_id not in self.mapping: | |
175 | raise web.HTTPError(404) |
|
175 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) | |
176 | old_name = self.mapping[notebook_id] |
|
176 | old_name = self.mapping[notebook_id] | |
177 | try: |
|
177 | try: | |
178 | new_name = nb.metadata.name |
|
178 | new_name = nb.metadata.name | |
179 | except AttributeError: |
|
179 | except AttributeError: | |
180 | raise web.HTTPError(400) |
|
180 | raise web.HTTPError(400, u'Missing notebook name') | |
181 | path = self.get_path_by_name(new_name) |
|
181 | path = self.get_path_by_name(new_name) | |
182 | try: |
|
182 | try: | |
183 | with open(path,'w') as f: |
|
183 | with open(path,'w') as f: | |
184 | current.write(nb, f, u'json') |
|
184 | current.write(nb, f, u'json') | |
185 | except: |
|
185 | except: | |
186 | raise web.HTTPError(400) |
|
186 | raise web.HTTPError(400, u'Unexpected error while saving notebook') | |
187 | if old_name != new_name: |
|
187 | if old_name != new_name: | |
188 | old_path = self.get_path_by_name(old_name) |
|
188 | old_path = self.get_path_by_name(old_name) | |
189 | if os.path.isfile(old_path): |
|
189 | if os.path.isfile(old_path): | |
@@ -195,7 +195,7 b' class NotebookManager(LoggingConfigurable):' | |||||
195 | """Delete notebook by notebook_id.""" |
|
195 | """Delete notebook by notebook_id.""" | |
196 | path = self.find_path(notebook_id) |
|
196 | path = self.find_path(notebook_id) | |
197 | if not os.path.isfile(path): |
|
197 | if not os.path.isfile(path): | |
198 | raise web.HTTPError(404) |
|
198 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) | |
199 | os.unlink(path) |
|
199 | os.unlink(path) | |
200 | self.delete_notebook_id(notebook_id) |
|
200 | self.delete_notebook_id(notebook_id) | |
201 |
|
201 |
General Comments 0
You need to be logged in to leave comments.
Login now