Show More
@@ -1,780 +1,950 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'services/kernels/js/comm', |
|
9 | 9 | 'widgets/js/init', |
|
10 | 10 | ], function(IPython, $, utils, comm, widgetmanager) { |
|
11 | 11 | "use strict"; |
|
12 | 12 | |
|
13 | // Initialization and connection. | |
|
14 | 13 | /** |
|
15 |
* A Kernel |
|
|
16 | * @Class Kernel | |
|
14 | * A Kernel class to communicate with the Python kernel. This | |
|
15 | * should generally not be constructed directly, but be created | |
|
16 | * by. the `Session` object. Once created, this object should be | |
|
17 | * used to communicate with the kernel. | |
|
18 | * | |
|
19 | * @class Kernel | |
|
20 | * @param {string} kernel_service_url - the URL to access the kernel REST api | |
|
21 | * @param {string} ws_url - the websockets URL | |
|
22 | * @param {Notebook} notebook - notebook object | |
|
23 | * @param {string} id - the kernel id | |
|
24 | * @param {string} name - the kernel type (e.g. python3) | |
|
17 | 25 | */ |
|
18 | 26 | var Kernel = function (kernel_service_url, ws_url, notebook, id, name) { |
|
19 | 27 | this.events = notebook.events; |
|
20 | 28 | |
|
21 | 29 | this.id = id; |
|
22 | 30 | this.name = name; |
|
23 | 31 | |
|
24 | 32 | this.channels = { |
|
25 | 33 | 'shell': null, |
|
26 | 34 | 'iopub': null, |
|
27 | 35 | 'stdin': null |
|
28 | 36 | }; |
|
29 | 37 | |
|
30 | 38 | this.kernel_service_url = kernel_service_url; |
|
31 | 39 | this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id); |
|
32 | 40 | this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl"); |
|
33 | 41 | if (!this.ws_url) { |
|
34 | 42 | // trailing 's' in https will become wss for secure web sockets |
|
35 | 43 | this.ws_url = location.protocol.replace('http', 'ws') + "//" + location.host; |
|
36 | 44 | } |
|
37 | 45 | |
|
38 | 46 | this.username = "username"; |
|
39 | 47 | this.session_id = utils.uuid(); |
|
40 | 48 | this._msg_callbacks = {}; |
|
41 | 49 | |
|
42 | 50 | if (typeof(WebSocket) !== 'undefined') { |
|
43 | 51 | this.WebSocket = WebSocket; |
|
44 | 52 | } else if (typeof(MozWebSocket) !== 'undefined') { |
|
45 | 53 | this.WebSocket = MozWebSocket; |
|
46 | 54 | } else { |
|
47 | 55 | alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox ≥ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.'); |
|
48 | 56 | } |
|
49 | 57 | |
|
50 | 58 | this.bind_events(); |
|
51 | 59 | this.init_iopub_handlers(); |
|
52 | 60 | this.comm_manager = new comm.CommManager(this); |
|
53 | 61 | this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook); |
|
54 | 62 | |
|
55 | 63 | this.last_msg_id = null; |
|
56 | 64 | this.last_msg_callbacks = {}; |
|
57 | 65 | }; |
|
58 | 66 | |
|
59 | ||
|
67 | /** | |
|
68 | * @function _get_msg | |
|
69 | */ | |
|
60 | 70 | Kernel.prototype._get_msg = function (msg_type, content, metadata) { |
|
61 | 71 | var msg = { |
|
62 | 72 | header : { |
|
63 | 73 | msg_id : utils.uuid(), |
|
64 | 74 | username : this.username, |
|
65 | 75 | session : this.session_id, |
|
66 | 76 | msg_type : msg_type, |
|
67 | 77 | version : "5.0" |
|
68 | 78 | }, |
|
69 | 79 | metadata : metadata || {}, |
|
70 | 80 | content : content, |
|
71 | 81 | parent_header : {} |
|
72 | 82 | }; |
|
73 | 83 | return msg; |
|
74 | 84 | }; |
|
75 | 85 | |
|
86 | /** | |
|
87 | * @function bind_events | |
|
88 | */ | |
|
76 | 89 | Kernel.prototype.bind_events = function () { |
|
77 | 90 | var that = this; |
|
78 | 91 | this.events.on('send_input_reply.Kernel', function(evt, data) { |
|
79 | 92 | that.send_input_reply(data); |
|
80 | 93 | }); |
|
81 | 94 | }; |
|
82 | 95 | |
|
83 | // Initialize the iopub handlers | |
|
84 | ||
|
96 | /** | |
|
97 | * Initialize the iopub handlers. | |
|
98 | * | |
|
99 | * @function init_iopub_handlers | |
|
100 | */ | |
|
85 | 101 | Kernel.prototype.init_iopub_handlers = function () { |
|
86 | 102 | var output_msg_types = ['stream', 'display_data', 'execute_result', 'error']; |
|
87 | 103 | this._iopub_handlers = {}; |
|
88 | 104 | this.register_iopub_handler('status', $.proxy(this._handle_status_message, this)); |
|
89 | 105 | this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this)); |
|
90 | 106 | |
|
91 | 107 | for (var i=0; i < output_msg_types.length; i++) { |
|
92 | 108 | this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this)); |
|
93 | 109 | } |
|
94 | 110 | }; |
|
95 | 111 | |
|
96 | 112 | /** |
|
97 | 113 | * GET /api/kernels |
|
114 | * | |
|
115 | * Get the list of running kernels. | |
|
116 | * | |
|
117 | * @function list | |
|
118 | * @param {function} [success] - function executed on ajax success | |
|
119 | * @param {function} [error] - functon executed on ajax error | |
|
98 | 120 | */ |
|
99 | 121 | Kernel.prototype.list = function (success, error) { |
|
100 | 122 | $.ajax(this.kernel_service_url, { |
|
101 | 123 | processData: false, |
|
102 | 124 | cache: false, |
|
103 | 125 | type: "GET", |
|
104 | 126 | dataType: "json", |
|
105 | 127 | success: success, |
|
106 | 128 | error: this._on_error(error) |
|
107 | 129 | }); |
|
108 | 130 | }; |
|
109 | 131 | |
|
110 | 132 | /** |
|
111 | 133 | * POST /api/kernels |
|
112 | 134 | * |
|
135 | * Start a new kernel. | |
|
136 | * | |
|
113 | 137 | * In general this shouldn't be used -- the kernel should be |
|
114 | 138 | * started through the session API. If you use this function and |
|
115 | 139 | * are also using the session API then your session and kernel |
|
116 | 140 | * WILL be out of sync! |
|
141 | * | |
|
142 | * @function start | |
|
143 | * @param {params} [Object] - parameters to include in the query string | |
|
144 | * @param {function} [success] - function executed on ajax success | |
|
145 | * @param {function} [error] - functon executed on ajax error | |
|
117 | 146 | */ |
|
118 | 147 | Kernel.prototype.start = function (params, success, error) { |
|
119 | 148 | var url = this.kernel_service_url; |
|
120 | 149 | var qs = $.param(params || {}); // query string for sage math stuff |
|
121 | 150 | if (qs !== "") { |
|
122 | 151 | url = url + "?" + qs; |
|
123 | 152 | } |
|
124 | 153 | |
|
125 | 154 | var that = this; |
|
126 | 155 | var on_success = function (data, status, xhr) { |
|
127 | 156 | that.id = data.id; |
|
128 | 157 | that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id); |
|
129 |
that._kernel_started( |
|
|
158 | that._kernel_started(); | |
|
130 | 159 | if (success) { |
|
131 | 160 | success(data, status, xhr); |
|
132 | 161 | } |
|
133 | 162 | }; |
|
134 | 163 | |
|
135 | 164 | $.ajax(url, { |
|
136 | 165 | processData: false, |
|
137 | 166 | cache: false, |
|
138 | 167 | type: "POST", |
|
139 | 168 | data: JSON.stringify({name: this.name}), |
|
140 | 169 | dataType: "json", |
|
141 | 170 | success: this._on_success(on_success), |
|
142 | 171 | error: this._on_error(error) |
|
143 | 172 | }); |
|
144 | 173 | |
|
145 | 174 | return url; |
|
146 | 175 | }; |
|
147 | 176 | |
|
148 | 177 | /** |
|
149 | 178 | * GET /api/kernels/[:kernel_id] |
|
179 | * | |
|
180 | * Get information about the kernel. | |
|
181 | * | |
|
182 | * @function get_info | |
|
183 | * @param {function} [success] - function executed on ajax success | |
|
184 | * @param {function} [error] - functon executed on ajax error | |
|
150 | 185 | */ |
|
151 | 186 | Kernel.prototype.get_info = function (success, error) { |
|
152 | 187 | $.ajax(this.kernel_url, { |
|
153 | 188 | processData: false, |
|
154 | 189 | cache: false, |
|
155 | 190 | type: "GET", |
|
156 | 191 | dataType: "json", |
|
157 | 192 | success: this._on_success(success), |
|
158 | 193 | error: this._on_error(error) |
|
159 | 194 | }); |
|
160 | 195 | }; |
|
161 | 196 | |
|
162 | 197 | /** |
|
163 | 198 | * DELETE /api/kernels/[:kernel_id] |
|
199 | * | |
|
200 | * Shutdown the kernel. | |
|
201 | * | |
|
202 | * If you are also using sessions, then this function shoul NOT be | |
|
203 | * used. Instead, use Session.delete. Otherwise, the session and | |
|
204 | * kernel WILL be out of sync. | |
|
205 | * | |
|
206 | * @function kill | |
|
207 | * @param {function} [success] - function executed on ajax success | |
|
208 | * @param {function} [error] - functon executed on ajax error | |
|
164 | 209 | */ |
|
165 | 210 | Kernel.prototype.kill = function (success, error) { |
|
166 | 211 | this._kernel_dead(); |
|
167 | 212 | $.ajax(this.kernel_url, { |
|
168 | 213 | processData: false, |
|
169 | 214 | cache: false, |
|
170 | 215 | type: "DELETE", |
|
171 | 216 | dataType: "json", |
|
172 | 217 | success: this._on_success(success), |
|
173 | 218 | error: this._on_error(error) |
|
174 | 219 | }); |
|
175 | 220 | }; |
|
176 | 221 | |
|
177 | 222 | /** |
|
178 | 223 | * POST /api/kernels/[:kernel_id]/interrupt |
|
224 | * | |
|
225 | * Interrupt the kernel. | |
|
226 | * | |
|
227 | * @function interrupt | |
|
228 | * @param {function} [success] - function executed on ajax success | |
|
229 | * @param {function} [error] - functon executed on ajax error | |
|
179 | 230 | */ |
|
180 | 231 | Kernel.prototype.interrupt = function (success, error) { |
|
181 | 232 | this.events.trigger('status_interrupting.Kernel', {kernel: this}); |
|
182 | 233 | var url = utils.url_join_encode(this.kernel_url, 'interrupt'); |
|
183 | 234 | $.ajax(url, { |
|
184 | 235 | processData: false, |
|
185 | 236 | cache: false, |
|
186 | 237 | type: "POST", |
|
187 | 238 | dataType: "json", |
|
188 | 239 | success: this._on_success(success), |
|
189 | 240 | error: this._on_error(error) |
|
190 | 241 | }); |
|
191 | 242 | }; |
|
192 | 243 | |
|
193 | 244 | /** |
|
194 | 245 | * POST /api/kernels/[:kernel_id]/restart |
|
246 | * | |
|
247 | * Restart the kernel. | |
|
248 | * | |
|
249 | * @function interrupt | |
|
250 | * @param {function} [success] - function executed on ajax success | |
|
251 | * @param {function} [error] - functon executed on ajax error | |
|
195 | 252 | */ |
|
196 | 253 | Kernel.prototype.restart = function (success, error) { |
|
197 | 254 | this.events.trigger('status_restarting.Kernel', {kernel: this}); |
|
198 | 255 | this.stop_channels(); |
|
199 | 256 | |
|
200 | 257 | var that = this; |
|
201 | 258 | var on_success = function (data, status, xhr) { |
|
202 |
that._kernel_started( |
|
|
259 | that._kernel_started(); | |
|
203 | 260 | if (success) { |
|
204 | 261 | success(data, status, xhr); |
|
205 | 262 | } |
|
206 | 263 | }; |
|
207 | 264 | |
|
208 | 265 | var url = utils.url_join_encode(this.kernel_url, 'restart'); |
|
209 | 266 | $.ajax(url, { |
|
210 | 267 | processData: false, |
|
211 | 268 | cache: false, |
|
212 | 269 | type: "POST", |
|
213 | 270 | dataType: "json", |
|
214 | 271 | success: this._on_success(on_success), |
|
215 | 272 | error: this._on_error(error) |
|
216 | 273 | }); |
|
217 | 274 | }; |
|
218 | 275 | |
|
219 | 276 | /** |
|
220 | * Not actually a HTTP request, but useful function nonetheless | |
|
221 | * for reconnecting to the kernel if the connection is somehow lost | |
|
277 | * Reconnect to a disconnected kernel. This is not actually a | |
|
278 | * standard HTTP request, but useful function nonetheless for | |
|
279 | * reconnecting to the kernel if the connection is somehow lost. | |
|
280 | * | |
|
281 | * @function reconnect | |
|
222 | 282 | */ |
|
223 | 283 | Kernel.prototype.reconnect = function () { |
|
224 | 284 | this.events.trigger('status_reconnecting.Kernel'); |
|
225 | 285 | var that = this; |
|
226 | 286 | setTimeout(function () { |
|
227 | 287 | that.start_channels(); |
|
228 | 288 | }, 5000); |
|
229 | 289 | }; |
|
230 | 290 | |
|
291 | /** | |
|
292 | * Handle a successful AJAX request by updating the kernel id and | |
|
293 | * name from the response, and then optionally calling a provided | |
|
294 | * callback. | |
|
295 | * | |
|
296 | * @function _on_success | |
|
297 | * @param {function} success - callback | |
|
298 | */ | |
|
231 | 299 | Kernel.prototype._on_success = function (success) { |
|
232 | 300 | var that = this; |
|
233 | 301 | return function (data, status, xhr) { |
|
234 | 302 | if (data) { |
|
235 | 303 | that.id = data.id; |
|
236 | 304 | that.name = data.name; |
|
237 | 305 | } |
|
238 | 306 | that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id); |
|
239 | 307 | if (success) { |
|
240 | 308 | success(data, status, xhr); |
|
241 | 309 | } |
|
242 | 310 | }; |
|
243 | 311 | }; |
|
244 | 312 | |
|
313 | /** | |
|
314 | * Handle a failed AJAX request by logging the error message, and | |
|
315 | * then optionally calling a provided callback. | |
|
316 | * | |
|
317 | * @function _on_error | |
|
318 | * @param {function} error - callback | |
|
319 | */ | |
|
245 | 320 | Kernel.prototype._on_error = function (error) { |
|
246 | 321 | return function (xhr, status, err) { |
|
247 | 322 | utils.log_ajax_error(xhr, status, err); |
|
248 | 323 | if (error) { |
|
249 | 324 | error(xhr, status, err); |
|
250 | 325 | } |
|
251 | 326 | }; |
|
252 | 327 | }; |
|
253 | 328 | |
|
254 | Kernel.prototype._kernel_started = function (json) { | |
|
255 | console.log("Kernel started: ", json.id); | |
|
329 | /** | |
|
330 | * Perform necessary tasks once the kernel has been started. This | |
|
331 | * includes triggering the 'status_started.Kernel' event and | |
|
332 | * then actually connecting to the kernel. | |
|
333 | * | |
|
334 | * @function _kernel_started | |
|
335 | */ | |
|
336 | Kernel.prototype._kernel_started = function () { | |
|
337 | console.log("Kernel started: ", this.id); | |
|
256 | 338 | this.events.trigger('status_started.Kernel', {kernel: this}); |
|
257 | 339 | this.start_channels(); |
|
258 | 340 | }; |
|
259 | 341 | |
|
342 | /** | |
|
343 | * Perform necessary tasks once the connection to the kernel has | |
|
344 | * been established. This includes triggering the | |
|
345 | * 'status_connected.Kernel' event and then requesting information | |
|
346 | * about the kernel. | |
|
347 | * | |
|
348 | * @function _kernel_connected | |
|
349 | */ | |
|
260 | 350 | Kernel.prototype._kernel_connected = function () { |
|
261 | 351 | var that = this; |
|
262 | 352 | console.log('Connected to kernel: ', this.id); |
|
263 | 353 | this.events.trigger('status_connected.Kernel'); |
|
264 | 354 | this.kernel_info(function () { |
|
265 | 355 | that.events.trigger('status_idle.Kernel'); |
|
266 | 356 | }); |
|
267 | 357 | }; |
|
268 | 358 | |
|
359 | /** | |
|
360 | * Perform necessary tasks after the kernel has died. This | |
|
361 | * includes triggering both 'status_dead.Kernel' and | |
|
362 | * 'no_kernel.Kernel', and then closing communication channels to | |
|
363 | * the kernel if they are still somehow open. | |
|
364 | * | |
|
365 | * @function _kernel_dead | |
|
366 | */ | |
|
269 | 367 | Kernel.prototype._kernel_dead = function () { |
|
270 | 368 | this.events.trigger('status_dead.Kernel'); |
|
271 | 369 | this.events.trigger('no_kernel.Kernel'); |
|
272 | 370 | this.stop_channels(); |
|
273 | 371 | }; |
|
274 | 372 | |
|
275 | ||
|
276 | 373 | /** |
|
277 | 374 | * Start the `shell`and `iopub` channels. |
|
278 | 375 | * Will stop and restart them if they already exist. |
|
279 | 376 | * |
|
280 |
* @ |
|
|
377 | * @function start_channels | |
|
281 | 378 | */ |
|
282 | 379 | Kernel.prototype.start_channels = function () { |
|
283 | 380 | var that = this; |
|
284 | 381 | this.stop_channels(); |
|
285 | 382 | var ws_host_url = this.ws_url + this.kernel_url; |
|
286 | 383 | console.log("Starting WebSockets:", ws_host_url); |
|
287 | 384 | this.channels.shell = new this.WebSocket( |
|
288 | 385 | this.ws_url + utils.url_join_encode(this.kernel_url, "shell") |
|
289 | 386 | ); |
|
290 | 387 | this.channels.stdin = new this.WebSocket( |
|
291 | 388 | this.ws_url + utils.url_join_encode(this.kernel_url, "stdin") |
|
292 | 389 | ); |
|
293 | 390 | this.channels.iopub = new this.WebSocket( |
|
294 | 391 | this.ws_url + utils.url_join_encode(this.kernel_url, "iopub") |
|
295 | 392 | ); |
|
296 | 393 | |
|
297 | 394 | var already_called_onclose = false; // only alert once |
|
298 | 395 | var ws_closed_early = function(evt){ |
|
299 | 396 | if (already_called_onclose){ |
|
300 | 397 | return; |
|
301 | 398 | } |
|
302 | 399 | already_called_onclose = true; |
|
303 | 400 | if ( ! evt.wasClean ){ |
|
304 | 401 | that._ws_closed(ws_host_url, true); |
|
305 | 402 | } |
|
306 | 403 | }; |
|
307 | 404 | var ws_closed_late = function(evt){ |
|
308 | 405 | if (already_called_onclose){ |
|
309 | 406 | return; |
|
310 | 407 | } |
|
311 | 408 | already_called_onclose = true; |
|
312 | 409 | if ( ! evt.wasClean ){ |
|
313 | 410 | that._ws_closed(ws_host_url, false); |
|
314 | 411 | } |
|
315 | 412 | }; |
|
316 | 413 | var ws_error = function(evt){ |
|
317 | 414 | if (already_called_onclose){ |
|
318 | 415 | return; |
|
319 | 416 | } |
|
320 | 417 | already_called_onclose = true; |
|
321 | 418 | that._ws_closed(ws_host_url, false); |
|
322 | 419 | }; |
|
323 | 420 | |
|
324 | 421 | for (var c in this.channels) { |
|
325 | 422 | this.channels[c].onopen = $.proxy(this._ws_opened, this); |
|
326 | 423 | this.channels[c].onclose = ws_closed_early; |
|
327 | 424 | this.channels[c].onerror = ws_error; |
|
328 | 425 | } |
|
329 | 426 | // switch from early-close to late-close message after 1s |
|
330 | 427 | setTimeout(function() { |
|
331 | 428 | for (var c in that.channels) { |
|
332 | 429 | if (that.channels[c] !== null) { |
|
333 | 430 | that.channels[c].onclose = ws_closed_late; |
|
334 | 431 | } |
|
335 | 432 | } |
|
336 | 433 | }, 1000); |
|
337 | 434 | this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this); |
|
338 | 435 | this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this); |
|
339 | 436 | this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this); |
|
340 | 437 | }; |
|
341 | 438 | |
|
342 | 439 | /** |
|
343 | * Handle a websocket entering the open state | |
|
344 |
* |
|
|
345 | * Once all sockets are open, signal the Kernel.status_started event. | |
|
346 |
* @ |
|
|
440 | * Handle a websocket entering the open state sends session and | |
|
441 | * cookie authentication info as first message. | |
|
442 | * | |
|
443 | * @function _ws_opened | |
|
347 | 444 | */ |
|
348 | 445 | Kernel.prototype._ws_opened = function (evt) { |
|
349 | 446 | // send the session id so the Session object Python-side |
|
350 | 447 | // has the same identity |
|
351 | 448 | evt.target.send(this.session_id + ':' + document.cookie); |
|
352 | 449 | |
|
353 | 450 | if (this.is_connected()) { |
|
354 | 451 | // all events ready, trigger started event. |
|
355 | 452 | this._kernel_connected(); |
|
356 | 453 | } |
|
357 | 454 | }; |
|
358 | 455 | |
|
456 | /** | |
|
457 | * Handle a websocket entering the closed state. This closes the | |
|
458 | * other communication channels if they are open, and triggers the | |
|
459 | * 'status_disconnected.Kernel' event. If the websocket was closed | |
|
460 | * early, then also trigger 'early_disconnect.Kernel'. Otherwise, | |
|
461 | * try to reconnect to the kernel. | |
|
462 | * | |
|
463 | * @function _ws_closed | |
|
464 | * @param {string} ws_url - the websocket url | |
|
465 | * @param {bool} early - whether the connection was closed early or not | |
|
466 | */ | |
|
359 | 467 | Kernel.prototype._ws_closed = function(ws_url, early) { |
|
360 | 468 | this.stop_channels(); |
|
361 | 469 | this.events.trigger('status_disconnected.Kernel'); |
|
362 | 470 | if (!early) { |
|
363 | 471 | this.reconnect(); |
|
364 | 472 | } else { |
|
365 | 473 | console.log('WebSocket connection failed: ', ws_url); |
|
366 | 474 | this.events.trigger('early_disconnect.Kernel', ws_url); |
|
367 | 475 | } |
|
368 | 476 | }; |
|
369 | 477 | |
|
370 | 478 | /** |
|
371 |
* |
|
|
372 | * @method stop_channels | |
|
479 | * Close the websocket channels. After successful close, the value | |
|
480 | * in `this.channels[channel_name]` will be null. | |
|
481 | * | |
|
482 | * @function stop_channels | |
|
373 | 483 | */ |
|
374 | 484 | Kernel.prototype.stop_channels = function () { |
|
375 | 485 | var that = this; |
|
376 | 486 | var close = function (c) { |
|
377 | 487 | return function () { |
|
378 | 488 | if (that.channels[c].readyState === WebSocket.CLOSED) { |
|
379 | 489 | that.channels[c] = null; |
|
380 | 490 | } |
|
381 | 491 | }; |
|
382 | 492 | }; |
|
383 | 493 | for (var c in this.channels) { |
|
384 | 494 | if ( this.channels[c] !== null ) { |
|
385 | 495 | this.channels[c].onclose = close(c); |
|
386 | 496 | this.channels[c].close(); |
|
387 | 497 | } |
|
388 | 498 | } |
|
389 | 499 | }; |
|
390 | 500 | |
|
391 | // Main public methods. | |
|
392 | ||
|
501 | /** | |
|
502 | * Check whether there is a connection to the kernel. This | |
|
503 | * function only returns true if all channel objects have been | |
|
504 | * created and have a state of WebSocket.OPEN. | |
|
505 | * | |
|
506 | * @function is_connected | |
|
507 | * @returns {bool} - whether there is a connection | |
|
508 | */ | |
|
393 | 509 | Kernel.prototype.is_connected = function () { |
|
394 | 510 | for (var c in this.channels) { |
|
395 | 511 | // if any channel is not ready, then we're not connected |
|
396 | 512 | if (this.channels[c] === null) { |
|
397 | 513 | return false; |
|
398 | 514 | } |
|
399 | 515 | if (this.channels[c].readyState !== WebSocket.OPEN) { |
|
400 | 516 | return false; |
|
401 | 517 | } |
|
402 | 518 | } |
|
403 | 519 | return true; |
|
404 | 520 | }; |
|
405 | 521 | |
|
522 | /** | |
|
523 | * Check whether the connection to the kernel has been completely | |
|
524 | * severed. This function only returns true if all channel objects | |
|
525 | * are null. | |
|
526 | * | |
|
527 | * @function is_fully_disconnected | |
|
528 | * @returns {bool} - whether the kernel is fully disconnected | |
|
529 | */ | |
|
406 | 530 | Kernel.prototype.is_fully_disconnected = function () { |
|
407 | 531 | for (var c in this.channels) { |
|
408 | 532 | if (this.channels[c] === null) { |
|
409 | 533 | return true; |
|
410 | 534 | } |
|
411 | 535 | } |
|
412 | 536 | return false; |
|
413 | 537 | }; |
|
414 | 538 | |
|
415 | // send a message on the Kernel's shell channel | |
|
539 | /** | |
|
540 | * Send a message on the Kernel's shell channel | |
|
541 | * | |
|
542 | * @function send_shell_message | |
|
543 | */ | |
|
416 | 544 | Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) { |
|
417 | 545 | if (!this.is_connected()) { |
|
418 | 546 | throw new Error("kernel is not connected"); |
|
419 | 547 | } |
|
420 | 548 | var msg = this._get_msg(msg_type, content, metadata); |
|
421 | 549 | this.channels.shell.send(JSON.stringify(msg)); |
|
422 | 550 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
423 | 551 | return msg.header.msg_id; |
|
424 | 552 | }; |
|
425 | 553 | |
|
426 | 554 | /** |
|
427 | 555 | * Get kernel info |
|
428 | 556 | * |
|
557 | * @function kernel_info | |
|
429 | 558 | * @param callback {function} |
|
430 | * @method kernel_info | |
|
431 | 559 | * |
|
432 | 560 | * When calling this method, pass a callback function that expects one argument. |
|
433 | 561 | * The callback will be passed the complete `kernel_info_reply` message documented |
|
434 | 562 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info) |
|
435 | 563 | */ |
|
436 | 564 | Kernel.prototype.kernel_info = function (callback) { |
|
437 | 565 | var callbacks; |
|
438 | 566 | if (callback) { |
|
439 | 567 | callbacks = { shell : { reply : callback } }; |
|
440 | 568 | } |
|
441 | 569 | return this.send_shell_message("kernel_info_request", {}, callbacks); |
|
442 | 570 | }; |
|
443 | 571 | |
|
444 | 572 | /** |
|
445 | 573 | * Get info on an object |
|
446 | 574 | * |
|
447 | * @param code {string} | |
|
448 | * @param cursor_pos {integer} | |
|
449 | * @param callback {function} | |
|
450 | * @method inspect | |
|
451 | * | |
|
452 | 575 | * When calling this method, pass a callback function that expects one argument. |
|
453 | 576 | * The callback will be passed the complete `inspect_reply` message documented |
|
454 | 577 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) |
|
578 | * | |
|
579 | * @function inspect | |
|
580 | * @param code {string} | |
|
581 | * @param cursor_pos {integer} | |
|
582 | * @param callback {function} | |
|
455 | 583 | */ |
|
456 | 584 | Kernel.prototype.inspect = function (code, cursor_pos, callback) { |
|
457 | 585 | var callbacks; |
|
458 | 586 | if (callback) { |
|
459 | 587 | callbacks = { shell : { reply : callback } }; |
|
460 | 588 | } |
|
461 | 589 | |
|
462 | 590 | var content = { |
|
463 | 591 | code : code, |
|
464 | 592 | cursor_pos : cursor_pos, |
|
465 | 593 | detail_level : 0 |
|
466 | 594 | }; |
|
467 | 595 | return this.send_shell_message("inspect_request", content, callbacks); |
|
468 | 596 | }; |
|
469 | 597 | |
|
470 | 598 | /** |
|
471 | 599 | * Execute given code into kernel, and pass result to callback. |
|
472 | 600 | * |
|
473 | 601 | * @async |
|
474 |
* @ |
|
|
602 | * @function execute | |
|
475 | 603 | * @param {string} code |
|
476 | 604 | * @param [callbacks] {Object} With the following keys (all optional) |
|
477 | 605 | * @param callbacks.shell.reply {function} |
|
478 | 606 | * @param callbacks.shell.payload.[payload_name] {function} |
|
479 | 607 | * @param callbacks.iopub.output {function} |
|
480 | 608 | * @param callbacks.iopub.clear_output {function} |
|
481 | 609 | * @param callbacks.input {function} |
|
482 | 610 | * @param {object} [options] |
|
483 | 611 | * @param [options.silent=false] {Boolean} |
|
484 | 612 | * @param [options.user_expressions=empty_dict] {Dict} |
|
485 | 613 | * @param [options.allow_stdin=false] {Boolean} true|false |
|
486 | 614 | * |
|
487 | 615 | * @example |
|
488 | 616 | * |
|
489 |
* The options object should contain the options for the execute |
|
|
490 | * values are: | |
|
617 | * The options object should contain the options for the execute | |
|
618 | * call. Its default values are: | |
|
491 | 619 | * |
|
492 | 620 | * options = { |
|
493 | 621 | * silent : true, |
|
494 | 622 | * user_expressions : {}, |
|
495 | 623 | * allow_stdin : false |
|
496 | 624 | * } |
|
497 | 625 | * |
|
498 |
* When calling this method pass a callbacks structure of the |
|
|
626 | * When calling this method pass a callbacks structure of the | |
|
627 | * form: | |
|
499 | 628 | * |
|
500 | 629 | * callbacks = { |
|
501 | 630 | * shell : { |
|
502 | 631 | * reply : execute_reply_callback, |
|
503 | 632 | * payload : { |
|
504 | 633 | * set_next_input : set_next_input_callback, |
|
505 | 634 | * } |
|
506 | 635 | * }, |
|
507 | 636 | * iopub : { |
|
508 | 637 | * output : output_callback, |
|
509 | 638 | * clear_output : clear_output_callback, |
|
510 | 639 | * }, |
|
511 | 640 | * input : raw_input_callback |
|
512 | 641 | * } |
|
513 | 642 | * |
|
514 |
* Each callback will be passed the entire message as a single |
|
|
515 |
* Payload handlers will be passed the corresponding |
|
|
643 | * Each callback will be passed the entire message as a single | |
|
644 | * arugment. Payload handlers will be passed the corresponding | |
|
645 | * payload and the execute_reply message. | |
|
516 | 646 | */ |
|
517 | 647 | Kernel.prototype.execute = function (code, callbacks, options) { |
|
518 | 648 | var content = { |
|
519 | 649 | code : code, |
|
520 | 650 | silent : true, |
|
521 | 651 | store_history : false, |
|
522 | 652 | user_expressions : {}, |
|
523 | 653 | allow_stdin : false |
|
524 | 654 | }; |
|
525 | 655 | callbacks = callbacks || {}; |
|
526 | 656 | if (callbacks.input !== undefined) { |
|
527 | 657 | content.allow_stdin = true; |
|
528 | 658 | } |
|
529 | 659 | $.extend(true, content, options); |
|
530 | 660 | this.events.trigger('execution_request.Kernel', {kernel: this, content:content}); |
|
531 | 661 | return this.send_shell_message("execute_request", content, callbacks); |
|
532 | 662 | }; |
|
533 | 663 | |
|
534 | 664 | /** |
|
535 |
* When calling this method, pass a function to be called with the |
|
|
536 | * as its only argument when it arrives. | |
|
665 | * When calling this method, pass a function to be called with the | |
|
666 | * `complete_reply` message as its only argument when it arrives. | |
|
537 | 667 | * |
|
538 | 668 | * `complete_reply` is documented |
|
539 | 669 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) |
|
540 | 670 | * |
|
541 |
* @ |
|
|
671 | * @function complete | |
|
542 | 672 | * @param code {string} |
|
543 | 673 | * @param cursor_pos {integer} |
|
544 | 674 | * @param callback {function} |
|
545 | * | |
|
546 | 675 | */ |
|
547 | 676 | Kernel.prototype.complete = function (code, cursor_pos, callback) { |
|
548 | 677 | var callbacks; |
|
549 | 678 | if (callback) { |
|
550 | 679 | callbacks = { shell : { reply : callback } }; |
|
551 | 680 | } |
|
552 | 681 | var content = { |
|
553 | 682 | code : code, |
|
554 | 683 | cursor_pos : cursor_pos |
|
555 | 684 | }; |
|
556 | 685 | return this.send_shell_message("complete_request", content, callbacks); |
|
557 | 686 | }; |
|
558 | 687 | |
|
688 | /** | |
|
689 | * @function send_input_reply | |
|
690 | */ | |
|
559 | 691 | Kernel.prototype.send_input_reply = function (input) { |
|
560 | 692 | if (!this.is_connected()) { |
|
561 | 693 | throw new Error("kernel is not connected"); |
|
562 | 694 | } |
|
563 | 695 | var content = { |
|
564 | 696 | value : input |
|
565 | 697 | }; |
|
566 | 698 | this.events.trigger('input_reply.Kernel', {kernel: this, content:content}); |
|
567 | 699 | var msg = this._get_msg("input_reply", content); |
|
568 | 700 | this.channels.stdin.send(JSON.stringify(msg)); |
|
569 | 701 | return msg.header.msg_id; |
|
570 | 702 | }; |
|
571 | 703 | |
|
572 | ||
|
573 | // Reply handlers | |
|
574 | ||
|
704 | /** | |
|
705 | * @function register_iopub_handler | |
|
706 | */ | |
|
575 | 707 | Kernel.prototype.register_iopub_handler = function (msg_type, callback) { |
|
576 | 708 | this._iopub_handlers[msg_type] = callback; |
|
577 | 709 | }; |
|
578 | 710 | |
|
711 | /** | |
|
712 | * Get the iopub handler for a specific message type. | |
|
713 | * | |
|
714 | * @function get_iopub_handler | |
|
715 | */ | |
|
579 | 716 | Kernel.prototype.get_iopub_handler = function (msg_type) { |
|
580 | // get iopub handler for a specific message type | |
|
581 | 717 | return this._iopub_handlers[msg_type]; |
|
582 | 718 | }; |
|
583 | 719 | |
|
584 | ||
|
720 | /** | |
|
721 | * Get callbacks for a specific message. | |
|
722 | * | |
|
723 | * @function get_callbacks_for_msg | |
|
724 | */ | |
|
585 | 725 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { |
|
586 | // get callbacks for a specific message | |
|
587 | 726 | if (msg_id == this.last_msg_id) { |
|
588 | 727 | return this.last_msg_callbacks; |
|
589 | 728 | } else { |
|
590 | 729 | return this._msg_callbacks[msg_id]; |
|
591 | 730 | } |
|
592 | 731 | }; |
|
593 | 732 | |
|
594 | ||
|
733 | /** | |
|
734 | * Clear callbacks for a specific message. | |
|
735 | * | |
|
736 | * @function clear_callbacks_for_msg | |
|
737 | */ | |
|
595 | 738 | Kernel.prototype.clear_callbacks_for_msg = function (msg_id) { |
|
596 | 739 | if (this._msg_callbacks[msg_id] !== undefined ) { |
|
597 | 740 | delete this._msg_callbacks[msg_id]; |
|
598 | 741 | } |
|
599 | 742 | }; |
|
600 | 743 | |
|
744 | /** | |
|
745 | * @function _finish_shell | |
|
746 | */ | |
|
601 | 747 | Kernel.prototype._finish_shell = function (msg_id) { |
|
602 | 748 | var callbacks = this._msg_callbacks[msg_id]; |
|
603 | 749 | if (callbacks !== undefined) { |
|
604 | 750 | callbacks.shell_done = true; |
|
605 | 751 | if (callbacks.iopub_done) { |
|
606 | 752 | this.clear_callbacks_for_msg(msg_id); |
|
607 | 753 | } |
|
608 | 754 | } |
|
609 | 755 | }; |
|
610 | 756 | |
|
757 | /** | |
|
758 | * @function _finish_iopub | |
|
759 | */ | |
|
611 | 760 | Kernel.prototype._finish_iopub = function (msg_id) { |
|
612 | 761 | var callbacks = this._msg_callbacks[msg_id]; |
|
613 | 762 | if (callbacks !== undefined) { |
|
614 | 763 | callbacks.iopub_done = true; |
|
615 | 764 | if (callbacks.shell_done) { |
|
616 | 765 | this.clear_callbacks_for_msg(msg_id); |
|
617 | 766 | } |
|
618 | 767 | } |
|
619 | 768 | }; |
|
620 | 769 | |
|
621 | /* Set callbacks for a particular message. | |
|
770 | /** | |
|
771 | * Set callbacks for a particular message. | |
|
622 | 772 | * Callbacks should be a struct of the following form: |
|
623 | 773 | * shell : { |
|
624 | 774 | * |
|
625 | 775 | * } |
|
626 | ||
|
776 | * | |
|
777 | * @function set_callbacks_for_msg | |
|
627 | 778 | */ |
|
628 | 779 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { |
|
629 | 780 | this.last_msg_id = msg_id; |
|
630 | 781 | if (callbacks) { |
|
631 | 782 | // shallow-copy mapping, because we will modify it at the top level |
|
632 | 783 | var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {}; |
|
633 | 784 | cbcopy.shell = callbacks.shell; |
|
634 | 785 | cbcopy.iopub = callbacks.iopub; |
|
635 | 786 | cbcopy.input = callbacks.input; |
|
636 | 787 | cbcopy.shell_done = (!callbacks.shell); |
|
637 | 788 | cbcopy.iopub_done = (!callbacks.iopub); |
|
638 | 789 | } else { |
|
639 | 790 | this.last_msg_callbacks = {}; |
|
640 | 791 | } |
|
641 | 792 | }; |
|
642 | 793 | |
|
643 | ||
|
794 | /** | |
|
795 | * @function _handle_shell_reply | |
|
796 | */ | |
|
644 | 797 | Kernel.prototype._handle_shell_reply = function (e) { |
|
645 | 798 | var reply = $.parseJSON(e.data); |
|
646 | 799 | this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply}); |
|
647 | 800 | var content = reply.content; |
|
648 | 801 | var metadata = reply.metadata; |
|
649 | 802 | var parent_id = reply.parent_header.msg_id; |
|
650 | 803 | var callbacks = this.get_callbacks_for_msg(parent_id); |
|
651 | 804 | if (!callbacks || !callbacks.shell) { |
|
652 | 805 | return; |
|
653 | 806 | } |
|
654 | 807 | var shell_callbacks = callbacks.shell; |
|
655 | 808 | |
|
656 | 809 | // signal that shell callbacks are done |
|
657 | 810 | this._finish_shell(parent_id); |
|
658 | 811 | |
|
659 | 812 | if (shell_callbacks.reply !== undefined) { |
|
660 | 813 | shell_callbacks.reply(reply); |
|
661 | 814 | } |
|
662 | 815 | if (content.payload && shell_callbacks.payload) { |
|
663 | 816 | this._handle_payloads(content.payload, shell_callbacks.payload, reply); |
|
664 | 817 | } |
|
665 | 818 | }; |
|
666 | 819 | |
|
667 | ||
|
820 | /** | |
|
821 | * @function _handle_payloads | |
|
822 | */ | |
|
668 | 823 | Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) { |
|
669 | 824 | var l = payloads.length; |
|
670 | 825 | // Payloads are handled by triggering events because we don't want the Kernel |
|
671 | 826 | // to depend on the Notebook or Pager classes. |
|
672 | 827 | for (var i=0; i<l; i++) { |
|
673 | 828 | var payload = payloads[i]; |
|
674 | 829 | var callback = payload_callbacks[payload.source]; |
|
675 | 830 | if (callback) { |
|
676 | 831 | callback(payload, msg); |
|
677 | 832 | } |
|
678 | 833 | } |
|
679 | 834 | }; |
|
680 | 835 | |
|
836 | /** | |
|
837 | * @function _handle_status_message | |
|
838 | */ | |
|
681 | 839 | Kernel.prototype._handle_status_message = function (msg) { |
|
682 | 840 | var execution_state = msg.content.execution_state; |
|
683 | 841 | var parent_id = msg.parent_header.msg_id; |
|
684 | 842 | |
|
685 | 843 | // dispatch status msg callbacks, if any |
|
686 | 844 | var callbacks = this.get_callbacks_for_msg(parent_id); |
|
687 | 845 | if (callbacks && callbacks.iopub && callbacks.iopub.status) { |
|
688 | 846 | try { |
|
689 | 847 | callbacks.iopub.status(msg); |
|
690 | 848 | } catch (e) { |
|
691 | 849 | console.log("Exception in status msg handler", e, e.stack); |
|
692 | 850 | } |
|
693 | 851 | } |
|
694 | 852 | |
|
695 | 853 | if (execution_state === 'busy') { |
|
696 | 854 | this.events.trigger('status_busy.Kernel', {kernel: this}); |
|
697 | 855 | |
|
698 | 856 | } else if (execution_state === 'idle') { |
|
699 | 857 | // signal that iopub callbacks are (probably) done |
|
700 | 858 | // async output may still arrive, |
|
701 | 859 | // but only for the most recent request |
|
702 | 860 | this._finish_iopub(parent_id); |
|
703 | 861 | |
|
704 | 862 | // trigger status_idle event |
|
705 | 863 | this.events.trigger('status_idle.Kernel', {kernel: this}); |
|
706 | 864 | |
|
707 | 865 | } else if (execution_state === 'restarting') { |
|
708 | 866 | // autorestarting is distinct from restarting, |
|
709 | 867 | // in that it means the kernel died and the server is restarting it. |
|
710 | 868 | // status_restarting sets the notification widget, |
|
711 | 869 | // autorestart shows the more prominent dialog. |
|
712 | 870 | this.events.trigger('status_autorestarting.Kernel', {kernel: this}); |
|
713 | 871 | this.events.trigger('status_restarting.Kernel', {kernel: this}); |
|
714 | 872 | |
|
715 | 873 | } else if (execution_state === 'dead') { |
|
716 | 874 | this._kernel_dead(); |
|
717 | 875 | } |
|
718 | 876 | }; |
|
719 | 877 | |
|
720 | ||
|
721 |
|
|
|
878 | /** | |
|
879 | * Handle clear_output message | |
|
880 | * | |
|
881 | * @function _handle_clear_output | |
|
882 | */ | |
|
722 | 883 | Kernel.prototype._handle_clear_output = function (msg) { |
|
723 | 884 | var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); |
|
724 | 885 | if (!callbacks || !callbacks.iopub) { |
|
725 | 886 | return; |
|
726 | 887 | } |
|
727 | 888 | var callback = callbacks.iopub.clear_output; |
|
728 | 889 | if (callback) { |
|
729 | 890 | callback(msg); |
|
730 | 891 | } |
|
731 | 892 | }; |
|
732 | 893 | |
|
733 | ||
|
734 |
|
|
|
894 | /** | |
|
895 | * handle an output message (execute_result, display_data, etc.) | |
|
896 | * | |
|
897 | * @function _handle_output_message | |
|
898 | */ | |
|
735 | 899 | Kernel.prototype._handle_output_message = function (msg) { |
|
736 | 900 | var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); |
|
737 | 901 | if (!callbacks || !callbacks.iopub) { |
|
738 | 902 | return; |
|
739 | 903 | } |
|
740 | 904 | var callback = callbacks.iopub.output; |
|
741 | 905 | if (callback) { |
|
742 | 906 | callback(msg); |
|
743 | 907 | } |
|
744 | 908 | }; |
|
745 | 909 | |
|
746 | // dispatch IOPub messages to respective handlers. | |
|
747 | // each message type should have a handler. | |
|
910 | /** | |
|
911 | * Dispatch IOPub messages to respective handlers. Each message | |
|
912 | * type should have a handler. | |
|
913 | * | |
|
914 | * @function _handle_iopub_message | |
|
915 | */ | |
|
748 | 916 | Kernel.prototype._handle_iopub_message = function (e) { |
|
749 | 917 | var msg = $.parseJSON(e.data); |
|
750 | 918 | |
|
751 | 919 | var handler = this.get_iopub_handler(msg.header.msg_type); |
|
752 | 920 | if (handler !== undefined) { |
|
753 | 921 | handler(msg); |
|
754 | 922 | } |
|
755 | 923 | }; |
|
756 | 924 | |
|
757 | ||
|
925 | /** | |
|
926 | * @function _handle_input_request | |
|
927 | */ | |
|
758 | 928 | Kernel.prototype._handle_input_request = function (e) { |
|
759 | 929 | var request = $.parseJSON(e.data); |
|
760 | 930 | var header = request.header; |
|
761 | 931 | var content = request.content; |
|
762 | 932 | var metadata = request.metadata; |
|
763 | 933 | var msg_type = header.msg_type; |
|
764 | 934 | if (msg_type !== 'input_request') { |
|
765 | 935 | console.log("Invalid input request!", request); |
|
766 | 936 | return; |
|
767 | 937 | } |
|
768 | 938 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); |
|
769 | 939 | if (callbacks) { |
|
770 | 940 | if (callbacks.input) { |
|
771 | 941 | callbacks.input(request); |
|
772 | 942 | } |
|
773 | 943 | } |
|
774 | 944 | }; |
|
775 | 945 | |
|
776 | 946 | // Backwards compatability. |
|
777 | 947 | IPython.Kernel = Kernel; |
|
778 | 948 | |
|
779 | 949 | return {'Kernel': Kernel}; |
|
780 | 950 | }); |
@@ -1,279 +1,279 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'services/kernels/js/kernel', |
|
9 | 9 | ], function(IPython, $, utils, kernel) { |
|
10 | 10 | "use strict"; |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * Session object for accessing the session REST api. The session |
|
14 | 14 | * should be used to start kernels and then shut them down -- for |
|
15 | 15 | * all other operations, the kernel object should be used. |
|
16 | 16 | * |
|
17 | 17 | * Options should include: |
|
18 | 18 | * - notebook_name: the notebook name |
|
19 | 19 | * - notebook_path: the path (not including name) to the notebook |
|
20 | 20 | * - kernel_name: the type of kernel (e.g. python3) |
|
21 | 21 | * - base_url: the root url of the notebook server |
|
22 | 22 | * - ws_url: the url to access websockets |
|
23 | 23 | * - notebook: Notebook object |
|
24 | 24 | * |
|
25 | 25 | * @class Session |
|
26 | 26 | * @param {Object} options |
|
27 | 27 | */ |
|
28 | 28 | var Session = function (options) { |
|
29 | 29 | this.id = null; |
|
30 | 30 | this.notebook_model = { |
|
31 | 31 | name: options.notebook_name, |
|
32 | 32 | path: options.notebook_path |
|
33 | 33 | }; |
|
34 | 34 | this.kernel_model = { |
|
35 | 35 | id: null, |
|
36 | 36 | name: options.kernel_name |
|
37 | 37 | }; |
|
38 | 38 | |
|
39 | 39 | this.base_url = options.base_url; |
|
40 | 40 | this.ws_url = options.ws_url; |
|
41 | 41 | this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions'); |
|
42 | 42 | this.session_url = null; |
|
43 | 43 | |
|
44 | 44 | this.notebook = options.notebook; |
|
45 | 45 | this.kernel = null; |
|
46 | 46 | this.events = options.notebook.events; |
|
47 | 47 | }; |
|
48 | 48 | |
|
49 | 49 | // Public REST api functions |
|
50 | 50 | |
|
51 | 51 | /** |
|
52 | 52 | * GET /api/sessions |
|
53 | 53 | * |
|
54 | 54 | * Get a list of the current sessions. |
|
55 | 55 | * |
|
56 | 56 | * @function list |
|
57 | 57 | * @param {function} [success] - function executed on ajax success |
|
58 | 58 | * @param {function} [error] - functon executed on ajax error |
|
59 | 59 | */ |
|
60 | 60 | Session.prototype.list = function (success, error) { |
|
61 | 61 | $.ajax(this.session_service_url, { |
|
62 | 62 | processData: false, |
|
63 | 63 | cache: false, |
|
64 | 64 | type: "GET", |
|
65 | 65 | dataType: "json", |
|
66 | 66 | success: success, |
|
67 | 67 | error: this._on_error(error) |
|
68 | 68 | }); |
|
69 | 69 | }; |
|
70 | 70 | |
|
71 | 71 | /** |
|
72 | 72 | * POST /api/sessions |
|
73 | 73 | * |
|
74 | 74 | * Start a new session. This function can only executed once. |
|
75 | 75 | * |
|
76 | 76 | * @function start |
|
77 | 77 | * @param {function} [success] - function executed on ajax success |
|
78 | 78 | * @param {function} [error] - functon executed on ajax error |
|
79 | 79 | */ |
|
80 | 80 | Session.prototype.start = function (success, error) { |
|
81 | 81 | if (this.kernel !== null) { |
|
82 | 82 | throw new Error("session has already been started"); |
|
83 | 83 | }; |
|
84 | 84 | |
|
85 | 85 | var that = this; |
|
86 | 86 | var on_success = function (data, status, xhr) { |
|
87 | 87 | var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); |
|
88 | 88 | that.kernel = new kernel.Kernel( |
|
89 | 89 | kernel_service_url, that.ws_url, that.notebook, |
|
90 | 90 | that.kernel_model.id, that.kernel_model.name); |
|
91 |
that.kernel._kernel_started( |
|
|
91 | that.kernel._kernel_started(); | |
|
92 | 92 | if (success) { |
|
93 | 93 | success(data, status, xhr); |
|
94 | 94 | } |
|
95 | 95 | }; |
|
96 | 96 | var on_error = function (xhr, status, err) { |
|
97 | 97 | that.events.trigger('no_kernel.Kernel'); |
|
98 | 98 | if (error) { |
|
99 | 99 | error(xhr, status, err); |
|
100 | 100 | } |
|
101 | 101 | }; |
|
102 | 102 | |
|
103 | 103 | $.ajax(this.session_service_url, { |
|
104 | 104 | processData: false, |
|
105 | 105 | cache: false, |
|
106 | 106 | type: "POST", |
|
107 | 107 | data: JSON.stringify(this._get_model()), |
|
108 | 108 | dataType: "json", |
|
109 | 109 | success: this._on_success(on_success), |
|
110 | 110 | error: this._on_error(on_error) |
|
111 | 111 | }); |
|
112 | 112 | }; |
|
113 | 113 | |
|
114 | 114 | /** |
|
115 | 115 | * GET /api/sessions/[:session_id] |
|
116 | 116 | * |
|
117 | 117 | * Get information about a session. |
|
118 | 118 | * |
|
119 | 119 | * @function get_info |
|
120 | 120 | * @param {function} [success] - function executed on ajax success |
|
121 | 121 | * @param {function} [error] - functon executed on ajax error |
|
122 | 122 | */ |
|
123 | 123 | Session.prototype.get_info = function (success, error) { |
|
124 | 124 | $.ajax(this.session_url, { |
|
125 | 125 | processData: false, |
|
126 | 126 | cache: false, |
|
127 | 127 | type: "GET", |
|
128 | 128 | dataType: "json", |
|
129 | 129 | success: this._on_success(success), |
|
130 | 130 | error: this._on_error(error) |
|
131 | 131 | }); |
|
132 | 132 | }; |
|
133 | 133 | |
|
134 | 134 | /** |
|
135 | 135 | * PATCH /api/sessions/[:session_id] |
|
136 | 136 | * |
|
137 | 137 | * Rename or move a notebook. If the given name or path are |
|
138 | 138 | * undefined, then they will not be changed. |
|
139 | 139 | * |
|
140 | 140 | * @function rename_notebook |
|
141 | 141 | * @param {string} [name] - new notebook name |
|
142 | 142 | * @param {string} [path] - new path to notebook |
|
143 | 143 | * @param {function} [success] - function executed on ajax success |
|
144 | 144 | * @param {function} [error] - functon executed on ajax error |
|
145 | 145 | */ |
|
146 | 146 | Session.prototype.rename_notebook = function (name, path, success, error) { |
|
147 | 147 | if (name !== undefined) { |
|
148 | 148 | this.notebook_model.name = name; |
|
149 | 149 | } |
|
150 | 150 | if (path !== undefined) { |
|
151 | 151 | this.notebook_model.path = path; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | $.ajax(this.session_url, { |
|
155 | 155 | processData: false, |
|
156 | 156 | cache: false, |
|
157 | 157 | type: "PATCH", |
|
158 | 158 | data: JSON.stringify(this._get_model()), |
|
159 | 159 | dataType: "json", |
|
160 | 160 | success: this._on_success(success), |
|
161 | 161 | error: this._on_error(error) |
|
162 | 162 | }); |
|
163 | 163 | }; |
|
164 | 164 | |
|
165 | 165 | /** |
|
166 | 166 | * DELETE /api/sessions/[:session_id] |
|
167 | 167 | * |
|
168 | 168 | * Kill the kernel and shutdown the session. |
|
169 | 169 | * |
|
170 | 170 | * @function delete |
|
171 | 171 | * @param {function} [success] - function executed on ajax success |
|
172 | 172 | * @param {function} [error] - functon executed on ajax error |
|
173 | 173 | */ |
|
174 | 174 | Session.prototype.delete = function (success, error) { |
|
175 | 175 | if (this.kernel) { |
|
176 | 176 | this.kernel._kernel_dead(); |
|
177 | 177 | } |
|
178 | 178 | |
|
179 | 179 | $.ajax(this.session_url, { |
|
180 | 180 | processData: false, |
|
181 | 181 | cache: false, |
|
182 | 182 | type: "DELETE", |
|
183 | 183 | dataType: "json", |
|
184 | 184 | success: this._on_success(success), |
|
185 | 185 | error: this._on_error(error) |
|
186 | 186 | }); |
|
187 | 187 | }; |
|
188 | 188 | |
|
189 | 189 | // Helper functions |
|
190 | 190 | |
|
191 | 191 | /** |
|
192 | 192 | * Get the data model for the session, which includes the notebook |
|
193 | 193 | * (name and path) and kernel (name and id). |
|
194 | 194 | * |
|
195 | 195 | * @function _get_model |
|
196 | 196 | * @returns {Object} - the data model |
|
197 | 197 | */ |
|
198 | 198 | Session.prototype._get_model = function () { |
|
199 | 199 | return { |
|
200 | 200 | notebook: this.notebook_model, |
|
201 | 201 | kernel: this.kernel_model |
|
202 | 202 | }; |
|
203 | 203 | }; |
|
204 | 204 | |
|
205 | 205 | /** |
|
206 | 206 | * Update the data model from the given JSON object, which should |
|
207 | 207 | * have attributes of `id`, `notebook`, and/or `kernel`. If |
|
208 | 208 | * provided, the notebook data must include name and path, and the |
|
209 | 209 | * kernel data must include name and id. |
|
210 | 210 | * |
|
211 | 211 | * @function _update_model |
|
212 | 212 | * @param {Object} data - updated data model |
|
213 | 213 | */ |
|
214 | 214 | Session.prototype._update_model = function (data) { |
|
215 | 215 | if (data && data.id) { |
|
216 | 216 | this.id = data.id; |
|
217 | 217 | this.session_url = utils.url_join_encode(this.session_service_url, this.id); |
|
218 | 218 | } |
|
219 | 219 | if (data && data.notebook) { |
|
220 | 220 | this.notebook_model.name = data.notebook.name; |
|
221 | 221 | this.notebook_model.path = data.notebook.path; |
|
222 | 222 | } |
|
223 | 223 | if (data && data.kernel) { |
|
224 | 224 | this.kernel_model.name = data.kernel.name; |
|
225 | 225 | this.kernel_model.id = data.kernel.id; |
|
226 | 226 | } |
|
227 | 227 | }; |
|
228 | 228 | |
|
229 | 229 | /** |
|
230 | 230 | * Handle a successful AJAX request by updating the session data |
|
231 | 231 | * model with the response, and then optionally calling a provided |
|
232 | 232 | * callback. |
|
233 | 233 | * |
|
234 | 234 | * @function _on_success |
|
235 | 235 | * @param {function} success - callback |
|
236 | 236 | */ |
|
237 | 237 | Session.prototype._on_success = function (success) { |
|
238 | 238 | var that = this; |
|
239 | 239 | return function (data, status, xhr) { |
|
240 | 240 | that._update_model(data); |
|
241 | 241 | if (success) { |
|
242 | 242 | success(data, status, xhr); |
|
243 | 243 | } |
|
244 | 244 | }; |
|
245 | 245 | }; |
|
246 | 246 | |
|
247 | 247 | /** |
|
248 | 248 | * Handle a failed AJAX request by logging the error message, and |
|
249 | 249 | * then optionally calling a provided callback. |
|
250 | 250 | * |
|
251 | 251 | * @function _on_error |
|
252 | 252 | * @param {function} error - callback |
|
253 | 253 | */ |
|
254 | 254 | Session.prototype._on_error = function (error) { |
|
255 | 255 | return function (xhr, status, err) { |
|
256 | 256 | utils.log_ajax_error(xhr, status, err); |
|
257 | 257 | if (error) { |
|
258 | 258 | error(xhr, status, err); |
|
259 | 259 | } |
|
260 | 260 | }; |
|
261 | 261 | }; |
|
262 | 262 | |
|
263 | 263 | /** |
|
264 | 264 | * Error type indicating that the session is already starting. |
|
265 | 265 | */ |
|
266 | 266 | var SessionAlreadyStarting = function (message) { |
|
267 | 267 | this.name = "SessionAlreadyStarting"; |
|
268 | 268 | this.message = (message || ""); |
|
269 | 269 | }; |
|
270 | 270 | SessionAlreadyStarting.prototype = Error.prototype; |
|
271 | 271 | |
|
272 | 272 | // For backwards compatability. |
|
273 | 273 | IPython.Session = Session; |
|
274 | 274 | |
|
275 | 275 | return { |
|
276 | 276 | Session: Session, |
|
277 | 277 | SessionAlreadyStarting: SessionAlreadyStarting |
|
278 | 278 | }; |
|
279 | 279 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now