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