Show More
@@ -1,246 +1,244 b'' | |||||
1 | """Tornado handlers for the notebooks web service. |
|
1 | """Tornado handlers for the notebooks web service. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import json |
|
19 | import json | |
20 |
|
20 | |||
21 | from tornado import web |
|
21 | from tornado import web | |
22 |
|
22 | |||
23 | from IPython.html.utils import url_path_join |
|
23 | from IPython.html.utils import url_path_join | |
24 | from IPython.utils.jsonutil import date_default |
|
24 | from IPython.utils.jsonutil import date_default | |
25 |
|
25 | |||
26 | from IPython.html.base.handlers import IPythonHandler, json_errors |
|
26 | from IPython.html.base.handlers import IPythonHandler, json_errors | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Notebook web service handlers |
|
29 | # Notebook web service handlers | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class NotebookHandler(IPythonHandler): |
|
33 | class NotebookHandler(IPythonHandler): | |
34 |
|
34 | |||
35 | SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE') |
|
35 | SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE') | |
36 |
|
36 | |||
37 | def notebook_location(self, name, path=''): |
|
37 | def notebook_location(self, name, path=''): | |
38 | """Return the full URL location of a notebook based. |
|
38 | """Return the full URL location of a notebook based. | |
39 |
|
39 | |||
40 | Parameters |
|
40 | Parameters | |
41 | ---------- |
|
41 | ---------- | |
42 | name : unicode |
|
42 | name : unicode | |
43 | The base name of the notebook, such as "foo.ipynb". |
|
43 | The base name of the notebook, such as "foo.ipynb". | |
44 | path : unicode |
|
44 | path : unicode | |
45 | The URL path of the notebook. |
|
45 | The URL path of the notebook. | |
46 | """ |
|
46 | """ | |
47 | return url_path_join(self.base_project_url, 'api', 'notebooks', path, name) |
|
47 | return url_path_join(self.base_project_url, 'api', 'notebooks', path, name) | |
48 |
|
48 | |||
49 | @web.authenticated |
|
49 | @web.authenticated | |
50 | @json_errors |
|
50 | @json_errors | |
51 | def get(self, path='', name=None): |
|
51 | def get(self, path='', name=None): | |
|
52 | """Return a Notebook or list of notebooks. | |||
|
53 | ||||
|
54 | * GET with path and no notebook name lists notebooks in a directory | |||
|
55 | * GET with path and notebook name returns notebook JSON | |||
52 |
|
|
56 | """ | |
53 | GET with path and no notebook lists notebooks in a directory |
|
|||
54 | GET with path and notebook name |
|
|||
55 |
|
||||
56 | GET get checks if a notebook is not named, an returns a list of notebooks |
|
|||
57 | in the notebook path given. If a name is given, return |
|
|||
58 | the notebook representation""" |
|
|||
59 | nbm = self.notebook_manager |
|
57 | nbm = self.notebook_manager | |
60 | # Check to see if a notebook name was given |
|
58 | # Check to see if a notebook name was given | |
61 | if name is None: |
|
59 | if name is None: | |
62 | # List notebooks in 'path' |
|
60 | # List notebooks in 'path' | |
63 | notebooks = nbm.list_notebooks(path) |
|
61 | notebooks = nbm.list_notebooks(path) | |
64 | self.finish(json.dumps(notebooks, default=date_default)) |
|
62 | self.finish(json.dumps(notebooks, default=date_default)) | |
65 | return |
|
63 | return | |
66 | # get and return notebook representation |
|
64 | # get and return notebook representation | |
67 | model = nbm.get_notebook_model(name, path) |
|
65 | model = nbm.get_notebook_model(name, path) | |
68 | self.set_header(u'Last-Modified', model[u'last_modified']) |
|
66 | self.set_header(u'Last-Modified', model[u'last_modified']) | |
69 |
|
67 | |||
70 | if self.get_argument('download', default='False') == 'True': |
|
68 | if self.get_argument('download', default='False') == 'True': | |
71 | format = self.get_argument('format', default='json') |
|
69 | format = self.get_argument('format', default='json') | |
72 | if format != u'json': |
|
70 | if format != u'json': | |
73 | self.set_header('Content-Type', 'application/json') |
|
71 | self.set_header('Content-Type', 'application/json') | |
74 | raise web.HTTPError(400, "Unrecognized format: %s" % format) |
|
72 | raise web.HTTPError(400, "Unrecognized format: %s" % format) | |
75 |
|
73 | |||
76 | self.set_header('Content-Disposition', |
|
74 | self.set_header('Content-Disposition', | |
77 | 'attachment; filename="%s"' % name |
|
75 | 'attachment; filename="%s"' % name | |
78 | ) |
|
76 | ) | |
79 | self.finish(json.dumps(model['content'], default=date_default)) |
|
77 | self.finish(json.dumps(model['content'], default=date_default)) | |
80 | else: |
|
78 | else: | |
81 | self.finish(json.dumps(model, default=date_default)) |
|
79 | self.finish(json.dumps(model, default=date_default)) | |
82 |
|
80 | |||
83 | @web.authenticated |
|
81 | @web.authenticated | |
84 | @json_errors |
|
82 | @json_errors | |
85 | def patch(self, path='', name=None): |
|
83 | def patch(self, path='', name=None): | |
86 | """PATCH renames a notebook without re-uploading content.""" |
|
84 | """PATCH renames a notebook without re-uploading content.""" | |
87 | nbm = self.notebook_manager |
|
85 | nbm = self.notebook_manager | |
88 | if name is None: |
|
86 | if name is None: | |
89 | raise web.HTTPError(400, u'Notebook name missing') |
|
87 | raise web.HTTPError(400, u'Notebook name missing') | |
90 | model = self.get_json_body() |
|
88 | model = self.get_json_body() | |
91 | if model is None: |
|
89 | if model is None: | |
92 | raise web.HTTPError(400, u'JSON body missing') |
|
90 | raise web.HTTPError(400, u'JSON body missing') | |
93 | model = nbm.update_notebook_model(model, name, path) |
|
91 | model = nbm.update_notebook_model(model, name, path) | |
94 | location = self.notebook_location(model[u'name'], model[u'path']) |
|
92 | location = self.notebook_location(model[u'name'], model[u'path']) | |
95 | self.set_header(u'Location', location) |
|
93 | self.set_header(u'Location', location) | |
96 | self.set_header(u'Last-Modified', model[u'last_modified']) |
|
94 | self.set_header(u'Last-Modified', model[u'last_modified']) | |
97 | self.finish(json.dumps(model, default=date_default)) |
|
95 | self.finish(json.dumps(model, default=date_default)) | |
98 |
|
96 | |||
99 | @web.authenticated |
|
97 | @web.authenticated | |
100 | @json_errors |
|
98 | @json_errors | |
101 | def post(self, path='', name=None): |
|
99 | def post(self, path='', name=None): | |
102 | """Create a new notebook in the specified path. |
|
100 | """Create a new notebook in the specified path. | |
103 |
|
101 | |||
104 | POST creates new notebooks. |
|
102 | POST creates new notebooks. | |
105 |
|
103 | |||
106 | POST /api/notebooks/path : new untitled notebook in path |
|
104 | POST /api/notebooks/path : new untitled notebook in path | |
107 | POST /api/notebooks/path/notebook.ipynb : new notebook with name in path |
|
105 | POST /api/notebooks/path/notebook.ipynb : new notebook with name in path | |
108 | If content specified upload notebook, otherwise start empty. |
|
106 | If content specified upload notebook, otherwise start empty. | |
109 | """ |
|
107 | """ | |
110 | nbm = self.notebook_manager |
|
108 | nbm = self.notebook_manager | |
111 | model = self.get_json_body() |
|
109 | model = self.get_json_body() | |
112 | if name is None: |
|
110 | if name is None: | |
113 | # creating new notebook, model doesn't make sense |
|
111 | # creating new notebook, model doesn't make sense | |
114 | if model is not None: |
|
112 | if model is not None: | |
115 | raise web.HTTPError(400, "Model not valid when creating untitled notebooks.") |
|
113 | raise web.HTTPError(400, "Model not valid when creating untitled notebooks.") | |
116 | model = nbm.create_notebook_model(path=path) |
|
114 | model = nbm.create_notebook_model(path=path) | |
117 | else: |
|
115 | else: | |
118 | if model is None: |
|
116 | if model is None: | |
119 | self.log.info("Creating new Notebook at %s/%s", path, name) |
|
117 | self.log.info("Creating new Notebook at %s/%s", path, name) | |
120 | model = {} |
|
118 | model = {} | |
121 | else: |
|
119 | else: | |
122 | self.log.info("Uploading Notebook to %s/%s", path, name) |
|
120 | self.log.info("Uploading Notebook to %s/%s", path, name) | |
123 | # set the model name from the URL |
|
121 | # set the model name from the URL | |
124 | model['name'] = name |
|
122 | model['name'] = name | |
125 | model = nbm.create_notebook_model(model, path) |
|
123 | model = nbm.create_notebook_model(model, path) | |
126 |
|
124 | |||
127 | location = self.notebook_location(model[u'name'], model[u'path']) |
|
125 | location = self.notebook_location(model[u'name'], model[u'path']) | |
128 | self.set_header(u'Location', location) |
|
126 | self.set_header(u'Location', location) | |
129 | self.set_header(u'Last-Modified', model[u'last_modified']) |
|
127 | self.set_header(u'Last-Modified', model[u'last_modified']) | |
130 | self.set_status(201) |
|
128 | self.set_status(201) | |
131 | self.finish(json.dumps(model, default=date_default)) |
|
129 | self.finish(json.dumps(model, default=date_default)) | |
132 |
|
130 | |||
133 | @web.authenticated |
|
131 | @web.authenticated | |
134 | @json_errors |
|
132 | @json_errors | |
135 | def put(self, path='', name=None): |
|
133 | def put(self, path='', name=None): | |
136 | """saves the notebook in the location given by 'notebook_path'.""" |
|
134 | """saves the notebook in the location given by 'notebook_path'.""" | |
137 | nbm = self.notebook_manager |
|
135 | nbm = self.notebook_manager | |
138 | model = self.get_json_body() |
|
136 | model = self.get_json_body() | |
139 | if model is None: |
|
137 | if model is None: | |
140 | raise web.HTTPError(400, u'JSON body missing') |
|
138 | raise web.HTTPError(400, u'JSON body missing') | |
141 | nbm.save_notebook_model(model, name, path) |
|
139 | nbm.save_notebook_model(model, name, path) | |
142 | self.finish(json.dumps(model, default=date_default)) |
|
140 | self.finish(json.dumps(model, default=date_default)) | |
143 |
|
141 | |||
144 | @web.authenticated |
|
142 | @web.authenticated | |
145 | @json_errors |
|
143 | @json_errors | |
146 | def delete(self, path='', name=None): |
|
144 | def delete(self, path='', name=None): | |
147 | """delete the notebook in the given notebook path""" |
|
145 | """delete the notebook in the given notebook path""" | |
148 | nbm = self.notebook_manager |
|
146 | nbm = self.notebook_manager | |
149 | nbm.delete_notebook_model(name, path) |
|
147 | nbm.delete_notebook_model(name, path) | |
150 | self.set_status(204) |
|
148 | self.set_status(204) | |
151 | self.finish() |
|
149 | self.finish() | |
152 |
|
150 | |||
153 | class NotebookCopyHandler(IPythonHandler): |
|
151 | class NotebookCopyHandler(IPythonHandler): | |
154 |
|
152 | |||
155 | SUPPORTED_METHODS = ('POST') |
|
153 | SUPPORTED_METHODS = ('POST') | |
156 |
|
154 | |||
157 | @web.authenticated |
|
155 | @web.authenticated | |
158 | @json_errors |
|
156 | @json_errors | |
159 | def post(self, path='', name=None): |
|
157 | def post(self, path='', name=None): | |
160 | """Copy an existing notebook.""" |
|
158 | """Copy an existing notebook.""" | |
161 | nbm = self.notebook_manager |
|
159 | nbm = self.notebook_manager | |
162 | model = self.get_json_body() |
|
160 | model = self.get_json_body() | |
163 | if name is None: |
|
161 | if name is None: | |
164 | raise web.HTTPError(400, "Notebook name required") |
|
162 | raise web.HTTPError(400, "Notebook name required") | |
165 | self.log.info("Copying Notebook %s/%s", path, name) |
|
163 | self.log.info("Copying Notebook %s/%s", path, name) | |
166 | model = nbm.copy_notebook(name, path) |
|
164 | model = nbm.copy_notebook(name, path) | |
167 | location = url_path_join( |
|
165 | location = url_path_join( | |
168 | self.base_project_url, 'api', 'notebooks', |
|
166 | self.base_project_url, 'api', 'notebooks', | |
169 | model['path'], model['name'], |
|
167 | model['path'], model['name'], | |
170 | ) |
|
168 | ) | |
171 | self.set_header(u'Location', location) |
|
169 | self.set_header(u'Location', location) | |
172 | self.set_header(u'Last-Modified', model[u'last_modified']) |
|
170 | self.set_header(u'Last-Modified', model[u'last_modified']) | |
173 | self.set_status(201) |
|
171 | self.set_status(201) | |
174 | self.finish(json.dumps(model, default=date_default)) |
|
172 | self.finish(json.dumps(model, default=date_default)) | |
175 |
|
173 | |||
176 |
|
174 | |||
177 | class NotebookCheckpointsHandler(IPythonHandler): |
|
175 | class NotebookCheckpointsHandler(IPythonHandler): | |
178 |
|
176 | |||
179 | SUPPORTED_METHODS = ('GET', 'POST') |
|
177 | SUPPORTED_METHODS = ('GET', 'POST') | |
180 |
|
178 | |||
181 | @web.authenticated |
|
179 | @web.authenticated | |
182 | @json_errors |
|
180 | @json_errors | |
183 | def get(self, path='', name=None): |
|
181 | def get(self, path='', name=None): | |
184 | """get lists checkpoints for a notebook""" |
|
182 | """get lists checkpoints for a notebook""" | |
185 | nbm = self.notebook_manager |
|
183 | nbm = self.notebook_manager | |
186 | checkpoints = nbm.list_checkpoints(name, path) |
|
184 | checkpoints = nbm.list_checkpoints(name, path) | |
187 | data = json.dumps(checkpoints, default=date_default) |
|
185 | data = json.dumps(checkpoints, default=date_default) | |
188 | self.finish(data) |
|
186 | self.finish(data) | |
189 |
|
187 | |||
190 | @web.authenticated |
|
188 | @web.authenticated | |
191 | @json_errors |
|
189 | @json_errors | |
192 | def post(self, path='', name=None): |
|
190 | def post(self, path='', name=None): | |
193 | """post creates a new checkpoint""" |
|
191 | """post creates a new checkpoint""" | |
194 | nbm = self.notebook_manager |
|
192 | nbm = self.notebook_manager | |
195 | checkpoint = nbm.create_checkpoint(name, path) |
|
193 | checkpoint = nbm.create_checkpoint(name, path) | |
196 | data = json.dumps(checkpoint, default=date_default) |
|
194 | data = json.dumps(checkpoint, default=date_default) | |
197 | location = url_path_join(self.base_project_url, u'/api/notebooks', |
|
195 | location = url_path_join(self.base_project_url, u'/api/notebooks', | |
198 | path, name, 'checkpoints', checkpoint[u'checkpoint_id']) |
|
196 | path, name, 'checkpoints', checkpoint[u'checkpoint_id']) | |
199 | self.set_header(u'Location', location) |
|
197 | self.set_header(u'Location', location) | |
200 | self.set_status(201) |
|
198 | self.set_status(201) | |
201 | self.finish(data) |
|
199 | self.finish(data) | |
202 |
|
200 | |||
203 |
|
201 | |||
204 | class ModifyNotebookCheckpointsHandler(IPythonHandler): |
|
202 | class ModifyNotebookCheckpointsHandler(IPythonHandler): | |
205 |
|
203 | |||
206 | SUPPORTED_METHODS = ('POST', 'DELETE') |
|
204 | SUPPORTED_METHODS = ('POST', 'DELETE') | |
207 |
|
205 | |||
208 | @web.authenticated |
|
206 | @web.authenticated | |
209 | @json_errors |
|
207 | @json_errors | |
210 | def post(self, path, name, checkpoint_id): |
|
208 | def post(self, path, name, checkpoint_id): | |
211 | """post restores a notebook from a checkpoint""" |
|
209 | """post restores a notebook from a checkpoint""" | |
212 | nbm = self.notebook_manager |
|
210 | nbm = self.notebook_manager | |
213 | nbm.restore_checkpoint(checkpoint_id, name, path) |
|
211 | nbm.restore_checkpoint(checkpoint_id, name, path) | |
214 | self.set_status(204) |
|
212 | self.set_status(204) | |
215 | self.finish() |
|
213 | self.finish() | |
216 |
|
214 | |||
217 | @web.authenticated |
|
215 | @web.authenticated | |
218 | @json_errors |
|
216 | @json_errors | |
219 | def delete(self, path, name, checkpoint_id): |
|
217 | def delete(self, path, name, checkpoint_id): | |
220 | """delete clears a checkpoint for a given notebook""" |
|
218 | """delete clears a checkpoint for a given notebook""" | |
221 | nbm = self.notebook_manager |
|
219 | nbm = self.notebook_manager | |
222 | nbm.delete_checkpoint(checkpoint_id, name, path) |
|
220 | nbm.delete_checkpoint(checkpoint_id, name, path) | |
223 | self.set_status(204) |
|
221 | self.set_status(204) | |
224 | self.finish() |
|
222 | self.finish() | |
225 |
|
223 | |||
226 | #----------------------------------------------------------------------------- |
|
224 | #----------------------------------------------------------------------------- | |
227 | # URL to handler mappings |
|
225 | # URL to handler mappings | |
228 | #----------------------------------------------------------------------------- |
|
226 | #----------------------------------------------------------------------------- | |
229 |
|
227 | |||
230 |
|
228 | |||
231 | _path_regex = r"(?P<path>(?:/.*)*)" |
|
229 | _path_regex = r"(?P<path>(?:/.*)*)" | |
232 | _checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)" |
|
230 | _checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)" | |
233 | _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)" |
|
231 | _notebook_name_regex = r"(?P<name>[^/]+\.ipynb)" | |
234 | _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) |
|
232 | _notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) | |
235 |
|
233 | |||
236 | default_handlers = [ |
|
234 | default_handlers = [ | |
237 | (r"/api/notebooks%s/copy" % _notebook_path_regex, NotebookCopyHandler), |
|
235 | (r"/api/notebooks%s/copy" % _notebook_path_regex, NotebookCopyHandler), | |
238 | (r"/api/notebooks%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler), |
|
236 | (r"/api/notebooks%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler), | |
239 | (r"/api/notebooks%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex), |
|
237 | (r"/api/notebooks%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex), | |
240 | ModifyNotebookCheckpointsHandler), |
|
238 | ModifyNotebookCheckpointsHandler), | |
241 | (r"/api/notebooks%s" % _notebook_path_regex, NotebookHandler), |
|
239 | (r"/api/notebooks%s" % _notebook_path_regex, NotebookHandler), | |
242 | (r"/api/notebooks%s" % _path_regex, NotebookHandler), |
|
240 | (r"/api/notebooks%s" % _path_regex, NotebookHandler), | |
243 | ] |
|
241 | ] | |
244 |
|
242 | |||
245 |
|
243 | |||
246 |
|
244 |
General Comments 0
You need to be logged in to leave comments.
Login now