notebook/IPython/html/services/notebooks/handlers.py

201 lines
7.3 KiB
Python
Raw Normal View History

"""Tornado handlers for the notebooks web service.
2013-05-03 02:15:32 +08:00
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
from zmq.utils import jsonapi
from IPython.utils.jsonutil import date_default
from ...base.handlers import IPythonHandler
2013-05-03 02:15:32 +08:00
#-----------------------------------------------------------------------------
# Notebook web service handlers
#-----------------------------------------------------------------------------
2013-07-04 01:35:30 +08:00
2013-05-03 02:15:32 +08:00
class NotebookRootHandler(IPythonHandler):
@web.authenticated
2013-05-03 02:15:32 +08:00
def get(self):
nbm = self.notebook_manager
notebooks = nbm.list_notebooks("")
2013-07-04 01:35:30 +08:00
self.finish(jsonapi.dumps(notebooks))
2013-05-03 02:15:32 +08:00
@web.authenticated
def post(self):
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
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))
2013-07-26 01:38:08 +08:00
2013-07-04 01:35:30 +08:00
class NotebookRootRedirect(IPythonHandler):
2013-07-26 03:57:14 +08:00
@web.authenticated
2013-07-04 01:35:30 +08:00
def get(self):
self.redirect("/api/notebooks")
2013-05-03 02:15:32 +08:00
class NotebookHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'DELETE')
2013-05-03 02:15:32 +08:00
@web.authenticated
2013-07-04 01:35:30 +08:00
def get(self, notebook_path):
2013-05-03 02:15:32 +08:00
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
name, path = nbm.named_notebook_path(notebook_path)
2013-05-03 02:15:32 +08:00
2013-07-04 01:35:30 +08:00
if name == None:
notebooks = nbm.list_notebooks(path)
2013-07-04 01:35:30 +08:00
self.finish(jsonapi.dumps(notebooks))
else:
format = self.get_argument('format', default='json')
download = self.get_argument('download', default='False')
2013-07-04 01:35:30 +08:00
model = nbm.notebook_model(name,path)
last_mod, representation, name = nbm.get_notebook(name, path, format)
self.set_header('Last-Modified', last_mod)
if download == 'True':
if format == u'json':
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Disposition','attachment; filename="%s.ipynb"' % name)
self.finish(representation)
elif format == u'py':
self.set_header('Content-Type', 'application/x-python')
self.set_header('Content-Disposition','attachment; filename="%s.py"' % name)
self.finish(representation)
else:
self.finish(jsonapi.dumps(model))
2013-05-03 02:15:32 +08:00
@web.authenticated
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)
2013-07-27 02:57:48 +08:00
model, response = nbm.change_notebook(data, notebook_name, notebook_path)
self.set_status(response)
self.finish(jsonapi.dumps(model))
2013-05-03 02:15:32 +08:00
@web.authenticated
2013-07-04 01:35:30 +08:00
def put(self, notebook_path):
2013-05-03 02:15:32 +08:00
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
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)
self.set_header('Location', nbm.notebook_dir + notebook_path + notebook_name)
2013-07-04 01:35:30 +08:00
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))
2013-05-03 02:15:32 +08:00
@web.authenticated
2013-07-04 01:35:30 +08:00
def delete(self, notebook_path):
nbm = self.notebook_manager
name, path = nbm.named_notebook_path(notebook_path)
2013-07-04 03:25:53 +08:00
nbm.delete_notebook(name, path)
2013-05-03 02:15:32 +08:00
self.set_status(204)
self.finish()
class NotebookCheckpointsHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET', 'POST')
@web.authenticated
2013-07-04 01:35:30 +08:00
def get(self, notebook_path):
2013-05-03 02:15:32 +08:00
"""get lists checkpoints for a notebook"""
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
name, path = nbm.named_notebook_path(notebook_path)
checkpoints = nbm.list_checkpoints(name, path)
2013-05-03 02:15:32 +08:00
data = jsonapi.dumps(checkpoints, default=date_default)
self.finish(data)
@web.authenticated
2013-07-04 01:35:30 +08:00
def post(self, notebook_path):
2013-05-03 02:15:32 +08:00
"""post creates a new checkpoint"""
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
name, path = nbm.named_notebook_path(notebook_path)
checkpoint = nbm.create_checkpoint(name, path)
2013-05-03 02:15:32 +08:00
data = jsonapi.dumps(checkpoint, default=date_default)
2013-07-04 01:35:30 +08:00
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']
))
2013-05-03 02:15:32 +08:00
self.finish(data)
class ModifyNotebookCheckpointsHandler(IPythonHandler):
SUPPORTED_METHODS = ('POST', 'DELETE')
@web.authenticated
2013-07-04 01:35:30 +08:00
def post(self, notebook_path, checkpoint_id):
2013-05-03 02:15:32 +08:00
"""post restores a notebook from a checkpoint"""
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
name, path = nbm.named_notebook_path(notebook_path)
nbm.restore_checkpoint(name, checkpoint_id, path)
2013-05-03 02:15:32 +08:00
self.set_status(204)
self.finish()
@web.authenticated
2013-07-04 01:35:30 +08:00
def delete(self, notebook_path, checkpoint_id):
2013-05-03 02:15:32 +08:00
"""delete clears a checkpoint for a given notebook"""
nbm = self.notebook_manager
2013-07-04 01:35:30 +08:00
name, path = nbm.named_notebook_path(notebook_path)
nbm.delete_checkpoint(name, checkpoint_id, path)
2013-05-03 02:15:32 +08:00
self.set_status(204)
self.finish()
2013-07-04 01:35:30 +08:00
2013-05-16 01:55:43 +08:00
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
2013-07-04 01:35:30 +08:00
_notebook_path_regex = r"(?P<notebook_path>.+)"
2013-05-16 01:55:43 +08:00
_checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
default_handlers = [
2013-07-04 01:35:30 +08:00
(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),
2013-05-16 01:55:43 +08:00
]
2013-05-03 02:15:32 +08:00