handlers.py
51 lines
| 1.5 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17533 | """Tornado handlers for the live notebook view.""" | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r17533 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r10641 | |||
MinRK
|
r13563 | import os | ||
Brian E. Granger
|
r10641 | from tornado import web | ||
Brian E. Granger
|
r10642 | HTTPError = web.HTTPError | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r17533 | from ..base.handlers import ( | ||
IPythonHandler, FilesRedirectHandler, | ||||
notebook_path_regex, path_regex, | ||||
) | ||||
from ..utils import url_escape | ||||
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('/') | ||
MinRK
|
r17524 | cm = self.contents_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 | ||
MinRK
|
r17524 | if not cm.file_exists(name, path): | ||
MinRK
|
r13067 | 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', | ||
notebook_path=path, | ||||
notebook_name=name, | ||||
kill_kernel=False, | ||||
mathjax_url=self.mathjax_url, | ||||
) | ||||
) | ||||
Zachary Sailer
|
r13016 | |||
Brian E. Granger
|
r10641 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
default_handlers = [ | ||||
Thomas Kluyver
|
r13916 | (r"/notebooks%s" % notebook_path_regex, NotebookHandler), | ||
MinRK
|
r17533 | (r"/notebooks%s" % path_regex, FilesRedirectHandler), | ||
Zachary Sailer
|
r13033 | ] | ||