handlers.py
90 lines
| 3.3 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 | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r13563 | import os | ||
Brian E. Granger
|
r10641 | from tornado import web | ||
Brian E. Granger
|
r10642 | HTTPError = web.HTTPError | ||
Brian E. Granger
|
r10641 | |||
Thomas Kluyver
|
r13916 | from ..base.handlers import IPythonHandler, 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 | ||
MinRK
|
r13563 | if '/files/' in path: | ||
# redirect without files/ iff it would 404 | ||||
# this preserves pre-2.0-style 'files/' links | ||||
# FIXME: this is hardcoded based on notebook_path, | ||||
# but so is the files handler itself, | ||||
# so it should work until both are cleaned up. | ||||
parts = path.split('/') | ||||
files_path = os.path.join(nbm.notebook_dir, *parts) | ||||
self.log.warn("filespath: %s", files_path) | ||||
if not os.path.exists(files_path): | ||||
path = path.replace('/files/', '/', 1) | ||||
MinRK
|
r13073 | 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 = [ | ||||
Thomas Kluyver
|
r13916 | (r"/notebooks%s" % notebook_path_regex, NotebookHandler), | ||
(r"/notebooks%s" % path_regex, NotebookRedirectHandler), | ||||
Zachary Sailer
|
r13033 | ] | ||