handlers.py
98 lines
| 3.2 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 | ||||
#----------------------------------------------------------------------------- | ||||
import os | ||||
from tornado import web | ||||
Brian E. Granger
|
r10642 | HTTPError = web.HTTPError | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r11644 | from ..base.handlers import IPythonHandler | ||
Brian E. Granger
|
r10642 | from ..utils import url_path_join | ||
Zachary Sailer
|
r12981 | from urllib import quote | ||
Brian E. Granger
|
r10641 | |||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r10642 | # Handlers | ||
Brian E. Granger
|
r10641 | #----------------------------------------------------------------------------- | ||
Zachary Sailer
|
r12981 | class NewPathHandler(IPythonHandler): | ||
@web.authenticated | ||||
def get(self, notebook_path): | ||||
notebook_name = self.notebook_manager.new_notebook(notebook_path) | ||||
self.redirect(url_path_join(self.base_project_url,"notebooks", notebook_path, notebook_name)) | ||||
Brian E. Granger
|
r10641 | class NewHandler(IPythonHandler): | ||
@web.authenticated | ||||
def get(self): | ||||
Zachary Sailer
|
r12981 | notebook_name = self.notebook_manager.new_notebook() | ||
self.redirect(url_path_join(self.base_project_url, "notebooks", notebook_name)) | ||||
Brian E. Granger
|
r10642 | |||
Brian E. Granger
|
r10641 | |||
class NamedNotebookHandler(IPythonHandler): | ||||
MinRK
|
r11644 | @web.authenticated | ||
Zachary Sailer
|
r12981 | def get(self, notebook_path): | ||
Brian E. Granger
|
r10641 | nbm = self.notebook_manager | ||
Zachary Sailer
|
r12981 | name, path = nbm.named_notebook_path(notebook_path) | ||
if name != None: | ||||
name = quote(name) | ||||
self.log.info(name) | ||||
if path == None: | ||||
project = self.project + '/' + name | ||||
else: | ||||
project = self.project + '/' + path +'/'+ name | ||||
#if not nbm.notebook_exists(notebook_path): | ||||
# raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_path) | ||||
Brian E. Granger
|
r10723 | self.write(self.render_template('notebook.html', | ||
Zachary Sailer
|
r12981 | project=project, | ||
notebook_path=path, | ||||
notebook_name=name, | ||||
Brian E. Granger
|
r10641 | kill_kernel=False, | ||
mathjax_url=self.mathjax_url, | ||||
) | ||||
) | ||||
class NotebookCopyHandler(IPythonHandler): | ||||
@web.authenticated | ||||
Zachary Sailer
|
r12981 | def get(self, notebook_path=None): | ||
nbm = self.notebook_manager | ||||
name, path = nbm.named_notebook_path(notebook_path) | ||||
notebook_name = self.notebook_manager.copy_notebook(name, path) | ||||
if path==None: | ||||
self.redirect(url_path_join(self.base_project_url, "notebooks", notebook_name)) | ||||
else: | ||||
self.redirect(url_path_join(self.base_project_url, "notebooks", path, notebook_name)) | ||||
Brian E. Granger
|
r10641 | |||
Brian E. Granger
|
r10647 | |||
#----------------------------------------------------------------------------- | ||||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r12981 | _notebook_path_regex = r"(?P<notebook_path>.+)" | ||
Brian E. Granger
|
r10647 | |||
default_handlers = [ | ||||
Zachary Sailer
|
r12981 | (r"/notebooks/%s/new" % _notebook_path_regex, NewPathHandler), | ||
(r"/notebooks/new", NewHandler), | ||||
(r"/notebooks/%s/copy" % _notebook_path_regex, NotebookCopyHandler), | ||||
(r"/notebooks/%s" % _notebook_path_regex, NamedNotebookHandler) | ||||
Brian E. Granger
|
r10647 | ] | ||