handlers.py
104 lines
| 3.4 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r10642 | """Tornado handlers for the live notebook view. | ||
Brian E. Granger
|
r10641 | |||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r10642 | # Copyright (C) 2011 The IPython Development Team | ||
Brian E. Granger
|
r10641 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
import os | ||||
from tornado import web | ||||
Brian E. Granger
|
r10642 | HTTPError = web.HTTPError | ||
Zachary Sailer
|
r13016 | from zmq.utils import jsonapi | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | from ..base.handlers import IPythonHandler | ||
Brian E. Granger
|
r10642 | from ..utils import url_path_join | ||
Zachary Sailer
|
r13008 | from urllib import quote | ||
Brian E. Granger
|
r10641 | |||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r10642 | # Handlers | ||
Brian E. Granger
|
r10641 | #----------------------------------------------------------------------------- | ||
Zachary Sailer
|
r13016 | class NotebookHandler(IPythonHandler): | ||
Brian E. Granger
|
r10641 | |||
@web.authenticated | ||||
Zachary Sailer
|
r13016 | def post(self): | ||
Paul Ivanov
|
r13029 | """post either creates a new notebook if no json data is | ||
sent to the server, or copies the data and returns a | ||||
copied notebook.""" | ||||
Zachary Sailer
|
r13017 | nbm = self.notebook_manager | ||
data=self.request.body | ||||
Paul Ivanov
|
r13029 | if data: | ||
Zachary Sailer
|
r13017 | data = jsonapi.loads(data) | ||
notebook_name = nbm.copy_notebook(data['name']) | ||||
Paul Ivanov
|
r13029 | else: | ||
notebook_name = nbm.new_notebook() | ||||
Zachary Sailer
|
r13016 | self.finish(jsonapi.dumps({"name": notebook_name})) | ||
Brian E. Granger
|
r10642 | |||
Brian E. Granger
|
r10641 | |||
class NamedNotebookHandler(IPythonHandler): | ||||
MinRK
|
r11644 | @web.authenticated | ||
Zachary Sailer
|
r12981 | def get(self, notebook_path): | ||
Paul Ivanov
|
r13029 | """get renders the notebook template if a name is given, or | ||
redirects to the '/files/' handler if the name is not given.""" | ||||
Brian E. Granger
|
r10641 | nbm = self.notebook_manager | ||
Zachary Sailer
|
r12981 | name, path = nbm.named_notebook_path(notebook_path) | ||
Paul Ivanov
|
r13029 | if name is not None: | ||
# a .ipynb filename was given | ||||
if not nbm.notebook_exists(name, path): | ||||
raise web.HTTPError(404, u'Notebook does not exist: %s' % name) | ||||
Zachary Sailer
|
r13013 | name = nbm.url_encode(name) | ||
path = nbm.url_encode(path) | ||||
Paul Ivanov
|
r13029 | self.write(self.render_template('notebook.html', | ||
project=self.project_dir, | ||||
notebook_path=path, | ||||
notebook_name=name, | ||||
kill_kernel=False, | ||||
mathjax_url=self.mathjax_url, | ||||
) | ||||
Brian E. Granger
|
r10641 | ) | ||
Paul Ivanov
|
r13029 | else: | ||
url = "/files/" + notebook_path | ||||
self.redirect(url) | ||||
Zachary Sailer
|
r13016 | |||
Zachary Sailer
|
r13011 | @web.authenticated | ||
def post(self, notebook_path): | ||||
Paul Ivanov
|
r13029 | """post either creates a new notebook if no json data is | ||
sent to the server, or copies the data and returns a | ||||
copied notebook in the location given by 'notebook_path.""" | ||||
Zachary Sailer
|
r13017 | nbm = self.notebook_manager | ||
data = self.request.body | ||||
Paul Ivanov
|
r13029 | if data: | ||
Zachary Sailer
|
r13017 | data = jsonapi.loads(data) | ||
notebook_name = nbm.copy_notebook(data['name'], notebook_path) | ||||
Paul Ivanov
|
r13029 | else: | ||
notebook_name = nbm.new_notebook(notebook_path) | ||||
Zachary Sailer
|
r13016 | self.finish(jsonapi.dumps({"name": notebook_name})) | ||
Brian E. Granger
|
r10641 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r12981 | _notebook_path_regex = r"(?P<notebook_path>.+)" | ||
Brian E. Granger
|
r10647 | |||
default_handlers = [ | ||||
Zachary Sailer
|
r13016 | (r"/notebooks/%s" % _notebook_path_regex, NamedNotebookHandler), | ||
Paul Ivanov
|
r13029 | (r"/notebooks/", NotebookHandler), | ||
Zachary Sailer
|
r13033 | ] | ||