Show More
handlers.py
56 lines
| 1.7 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 ( | ||
|
r19675 | IPythonHandler, FilesRedirectHandler, path_regex, | ||
|
r17533 | ) | ||
from ..utils import url_escape | ||||
|
r10641 | |||
|
r13016 | class NotebookHandler(IPythonHandler): | ||
|
r10641 | |||
@web.authenticated | ||||
|
r18758 | 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 | |||
|
r19675 | # will raise 404 on not found | ||
|
r19676 | try: | ||
model = cm.get(path, content=False) | ||||
except web.HTTPError as e: | ||||
|
r19741 | if e.status_code == 404 and 'files' in path.split('/'): | ||
|
r19676 | # 404, but '/files/' in URL, let FilesRedirect take care of it | ||
|
r19742 | return FilesRedirectHandler.redirect_to_files(self, path) | ||
|
r19676 | else: | ||
raise | ||||
|
r19675 | if model['type'] != 'notebook': | ||
# not a notebook, redirect to files | ||||
|
r19742 | return FilesRedirectHandler.redirect_to_files(self, path) | ||
|
r18749 | 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 = [ | ||||
|
r19675 | (r"/notebooks%s" % path_regex, NotebookHandler), | ||
|
r13033 | ] | ||