##// END OF EJS Templates
logo link to current tree level...
logo link to current tree level rather than top level

File last commit:

r13045:cc15133f
r13064:a2287526
Show More
handlers.py
206 lines | 7.8 KiB | text/x-python | PythonLexer
Brian E. Granger
Splitting handlers into different files....
r10642 """Tornado handlers for the notebooks web service.
Brian E. Granger
Adding new files.
r10641
Authors:
* Brian Granger
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 import json
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 from tornado import web
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 from ...utils import url_path_join
Brian E. Granger
Adding new files.
r10641 from IPython.utils.jsonutil import date_default
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 from ...base.handlers import IPythonHandler, json_errors
Brian E. Granger
Adding new files.
r10641
#-----------------------------------------------------------------------------
# Notebook web service handlers
#-----------------------------------------------------------------------------
Zachary Sailer
manual rebase notebooks web services
r12984
Brian E. Granger
Adding new files.
r10641 class NotebookHandler(IPythonHandler):
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE')
def notebook_location(self, name, path):
"""Return the full URL location of a notebook based.
Parameters
----------
name : unicode
The name of the notebook like "foo.ipynb".
path : unicode
The URL path of the notebook.
"""
return url_path_join(self.base_project_url, u'/api/notebooks', path, name)
Brian E. Granger
Adding new files.
r10641
MinRK
remove notebook read-only view...
r11644 @web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def get(self, notebook_path):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 """get checks if a notebook is not named, an returns a list of notebooks
in the notebook path given. If a name is given, return
the notebook representation"""
Brian E. Granger
Adding new files.
r10641 nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
Brian E. Granger
Adding new files.
r10641
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Check to see if a notebook name was given
if name is None:
# List notebooks in 'notebook_path'
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 notebooks = nbm.list_notebooks(path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 self.finish(json.dumps(notebooks, default=date_default))
Zachary Sailer
manual rebase notebooks web services
r12984 else:
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # get and return notebook representation
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 model = nbm.get_notebook_model(name, path)
self.set_header(u'Last-Modified', model[u'last_modified'])
self.finish(json.dumps(model, default=date_default))
Brian E. Granger
Adding new files.
r10641
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # @json_errors
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 def patch(self, notebook_path):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 """patch is currently used strictly for notebook renaming.
Changes the notebook name to the name given in data."""
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Paul Ivanov
named_notebook_path: consistent usage convention
r13029 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 if name is None:
raise web.HTTPError(400, u'Notebook name missing')
model = self.get_json_body()
if model is None:
raise web.HTTPError(400, u'JSON body missing')
model = nbm.update_notebook_model(model, name, path)
if model[u'name'] != name or model[u'path'] != path:
self.set_status(301)
location = self.notebook_location(model[u'name'], model[u'path'])
self.set_header(u'Location', location)
self.set_header(u'Last-Modified', model[u'last_modified'])
self.finish(json.dumps(model, default=date_default))
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
def post(self, notebook_path):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 """Create a new notebook in the location given by 'notebook_path'."""
Brian E. Granger
Adding new files.
r10641 nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
name, path = nbm.named_notebook_path(notebook_path)
model = self.get_json_body()
if name is not None:
raise web.HTTPError(400, 'No name can be provided when POSTing a new notebook.')
model = nbm.create_notebook_model(model, path)
location = nbm.notebook_dir + model[u'path'] + model[u'name']
location = self.notebook_location(model[u'name'], model[u'path'])
self.set_header(u'Location', location)
self.set_header(u'Last-Modified', model[u'last_modified'])
self.set_status(201)
self.finish(json.dumps(model, default=date_default))
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 def put(self, notebook_path):
"""saves the notebook in the location given by 'notebook_path'."""
nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 model = self.get_json_body()
if model is None:
raise web.HTTPError(400, u'JSON body missing')
nbm.save_notebook_model(model, name, path)
self.finish(json.dumps(model, default=date_default))
Brian E. Granger
Adding new files.
r10641
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def delete(self, notebook_path):
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 """delete the notebook in the given notebook path"""
Zachary Sailer
manual rebase notebooks web services
r12984 nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 nbm.delete_notebook_model(name, path)
Brian E. Granger
Adding new files.
r10641 self.set_status(204)
self.finish()
class NotebookCheckpointsHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET', 'POST')
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def get(self, notebook_path):
Brian E. Granger
Adding new files.
r10641 """get lists checkpoints for a notebook"""
nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
checkpoints = nbm.list_checkpoints(name, path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 data = json.dumps(checkpoints, default=date_default)
Brian E. Granger
Adding new files.
r10641 self.finish(data)
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def post(self, notebook_path):
Brian E. Granger
Adding new files.
r10641 """post creates a new checkpoint"""
nbm = self.notebook_manager
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 checkpoint = nbm.create_checkpoint(name, path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 data = json.dumps(checkpoint, default=date_default)
location = url_path_join(self.base_project_url, u'/api/notebooks',
path, name, '/checkpoints', checkpoint[u'checkpoint_id'])
self.set_header(u'Location', location)
Brian E. Granger
Adding new files.
r10641 self.finish(data)
class ModifyNotebookCheckpointsHandler(IPythonHandler):
SUPPORTED_METHODS = ('POST', 'DELETE')
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def post(self, notebook_path, checkpoint_id):
Brian E. Granger
Adding new files.
r10641 """post restores a notebook from a checkpoint"""
nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 nbm.restore_checkpoint(checkpoint_id, name, path)
Brian E. Granger
Adding new files.
r10641 self.set_status(204)
self.finish()
@web.authenticated
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 @json_errors
Zachary Sailer
manual rebase notebooks web services
r12984 def delete(self, notebook_path, checkpoint_id):
Brian E. Granger
Adding new files.
r10641 """delete clears a checkpoint for a given notebook"""
nbm = self.notebook_manager
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # path will have leading and trailing slashes, such as '/foo/bar/'
Zachary Sailer
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 nbm.delete_checkpoint(checkpoint_id, name, path)
Brian E. Granger
Adding new files.
r10641 self.set_status(204)
self.finish()
Zachary Sailer
manual rebase notebooks web services
r12984
Brian E. Granger
More work on the handlers
r10647 #-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 _notebook_path_regex = r"(?P<notebook_path>.*)"
Brian E. Granger
More work on the handlers
r10647 _checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
default_handlers = [
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 (r"/api/notebooks/%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler),
(r"/api/notebooks/%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex),
Zachary Sailer
manual rebase notebooks web services
r12984 ModifyNotebookCheckpointsHandler),
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 (r"/api/notebooks%s" % _notebook_path_regex, NotebookHandler),
Brian E. Granger
More work on the handlers
r10647 ]
Brian E. Granger
Adding new files.
r10641