Show More
@@ -1,81 +1,83 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 | 19 | import os |
|
20 | 20 | import json |
|
21 | 21 | |
|
22 | 22 | from tornado import web |
|
23 | 23 | HTTPError = web.HTTPError |
|
24 | 24 | |
|
25 | 25 | from ..base.handlers import IPythonHandler |
|
26 | 26 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex |
|
27 | 27 | from ..utils import url_path_join, url_escape, url_unescape |
|
28 | 28 | from urllib import quote |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | # Handlers |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class NotebookHandler(IPythonHandler): |
|
36 | 36 | |
|
37 | 37 | @web.authenticated |
|
38 | 38 | def get(self, path='', name=None): |
|
39 | 39 | """get renders the notebook template if a name is given, or |
|
40 | 40 | redirects to the '/files/' handler if the name is not given.""" |
|
41 | 41 | path = path.strip('/') |
|
42 | 42 | nbm = self.notebook_manager |
|
43 | 43 | if name is None: |
|
44 | 44 | raise web.HTTPError(500, "This shouldn't be accessible: %s" % self.request.uri) |
|
45 | 45 | |
|
46 | 46 | # a .ipynb filename was given |
|
47 | 47 | if not nbm.notebook_exists(name, path): |
|
48 | 48 | raise web.HTTPError(404, u'Notebook does not exist: %s/%s' % (path, name)) |
|
49 | 49 | name = url_escape(name) |
|
50 | 50 | path = url_escape(path) |
|
51 | 51 | self.write(self.render_template('notebook.html', |
|
52 | 52 | project=self.project_dir, |
|
53 | 53 | notebook_path=path, |
|
54 | 54 | notebook_name=name, |
|
55 | 55 | kill_kernel=False, |
|
56 | 56 | mathjax_url=self.mathjax_url, |
|
57 | 57 | ) |
|
58 | 58 | ) |
|
59 | 59 | |
|
60 | 60 | class NotebookRedirectHandler(IPythonHandler): |
|
61 | 61 | def get(self, path=''): |
|
62 | 62 | nbm = self.notebook_manager |
|
63 | 63 | if nbm.path_exists(path): |
|
64 | 64 | # it's a *directory*, redirect to /tree |
|
65 | 65 | url = url_path_join(self.base_project_url, 'tree', path) |
|
66 | 66 | else: |
|
67 | 67 | # otherwise, redirect to /files |
|
68 | 68 | # TODO: This should check if it's actually a file |
|
69 | 69 | url = url_path_join(self.base_project_url, 'files', path) |
|
70 | url = url_escape(url) | |
|
71 | self.log.debug("Redirecting %s to %s", self.request.path, url) | |
|
70 | 72 | self.redirect(url) |
|
71 | 73 | |
|
72 | 74 | #----------------------------------------------------------------------------- |
|
73 | 75 | # URL to handler mappings |
|
74 | 76 | #----------------------------------------------------------------------------- |
|
75 | 77 | |
|
76 | 78 | |
|
77 | 79 | default_handlers = [ |
|
78 | 80 | (r"/notebooks%s" % _notebook_path_regex, NotebookHandler), |
|
79 | 81 | (r"/notebooks%s" % _path_regex, NotebookRedirectHandler), |
|
80 | 82 | ] |
|
81 | 83 |
@@ -1,72 +1,77 b'' | |||
|
1 | 1 | """Tornado handlers for the tree 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 | import os |
|
19 | 19 | |
|
20 | 20 | from tornado import web |
|
21 | 21 | from ..base.handlers import IPythonHandler |
|
22 | from ..utils import url_path_join, path2url, url2path | |
|
22 | from ..utils import url_path_join, path2url, url2path, url_escape | |
|
23 | 23 | from ..services.notebooks.handlers import _notebook_path_regex, _path_regex |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Handlers |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | class TreeHandler(IPythonHandler): |
|
31 | 31 | """Render the tree view, listing notebooks, clusters, etc.""" |
|
32 | 32 | |
|
33 | 33 | @web.authenticated |
|
34 | 34 | def get(self, path='', name=None): |
|
35 | 35 | path = path.strip('/') |
|
36 | 36 | nbm = self.notebook_manager |
|
37 | 37 | if name is not None: |
|
38 | 38 | # is a notebook, redirect to notebook handler |
|
39 | url = url_path_join(self.base_project_url, 'notebooks', path, name) | |
|
39 | url = url_escape(url_path_join( | |
|
40 | self.base_project_url, 'notebooks', path, name | |
|
41 | )) | |
|
42 | self.log.debug("Redirecting %s to %s", self.request.path, url) | |
|
40 | 43 | self.redirect(url) |
|
41 | 44 | else: |
|
42 | 45 | if not nbm.path_exists(path=path): |
|
43 | 46 | # no such directory, 404 |
|
44 | 47 | raise web.HTTPError(404) |
|
45 | 48 | self.write(self.render_template('tree.html', |
|
46 | 49 | project=self.project_dir, |
|
47 | 50 | tree_url_path=path, |
|
48 | 51 | notebook_path=path, |
|
49 | 52 | )) |
|
50 | 53 | |
|
51 | 54 | |
|
52 | 55 | class TreeRedirectHandler(IPythonHandler): |
|
53 | 56 | """Redirect a request to the corresponding tree URL""" |
|
54 | 57 | |
|
55 | 58 | @web.authenticated |
|
56 | 59 | def get(self, path=''): |
|
57 | url = url_path_join(self.base_project_url, 'tree', path).rstrip('/') | |
|
58 | self.log.debug("Redirecting %s to %s", self.request.uri, url) | |
|
60 | url = url_escape(url_path_join( | |
|
61 | self.base_project_url, 'tree', path.strip('/') | |
|
62 | )) | |
|
63 | self.log.debug("Redirecting %s to %s", self.request.path, url) | |
|
59 | 64 | self.redirect(url) |
|
60 | 65 | |
|
61 | 66 | |
|
62 | 67 | #----------------------------------------------------------------------------- |
|
63 | 68 | # URL to handler mappings |
|
64 | 69 | #----------------------------------------------------------------------------- |
|
65 | 70 | |
|
66 | 71 | |
|
67 | 72 | default_handlers = [ |
|
68 | 73 | (r"/tree%s" % _notebook_path_regex, TreeHandler), |
|
69 | 74 | (r"/tree%s" % _path_regex, TreeHandler), |
|
70 | 75 | (r"/tree", TreeHandler), |
|
71 | 76 | (r"/", TreeRedirectHandler), |
|
72 | 77 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now