##// END OF EJS Templates
allow rename in subdirectory notebooks
allow rename in subdirectory notebooks

File last commit:

r12997:aa2ea6fd
r12998:a7f76924
Show More
handlers.py
200 lines | 7.3 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
#-----------------------------------------------------------------------------
from tornado import web
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 import ast
Brian E. Granger
Adding new files.
r10641
from zmq.utils import jsonapi
from IPython.utils.jsonutil import date_default
MinRK
remove notebook read-only view...
r11644 from ...base.handlers import IPythonHandler
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 NotebookRootHandler(IPythonHandler):
MinRK
remove notebook read-only view...
r11644 @web.authenticated
Brian E. Granger
Adding new files.
r10641 def get(self):
nbm = self.notebook_manager
km = self.kernel_manager
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 notebooks = nbm.list_notebooks("")
Zachary Sailer
manual rebase notebooks web services
r12984 self.finish(jsonapi.dumps(notebooks))
Brian E. Granger
Adding new files.
r10641
@web.authenticated
def post(self):
nbm = self.notebook_manager
Zachary Sailer
manual rebase notebooks web services
r12984 notebook_name = nbm.new_notebook()
model = nbm.notebook_model(notebook_name)
self.set_header('Location', '{0}api/notebooks/{1}'.format(self.base_project_url, notebook_name))
self.finish(jsonapi.dumps(model))
class NotebookRootRedirect(IPythonHandler):
@authenticate_unless_readonly
def get(self):
self.redirect("/api/notebooks")
Brian E. Granger
Adding new files.
r10641
class NotebookHandler(IPythonHandler):
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'DELETE')
Brian E. Granger
Adding new files.
r10641
MinRK
remove notebook read-only view...
r11644 @web.authenticated
Zachary Sailer
manual rebase notebooks web services
r12984 def get(self, notebook_path):
Brian E. Granger
Adding new files.
r10641 nbm = self.notebook_manager
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
manual rebase notebooks web services
r12984 if name == None:
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 notebooks = nbm.list_notebooks(path)
Zachary Sailer
manual rebase notebooks web services
r12984 self.finish(jsonapi.dumps(notebooks))
else:
format = self.get_argument('format', default='json')
model = nbm.notebook_model(name,path)
data, name = nbm.get_notebook(model, format)
if format == u'json':
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Disposition','attachment; filename="%s.ipynb"' % name)
elif format == u'py':
self.set_header('Content-Type', 'application/x-python')
self.set_header('Content-Disposition','attachment; filename="%s.py"' % name)
#self.set_header('Last-Modified', last_mod)
self.finish(jsonapi.dumps(model))
Brian E. Granger
Adding new files.
r10641
@web.authenticated
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 def patch(self, notebook_path):
nbm = self.notebook_manager
notebook_name, notebook_path = nbm.named_notebook_path(notebook_path)
data = jsonapi.loads(self.request.body)
model = nbm.change_notebook(data, notebook_name, notebook_path)
self.log.info(model)
self.finish(jsonapi.dumps(model))
@web.authenticated
Zachary Sailer
manual rebase notebooks web services
r12984 def put(self, notebook_path):
Brian E. Granger
Adding new files.
r10641 nbm = self.notebook_manager
Zachary Sailer
manual rebase notebooks web services
r12984 notebook_name, notebook_path = nbm.named_notebook_path(notebook_path)
if notebook_name == None:
body = self.request.body.strip()
format = self.get_argument('format', default='json')
name = self.get_argument('name', default=None)
if body:
notebook_name = nbm.save_new_notebook(body, notebook_path=notebook_path, name=name, format=format)
else:
notebook_name = nbm.new_notebook(notebook_path=notebook_path)
Zachary Sailer
fixing path redirects, cleaning path logic
r12992 if notebook_path==None:
Zachary Sailer
manual rebase notebooks web services
r12984 self.set_header('Location', nbm.notebook_dir + '/'+ notebook_name)
else:
self.set_header('Location', nbm.notebook_dir + '/'+ notebook_path + '/' + notebook_name)
model = nbm.notebook_model(notebook_name, notebook_path)
self.finish(jsonapi.dumps(model))
else:
format = self.get_argument('format', default='json')
name = self.get_argument('name', default=None)
nbm.save_notebook(self.request.body, notebook_path=notebook_path, name=name, format=format)
model = nbm.notebook_model(notebook_name, notebook_path)
self.set_status(204)
self.finish(jsonapi.dumps(model))
Brian E. Granger
Adding new files.
r10641
@web.authenticated
Zachary Sailer
manual rebase notebooks web services
r12984 def delete(self, notebook_path):
nbm = self.notebook_manager
name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
fixing some bugs after rebase
r12991 nbm.delete_notebook(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
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
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
checkpoints = nbm.list_checkpoints(name, path)
Brian E. Granger
Adding new files.
r10641 data = jsonapi.dumps(checkpoints, default=date_default)
self.finish(data)
@web.authenticated
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)
checkpoint = nbm.create_checkpoint(name, path)
Brian E. Granger
Adding new files.
r10641 data = jsonapi.dumps(checkpoint, default=date_default)
Zachary Sailer
manual rebase notebooks web services
r12984 if path == None:
self.set_header('Location', '{0}notebooks/{1}/checkpoints/{2}'.format(
self.base_project_url, name, checkpoint['checkpoint_id']
))
else:
self.set_header('Location', '{0}notebooks/{1}/{2}/checkpoints/{3}'.format(
self.base_project_url, path, name, checkpoint['checkpoint_id']
))
Brian E. Granger
Adding new files.
r10641 self.finish(data)
class ModifyNotebookCheckpointsHandler(IPythonHandler):
SUPPORTED_METHODS = ('POST', 'DELETE')
@web.authenticated
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
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
nbm.restore_checkpoint(name, checkpoint_id, path)
Brian E. Granger
Adding new files.
r10641 self.set_status(204)
self.finish()
@web.authenticated
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
manual rebase notebooks web services
r12984 name, path = nbm.named_notebook_path(notebook_path)
nbm.delete_checkpoint(name, checkpoint_id, 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
manual rebase notebooks web services
r12984 _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
manual rebase notebooks web services
r12984 (r"api/notebooks/%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler),
(r"api/notebooks/%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex),
ModifyNotebookCheckpointsHandler),
(r"api/notebooks/%s" % _notebook_path_regex, NotebookHandler),
(r"api/notebooks/", NotebookRootRedirect),
(r"api/notebooks", NotebookRootHandler),
Brian E. Granger
More work on the handlers
r10647 ]
Brian E. Granger
Adding new files.
r10641