Show More
@@ -1,187 +1,187 b'' | |||
|
1 | 1 | """Tornado handlers for the notebook. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
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 | import logging |
|
20 | 20 | from tornado import web |
|
21 | 21 | |
|
22 | 22 | from zmq.utils import jsonapi |
|
23 | 23 | |
|
24 | 24 | from IPython.utils.jsonutil import date_default |
|
25 | 25 | |
|
26 | 26 | from ...base.handlers import IPythonHandler |
|
27 | 27 | from ...base.zmqhandlers import AuthenticatedZMQStreamHandler |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Kernel handlers |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | class MainKernelHandler(IPythonHandler): |
|
35 | 35 | |
|
36 | 36 | @web.authenticated |
|
37 | 37 | def get(self): |
|
38 | 38 | km = self.kernel_manager |
|
39 | 39 | self.finish(jsonapi.dumps(km.list_kernels())) |
|
40 | 40 | |
|
41 | 41 | @web.authenticated |
|
42 | 42 | def post(self): |
|
43 | 43 | km = self.kernel_manager |
|
44 | 44 | nbm = self.notebook_manager |
|
45 | 45 | kernel_id = km.start_kernel(cwd=nbm.notebook_dir) |
|
46 | 46 | model = km.kernel_model(kernel_id, self.ws_url) |
|
47 | 47 | self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) |
|
48 | 48 | self.finish(jsonapi.dumps(model)) |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | class KernelHandler(IPythonHandler): |
|
52 | 52 | |
|
53 | 53 | SUPPORTED_METHODS = ('DELETE', 'GET') |
|
54 | 54 | |
|
55 | 55 | @web.authenticated |
|
56 | 56 | def get(self, kernel_id): |
|
57 | 57 | km = self.kernel_manager |
|
58 | model = km.kernel_model(kernel_id,self.ws_url) | |
|
58 | model = km.kernel_model(kernel_id, self.ws_url) | |
|
59 | 59 | self.finish(jsonapi.dumps(model)) |
|
60 | 60 | |
|
61 | 61 | @web.authenticated |
|
62 | 62 | def delete(self, kernel_id): |
|
63 | 63 | km = self.kernel_manager |
|
64 | 64 | km.shutdown_kernel(kernel_id) |
|
65 | 65 | self.set_status(204) |
|
66 | 66 | self.finish() |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | class KernelActionHandler(IPythonHandler): |
|
70 | 70 | |
|
71 | 71 | @web.authenticated |
|
72 | 72 | def post(self, kernel_id, action): |
|
73 | 73 | km = self.kernel_manager |
|
74 | 74 | if action == 'interrupt': |
|
75 | 75 | km.interrupt_kernel(kernel_id) |
|
76 | 76 | self.set_status(204) |
|
77 | 77 | if action == 'restart': |
|
78 | 78 | km.restart_kernel(kernel_id) |
|
79 | model = km.kernel_model(kernel_id,self.ws_url) | |
|
79 | model = km.kernel_model(kernel_id, self.ws_url) | |
|
80 | 80 | self.set_header('Location', '{0}api/kernels/{1}'.format(self.base_kernel_url, kernel_id)) |
|
81 | 81 | self.write(jsonapi.dumps(model)) |
|
82 | 82 | self.finish() |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | class ZMQChannelHandler(AuthenticatedZMQStreamHandler): |
|
86 | 86 | |
|
87 | 87 | def create_stream(self): |
|
88 | 88 | km = self.kernel_manager |
|
89 | 89 | meth = getattr(km, 'connect_%s' % self.channel) |
|
90 | 90 | self.zmq_stream = meth(self.kernel_id, identity=self.session.bsession) |
|
91 | 91 | |
|
92 | 92 | def initialize(self, *args, **kwargs): |
|
93 | 93 | self.zmq_stream = None |
|
94 | 94 | |
|
95 | 95 | def on_first_message(self, msg): |
|
96 | 96 | try: |
|
97 | 97 | super(ZMQChannelHandler, self).on_first_message(msg) |
|
98 | 98 | except web.HTTPError: |
|
99 | 99 | self.close() |
|
100 | 100 | return |
|
101 | 101 | try: |
|
102 | 102 | self.create_stream() |
|
103 | 103 | except web.HTTPError: |
|
104 | 104 | # WebSockets don't response to traditional error codes so we |
|
105 | 105 | # close the connection. |
|
106 | 106 | if not self.stream.closed(): |
|
107 | 107 | self.stream.close() |
|
108 | 108 | self.close() |
|
109 | 109 | else: |
|
110 | 110 | self.zmq_stream.on_recv(self._on_zmq_reply) |
|
111 | 111 | |
|
112 | 112 | def on_message(self, msg): |
|
113 | 113 | msg = jsonapi.loads(msg) |
|
114 | 114 | self.session.send(self.zmq_stream, msg) |
|
115 | 115 | |
|
116 | 116 | def on_close(self): |
|
117 | 117 | # This method can be called twice, once by self.kernel_died and once |
|
118 | 118 | # from the WebSocket close event. If the WebSocket connection is |
|
119 | 119 | # closed before the ZMQ streams are setup, they could be None. |
|
120 | 120 | if self.zmq_stream is not None and not self.zmq_stream.closed(): |
|
121 | 121 | self.zmq_stream.on_recv(None) |
|
122 | 122 | self.zmq_stream.close() |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | class IOPubHandler(ZMQChannelHandler): |
|
126 | 126 | channel = 'iopub' |
|
127 | 127 | |
|
128 | 128 | def create_stream(self): |
|
129 | 129 | super(IOPubHandler, self).create_stream() |
|
130 | 130 | km = self.kernel_manager |
|
131 | 131 | km.add_restart_callback(self.kernel_id, self.on_kernel_restarted) |
|
132 | 132 | km.add_restart_callback(self.kernel_id, self.on_restart_failed, 'dead') |
|
133 | 133 | |
|
134 | 134 | def on_close(self): |
|
135 | 135 | km = self.kernel_manager |
|
136 | 136 | if self.kernel_id in km: |
|
137 | 137 | km.remove_restart_callback( |
|
138 | 138 | self.kernel_id, self.on_kernel_restarted, |
|
139 | 139 | ) |
|
140 | 140 | km.remove_restart_callback( |
|
141 | 141 | self.kernel_id, self.on_restart_failed, 'dead', |
|
142 | 142 | ) |
|
143 | 143 | super(IOPubHandler, self).on_close() |
|
144 | 144 | |
|
145 | 145 | def _send_status_message(self, status): |
|
146 | 146 | msg = self.session.msg("status", |
|
147 | 147 | {'execution_state': status} |
|
148 | 148 | ) |
|
149 | 149 | self.write_message(jsonapi.dumps(msg, default=date_default)) |
|
150 | 150 | |
|
151 | 151 | def on_kernel_restarted(self): |
|
152 | 152 | logging.warn("kernel %s restarted", self.kernel_id) |
|
153 | 153 | self._send_status_message('restarting') |
|
154 | 154 | |
|
155 | 155 | def on_restart_failed(self): |
|
156 | 156 | logging.error("kernel %s restarted failed!", self.kernel_id) |
|
157 | 157 | self._send_status_message('dead') |
|
158 | 158 | |
|
159 | 159 | def on_message(self, msg): |
|
160 | 160 | """IOPub messages make no sense""" |
|
161 | 161 | pass |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | class ShellHandler(ZMQChannelHandler): |
|
165 | 165 | channel = 'shell' |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | class StdinHandler(ZMQChannelHandler): |
|
169 | 169 | channel = 'stdin' |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | #----------------------------------------------------------------------------- |
|
173 | 173 | # URL to handler mappings |
|
174 | 174 | #----------------------------------------------------------------------------- |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)" |
|
178 | 178 | _kernel_action_regex = r"(?P<action>restart|interrupt)" |
|
179 | 179 | |
|
180 | 180 | default_handlers = [ |
|
181 | 181 | (r"/api/kernels", MainKernelHandler), |
|
182 | 182 | (r"/api/kernels/%s" % _kernel_id_regex, KernelHandler), |
|
183 | 183 | (r"/api/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler), |
|
184 | 184 | (r"/api/kernels/%s/iopub" % _kernel_id_regex, IOPubHandler), |
|
185 | 185 | (r"/api/kernels/%s/shell" % _kernel_id_regex, ShellHandler), |
|
186 | 186 | (r"/api/kernels/%s/stdin" % _kernel_id_regex, StdinHandler) |
|
187 | 187 | ] |
@@ -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 | ||
|
35 | ||
|
34 | ||
|
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 | ||
|
44 | ||
|
42 | ||
|
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 | ||
|
59 | ||
|
56 | ||
|
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