Show More
|
1 | NO CONTENT: modified file |
@@ -1,108 +1,105 b'' | |||
|
1 | 1 | """Tornado handlers for the notebooks web service. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Zach Sailer |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008-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 | from tornado import web |
|
20 | 20 | |
|
21 | 21 | from zmq.utils import jsonapi |
|
22 | 22 | |
|
23 | 23 | from IPython.utils.jsonutil import date_default |
|
24 | 24 | |
|
25 | 25 | from ...base.handlers import IPythonHandler, authenticate_unless_readonly |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Session web service handlers |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class SessionRootHandler(IPythonHandler): |
|
34 | 34 | |
|
35 | ||
|
36 | 35 | @authenticate_unless_readonly |
|
37 | 36 | def get(self): |
|
38 | 37 | sm = self.session_manager |
|
39 | 38 | nbm = self.notebook_manager |
|
40 | 39 | km = self.kernel_manager |
|
41 | 40 | sessions = sm.list_sessions() |
|
42 | 41 | self.finish(jsonapi.dumps(sessions)) |
|
43 | 42 | |
|
44 | ||
|
45 | 43 | @web.authenticated |
|
46 | 44 | def post(self): |
|
47 | 45 | sm = self.session_manager |
|
48 | 46 | nbm = self.notebook_manager |
|
49 | 47 | km = self.kernel_manager |
|
50 | 48 | notebook_path = self.get_argument('notebook_path', default=None) |
|
51 | 49 | notebook_name, path = nbm.named_notebook_path(notebook_path) |
|
52 | 50 | session_id, model = sm.get_session(notebook_name, path) |
|
53 | 51 | if model == None: |
|
54 | 52 | kernel_id = km.start_kernel() |
|
55 | 53 | kernel = km.kernel_model(kernel_id, self.ws_url) |
|
56 | 54 | model = sm.session_model(session_id, notebook_name, path, kernel) |
|
57 | 55 | self.finish(jsonapi.dumps(model)) |
|
58 | 56 | |
|
59 | ||
|
60 | 57 | class SessionHandler(IPythonHandler): |
|
61 | 58 | |
|
62 | 59 | SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE') |
|
63 | 60 | |
|
64 | 61 | @authenticate_unless_readonly |
|
65 | 62 | def get(self, session_id): |
|
66 | 63 | sm = self.session_manager |
|
67 | 64 | model = sm.get_session_from_id(session_id) |
|
68 | 65 | self.finish(jsonapi.dumps(model)) |
|
69 | 66 | |
|
70 | 67 | @web.authenticated |
|
71 | 68 | def patch(self, session_id): |
|
72 | 69 | sm = self.session_manager |
|
73 | 70 | nbm = self.notebook_manager |
|
74 | 71 | km = self.kernel_manager |
|
75 | 72 | notebook_path = self.request.body |
|
76 | 73 | notebook_name, path = nbm.named_notebook_path(notebook_path) |
|
77 | 74 | kernel_id = sm.get_kernel_from_session(session_id) |
|
78 | 75 | kernel = km.kernel_model(kernel_id, self.ws_url) |
|
79 | 76 | sm.delete_mapping_for_session(session_id) |
|
80 | 77 | model = sm.session_model(session_id, notebook_name, path, kernel) |
|
81 | 78 | self.finish(jsonapi.dumps(model)) |
|
82 | 79 | |
|
83 | 80 | @web.authenticated |
|
84 | 81 | def delete(self, session_id): |
|
85 | 82 | sm = self.session_manager |
|
86 | 83 | nbm = self.notebook_manager |
|
87 | 84 | km = self.kernel_manager |
|
88 | 85 | kernel_id = sm.get_kernel_from_session(session_id) |
|
89 | 86 | km.shutdown_kernel(kernel_id) |
|
90 | 87 | sm.delete_mapping_for_session(session_id) |
|
91 | 88 | self.set_status(204) |
|
92 | 89 | self.finish() |
|
93 | 90 | |
|
94 | 91 | |
|
95 | 92 | #----------------------------------------------------------------------------- |
|
96 | 93 | # URL to handler mappings |
|
97 | 94 | #----------------------------------------------------------------------------- |
|
98 | 95 | |
|
99 | 96 | _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" |
|
100 | 97 | |
|
101 | 98 | default_handlers = [ |
|
102 | 99 | (r"api/sessions/%s" % _session_id_regex, SessionHandler), |
|
103 | 100 | (r"api/sessions", SessionRootHandler) |
|
104 | 101 | ] |
|
105 | 102 | |
|
106 | 103 | |
|
107 | 104 | |
|
108 | 105 |
@@ -1,97 +1,96 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Notebook |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | var IPython = (function (IPython) { |
|
13 | 13 | |
|
14 | 14 | var Session = function(notebook_path, Notebook){ |
|
15 | 15 | this.kernel = null; |
|
16 | 16 | this.kernel_id = null; |
|
17 | 17 | this.session_id = null; |
|
18 | 18 | this.notebook_path = notebook_path; |
|
19 | 19 | this.notebook = Notebook; |
|
20 | 20 | this._baseProjectUrl = Notebook.baseProjectUrl() |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | 23 | Session.prototype.start = function(){ |
|
24 | 24 | var that = this |
|
25 | 25 | var qs = $.param({notebook_path:this.notebook_path}); |
|
26 | 26 | var url = '/api/sessions' + '?' + qs; |
|
27 | 27 | $.post(url, |
|
28 | 28 | $.proxy(this.start_kernel, that), |
|
29 | 29 | 'json' |
|
30 | 30 | ); |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | Session.prototype.notebook_rename = function (notebook_path) { |
|
34 | 34 | this.notebook_path = notebook_path; |
|
35 | console.log("TEST"); | |
|
36 | 35 | var settings = { |
|
37 | 36 | processData : false, |
|
38 | 37 | cache : false, |
|
39 | 38 | type : "PATCH", |
|
40 | 39 | data: notebook_path, |
|
41 | 40 | dataType : "json", |
|
42 | 41 | }; |
|
43 | 42 | var url = this._baseProjectUrl + 'api/sessions/' + this.session_id; |
|
44 | 43 | $.ajax(url, settings); |
|
45 | 44 | } |
|
46 | 45 | |
|
47 | 46 | |
|
48 | 47 | Session.prototype.delete_session = function() { |
|
49 | 48 | var settings = { |
|
50 | 49 | processData : false, |
|
51 | 50 | cache : false, |
|
52 | 51 | type : "DELETE", |
|
53 | 52 | dataType : "json", |
|
54 | 53 | }; |
|
55 | 54 | var url = this._baseProjectUrl + 'api/sessions/' + this.session_id; |
|
56 | 55 | $.ajax(url, settings); |
|
57 | 56 | }; |
|
58 | 57 | |
|
59 | 58 | // Kernel related things |
|
60 | 59 | /** |
|
61 | 60 | * Start a new kernel and set it on each code cell. |
|
62 | 61 | * |
|
63 | 62 | * @method start_kernel |
|
64 | 63 | */ |
|
65 | 64 | Session.prototype.start_kernel = function (json) { |
|
66 | 65 | this.session_id = json.session_id; |
|
67 | 66 | this.kernel_content = json.kernel; |
|
68 | 67 | var base_url = $('body').data('baseKernelUrl') + "api/kernels"; |
|
69 | 68 | this.kernel = new IPython.Kernel(base_url, this.session_id); |
|
70 | 69 | // Now that the kernel has been created, tell the CodeCells about it. |
|
71 | 70 | this.kernel._kernel_started(this.kernel_content); |
|
72 | 71 | }; |
|
73 | 72 | |
|
74 | 73 | /** |
|
75 | 74 | * Prompt the user to restart the IPython kernel. |
|
76 | 75 | * |
|
77 | 76 | * @method restart_kernel |
|
78 | 77 | */ |
|
79 | 78 | Session.prototype.restart_kernel = function () { |
|
80 | 79 | this.kernel.restart(); |
|
81 | 80 | }; |
|
82 | 81 | |
|
83 | 82 | Session.prototype.interrupt_kernel = function() { |
|
84 | 83 | this.kernel.interrupt(); |
|
85 | 84 | }; |
|
86 | 85 | |
|
87 | 86 | |
|
88 | 87 | Session.prototype.kill_kernel = function() { |
|
89 | 88 | this.kernel.kill(); |
|
90 | 89 | }; |
|
91 | 90 | |
|
92 | 91 | IPython.Session = Session; |
|
93 | 92 | |
|
94 | 93 | |
|
95 | 94 | return IPython; |
|
96 | 95 | |
|
97 | 96 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now