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