handlers.py
79 lines
| 2.8 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 | ||||
#----------------------------------------------------------------------------- | ||||
from tornado import web | ||||
Brian E. Granger
|
r10642 | HTTPError = web.HTTPError | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | from ..base.handlers import IPythonHandler | ||
MinRK
|
r13067 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex | ||
Thomas Kluyver
|
r13398 | from ..utils import url_path_join, url_escape | ||
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 | ||||
MinRK
|
r13067 | def get(self, path='', name=None): | ||
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.""" | ||||
MinRK
|
r13117 | path = path.strip('/') | ||
Brian E. Granger
|
r10641 | nbm = self.notebook_manager | ||
MinRK
|
r13067 | if name is None: | ||
MinRK
|
r13073 | raise web.HTTPError(500, "This shouldn't be accessible: %s" % self.request.uri) | ||
MinRK
|
r13067 | # a .ipynb filename was given | ||
if not nbm.notebook_exists(name, path): | ||||
raise web.HTTPError(404, u'Notebook does not exist: %s/%s' % (path, name)) | ||||
MinRK
|
r13068 | name = url_escape(name) | ||
path = url_escape(path) | ||||
MinRK
|
r13067 | 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, | ||||
) | ||||
) | ||||
Zachary Sailer
|
r13016 | |||
MinRK
|
r13073 | class NotebookRedirectHandler(IPythonHandler): | ||
def get(self, path=''): | ||||
Zachary Sailer
|
r13017 | nbm = self.notebook_manager | ||
MinRK
|
r13073 | if nbm.path_exists(path): | ||
# it's a *directory*, redirect to /tree | ||||
url = url_path_join(self.base_project_url, 'tree', path) | ||||
Paul Ivanov
|
r13029 | else: | ||
MinRK
|
r13073 | # otherwise, redirect to /files | ||
# TODO: This should check if it's actually a file | ||||
url = url_path_join(self.base_project_url, 'files', path) | ||||
MinRK
|
r13135 | url = url_escape(url) | ||
self.log.debug("Redirecting %s to %s", self.request.path, url) | ||||
MinRK
|
r13073 | self.redirect(url) | ||
Brian E. Granger
|
r10641 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
default_handlers = [ | ||||
MinRK
|
r13079 | (r"/notebooks%s" % _notebook_path_regex, NotebookHandler), | ||
(r"/notebooks%s" % _path_regex, NotebookRedirectHandler), | ||||
Zachary Sailer
|
r13033 | ] | ||