handlers.py
49 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
|
r17533 | """Tornado handlers for the live notebook view.""" | ||
|
r10641 | |||
|
r17533 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
|
r10641 | |||
|
r13563 | import os | ||
|
r10641 | from tornado import web | ||
|
r10642 | HTTPError = web.HTTPError | ||
|
r10641 | |||
|
r17533 | from ..base.handlers import ( | ||
IPythonHandler, FilesRedirectHandler, | ||||
notebook_path_regex, path_regex, | ||||
) | ||||
from ..utils import url_escape | ||||
|
r10641 | |||
|
r13016 | class NotebookHandler(IPythonHandler): | ||
|
r10641 | |||
@web.authenticated | ||||
|
r18749 | def get(self, path=''): | ||
|
r13029 | """get renders the notebook template if a name is given, or | ||
redirects to the '/files/' handler if the name is not given.""" | ||||
|
r13117 | path = path.strip('/') | ||
|
r17524 | cm = self.contents_manager | ||
|
r13073 | |||
|
r13067 | # a .ipynb filename was given | ||
|
r18749 | if not cm.file_exists(path): | ||
raise web.HTTPError(404, u'Notebook does not exist: %s' % path) | ||||
name = url_escape(path.rsplit('/', 1)[-1]) | ||||
|
r13068 | path = url_escape(path) | ||
|
r13067 | self.write(self.render_template('notebook.html', | ||
notebook_path=path, | ||||
notebook_name=name, | ||||
kill_kernel=False, | ||||
mathjax_url=self.mathjax_url, | ||||
) | ||||
) | ||||
|
r13016 | |||
|
r10641 | |||
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
default_handlers = [ | ||||
|
r13916 | (r"/notebooks%s" % notebook_path_regex, NotebookHandler), | ||
|
r17533 | (r"/notebooks%s" % path_regex, FilesRedirectHandler), | ||
|
r13033 | ] | ||