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