Show More
@@ -1,79 +1,91 b'' | |||
|
1 | 1 | """Tornado handlers for the live notebook view. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | import os | |
|
19 | 20 | from tornado import web |
|
20 | 21 | HTTPError = web.HTTPError |
|
21 | 22 | |
|
22 | 23 | from ..base.handlers import IPythonHandler |
|
23 | 24 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex |
|
24 | 25 | from ..utils import url_path_join, url_escape |
|
25 | 26 | |
|
26 | 27 | #----------------------------------------------------------------------------- |
|
27 | 28 | # Handlers |
|
28 | 29 | #----------------------------------------------------------------------------- |
|
29 | 30 | |
|
30 | 31 | |
|
31 | 32 | class NotebookHandler(IPythonHandler): |
|
32 | 33 | |
|
33 | 34 | @web.authenticated |
|
34 | 35 | def get(self, path='', name=None): |
|
35 | 36 | """get renders the notebook template if a name is given, or |
|
36 | 37 | redirects to the '/files/' handler if the name is not given.""" |
|
37 | 38 | path = path.strip('/') |
|
38 | 39 | nbm = self.notebook_manager |
|
39 | 40 | if name is None: |
|
40 | 41 | raise web.HTTPError(500, "This shouldn't be accessible: %s" % self.request.uri) |
|
41 | 42 | |
|
42 | 43 | # a .ipynb filename was given |
|
43 | 44 | if not nbm.notebook_exists(name, path): |
|
44 | 45 | raise web.HTTPError(404, u'Notebook does not exist: %s/%s' % (path, name)) |
|
45 | 46 | name = url_escape(name) |
|
46 | 47 | path = url_escape(path) |
|
47 | 48 | self.write(self.render_template('notebook.html', |
|
48 | 49 | project=self.project_dir, |
|
49 | 50 | notebook_path=path, |
|
50 | 51 | notebook_name=name, |
|
51 | 52 | kill_kernel=False, |
|
52 | 53 | mathjax_url=self.mathjax_url, |
|
53 | 54 | ) |
|
54 | 55 | ) |
|
55 | 56 | |
|
56 | 57 | class NotebookRedirectHandler(IPythonHandler): |
|
57 | 58 | def get(self, path=''): |
|
58 | 59 | nbm = self.notebook_manager |
|
59 | 60 | if nbm.path_exists(path): |
|
60 | 61 | # it's a *directory*, redirect to /tree |
|
61 | 62 | url = url_path_join(self.base_project_url, 'tree', path) |
|
62 | 63 | else: |
|
63 | 64 | # otherwise, redirect to /files |
|
64 | # TODO: This should check if it's actually a file | |
|
65 | if '/files/' in path: | |
|
66 | # redirect without files/ iff it would 404 | |
|
67 | # this preserves pre-2.0-style 'files/' links | |
|
68 | # FIXME: this is hardcoded based on notebook_path, | |
|
69 | # but so is the files handler itself, | |
|
70 | # so it should work until both are cleaned up. | |
|
71 | parts = path.split('/') | |
|
72 | files_path = os.path.join(nbm.notebook_dir, *parts) | |
|
73 | self.log.warn("filespath: %s", files_path) | |
|
74 | if not os.path.exists(files_path): | |
|
75 | path = path.replace('/files/', '/', 1) | |
|
76 | ||
|
65 | 77 | url = url_path_join(self.base_project_url, 'files', path) |
|
66 | 78 | url = url_escape(url) |
|
67 | 79 | self.log.debug("Redirecting %s to %s", self.request.path, url) |
|
68 | 80 | self.redirect(url) |
|
69 | 81 | |
|
70 | 82 | #----------------------------------------------------------------------------- |
|
71 | 83 | # URL to handler mappings |
|
72 | 84 | #----------------------------------------------------------------------------- |
|
73 | 85 | |
|
74 | 86 | |
|
75 | 87 | default_handlers = [ |
|
76 | 88 | (r"/notebooks%s" % _notebook_path_regex, NotebookHandler), |
|
77 | 89 | (r"/notebooks%s" % _path_regex, NotebookRedirectHandler), |
|
78 | 90 | ] |
|
79 | 91 |
General Comments 0
You need to be logged in to leave comments.
Login now