##// END OF EJS Templates
Only set channels to null when they are fully closed
Jessica B. Hamrick -
Show More
@@ -1,746 +1,762
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'services/kernels/js/comm',
8 'services/kernels/js/comm',
9 'widgets/js/init',
9 'widgets/js/init',
10 ], function(IPython, $, utils, comm, widgetmanager) {
10 ], function(IPython, $, utils, comm, widgetmanager) {
11 "use strict";
11 "use strict";
12
12
13 // Initialization and connection.
13 // Initialization and connection.
14 /**
14 /**
15 * A Kernel Class to communicate with the Python kernel
15 * A Kernel Class to communicate with the Python kernel
16 * @Class Kernel
16 * @Class Kernel
17 */
17 */
18 var Kernel = function (kernel_service_url, ws_url, notebook, id, name) {
18 var Kernel = function (kernel_service_url, ws_url, notebook, id, name) {
19 this.events = notebook.events;
19 this.events = notebook.events;
20
20
21 this.id = id;
21 this.id = id;
22 this.name = name;
22 this.name = name;
23
23
24 this.channels = {
24 this.channels = {
25 'shell': null,
25 'shell': null,
26 'iopub': null,
26 'iopub': null,
27 'stdin': null
27 'stdin': null
28 };
28 };
29
29
30 this.kernel_service_url = kernel_service_url;
30 this.kernel_service_url = kernel_service_url;
31 this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id);
31 this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id);
32 this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl");
32 this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl");
33 if (!this.ws_url) {
33 if (!this.ws_url) {
34 // trailing 's' in https will become wss for secure web sockets
34 // trailing 's' in https will become wss for secure web sockets
35 this.ws_url = location.protocol.replace('http', 'ws') + "//" + location.host;
35 this.ws_url = location.protocol.replace('http', 'ws') + "//" + location.host;
36 }
36 }
37
37
38 this.username = "username";
38 this.username = "username";
39 this.session_id = utils.uuid();
39 this.session_id = utils.uuid();
40 this._msg_callbacks = {};
40 this._msg_callbacks = {};
41
41
42 if (typeof(WebSocket) !== 'undefined') {
42 if (typeof(WebSocket) !== 'undefined') {
43 this.WebSocket = WebSocket;
43 this.WebSocket = WebSocket;
44 } else if (typeof(MozWebSocket) !== 'undefined') {
44 } else if (typeof(MozWebSocket) !== 'undefined') {
45 this.WebSocket = MozWebSocket;
45 this.WebSocket = MozWebSocket;
46 } else {
46 } else {
47 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.');
47 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 }
48 }
49
49
50 this.bind_events();
50 this.bind_events();
51 this.init_iopub_handlers();
51 this.init_iopub_handlers();
52 this.comm_manager = new comm.CommManager(this);
52 this.comm_manager = new comm.CommManager(this);
53 this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook);
53 this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook);
54
54
55 this.last_msg_id = null;
55 this.last_msg_id = null;
56 this.last_msg_callbacks = {};
56 this.last_msg_callbacks = {};
57 };
57 };
58
58
59
59
60 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
60 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
61 var msg = {
61 var msg = {
62 header : {
62 header : {
63 msg_id : utils.uuid(),
63 msg_id : utils.uuid(),
64 username : this.username,
64 username : this.username,
65 session : this.session_id,
65 session : this.session_id,
66 msg_type : msg_type,
66 msg_type : msg_type,
67 version : "5.0"
67 version : "5.0"
68 },
68 },
69 metadata : metadata || {},
69 metadata : metadata || {},
70 content : content,
70 content : content,
71 parent_header : {}
71 parent_header : {}
72 };
72 };
73 return msg;
73 return msg;
74 };
74 };
75
75
76 Kernel.prototype.bind_events = function () {
76 Kernel.prototype.bind_events = function () {
77 var that = this;
77 var that = this;
78 this.events.on('send_input_reply.Kernel', function(evt, data) {
78 this.events.on('send_input_reply.Kernel', function(evt, data) {
79 that.send_input_reply(data);
79 that.send_input_reply(data);
80 });
80 });
81 };
81 };
82
82
83 // Initialize the iopub handlers
83 // Initialize the iopub handlers
84
84
85 Kernel.prototype.init_iopub_handlers = function () {
85 Kernel.prototype.init_iopub_handlers = function () {
86 var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
86 var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
87 this._iopub_handlers = {};
87 this._iopub_handlers = {};
88 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
88 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
89 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
89 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
90
90
91 for (var i=0; i < output_msg_types.length; i++) {
91 for (var i=0; i < output_msg_types.length; i++) {
92 this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this));
92 this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this));
93 }
93 }
94 };
94 };
95
95
96 /**
96 /**
97 * GET /api/kernels
97 * GET /api/kernels
98 */
98 */
99 Kernel.prototype.list = function (success, error) {
99 Kernel.prototype.list = function (success, error) {
100 $.ajax(this.kernel_service_url, {
100 $.ajax(this.kernel_service_url, {
101 processData: false,
101 processData: false,
102 cache: false,
102 cache: false,
103 type: "GET",
103 type: "GET",
104 dataType: "json",
104 dataType: "json",
105 success: success,
105 success: success,
106 error: this._on_error(error)
106 error: this._on_error(error)
107 });
107 });
108 };
108 };
109
109
110 /**
110 /**
111 * POST /api/kernels
111 * POST /api/kernels
112 */
112 */
113 Kernel.prototype.start = function (success, error) {
113 Kernel.prototype.start = function (success, error) {
114 var that = this;
114 var that = this;
115 var on_success = function (data, status, xhr) {
115 var on_success = function (data, status, xhr) {
116 that._kernel_started(data);
116 that._kernel_started(data);
117 if (success) {
117 if (success) {
118 success(data, status, xhr);
118 success(data, status, xhr);
119 }
119 }
120 };
120 };
121
121
122 $.ajax(this.kernel_service_url, {
122 $.ajax(this.kernel_service_url, {
123 processData: false,
123 processData: false,
124 cache: false,
124 cache: false,
125 type: "POST",
125 type: "POST",
126 dataType: "json",
126 dataType: "json",
127 success: this._on_success(on_success),
127 success: this._on_success(on_success),
128 error: this._on_error(error)
128 error: this._on_error(error)
129 });
129 });
130 };
130 };
131
131
132 /**
132 /**
133 * GET /api/kernels/[:kernel_id]
133 * GET /api/kernels/[:kernel_id]
134 */
134 */
135 Kernel.prototype.get_info = function (success, error) {
135 Kernel.prototype.get_info = function (success, error) {
136 $.ajax(this.kernel_url, {
136 $.ajax(this.kernel_url, {
137 processData: false,
137 processData: false,
138 cache: false,
138 cache: false,
139 type: "GET",
139 type: "GET",
140 dataType: "json",
140 dataType: "json",
141 success: this._on_success(success),
141 success: this._on_success(success),
142 error: this._on_error(error)
142 error: this._on_error(error)
143 });
143 });
144 };
144 };
145
145
146 /**
146 /**
147 * DELETE /api/kernels/[:kernel_id]
147 * DELETE /api/kernels/[:kernel_id]
148 */
148 */
149 Kernel.prototype.kill = function (success, error) {
149 Kernel.prototype.kill = function (success, error) {
150 this._kernel_dead();
150 this._kernel_dead();
151 $.ajax(this.kernel_url, {
151 $.ajax(this.kernel_url, {
152 processData: false,
152 processData: false,
153 cache: false,
153 cache: false,
154 type: "DELETE",
154 type: "DELETE",
155 dataType: "json",
155 dataType: "json",
156 success: this._on_success(success),
156 success: this._on_success(success),
157 error: this._on_error(error)
157 error: this._on_error(error)
158 });
158 });
159 };
159 };
160
160
161 /**
161 /**
162 * POST /api/kernels/[:kernel_id]/interrupt
162 * POST /api/kernels/[:kernel_id]/interrupt
163 */
163 */
164 Kernel.prototype.interrupt = function (success, error) {
164 Kernel.prototype.interrupt = function (success, error) {
165 this.events.trigger('status_interrupting.Kernel', {kernel: this});
165 this.events.trigger('status_interrupting.Kernel', {kernel: this});
166 var url = utils.url_join_encode(this.kernel_url, 'interrupt');
166 var url = utils.url_join_encode(this.kernel_url, 'interrupt');
167 $.ajax(url, {
167 $.ajax(url, {
168 processData: false,
168 processData: false,
169 cache: false,
169 cache: false,
170 type: "POST",
170 type: "POST",
171 dataType: "json",
171 dataType: "json",
172 success: this._on_success(success),
172 success: this._on_success(success),
173 error: this._on_error(error)
173 error: this._on_error(error)
174 });
174 });
175 };
175 };
176
176
177 /**
177 /**
178 * POST /api/kernels/[:kernel_id]/restart
178 * POST /api/kernels/[:kernel_id]/restart
179 */
179 */
180 Kernel.prototype.restart = function (success, error) {
180 Kernel.prototype.restart = function (success, error) {
181 this.events.trigger('status_restarting.Kernel', {kernel: this});
181 this.events.trigger('status_restarting.Kernel', {kernel: this});
182 this.stop_channels();
182 this.stop_channels();
183
183
184 var that = this;
184 var that = this;
185 var on_success = function (data, status, xhr) {
185 var on_success = function (data, status, xhr) {
186 that._kernel_started(data, status, xhr);
186 that._kernel_started(data, status, xhr);
187 if (success) {
187 if (success) {
188 success(data, status, xhr);
188 success(data, status, xhr);
189 }
189 }
190 };
190 };
191
191
192 var url = utils.url_join_encode(this.kernel_url, 'restart');
192 var url = utils.url_join_encode(this.kernel_url, 'restart');
193 $.ajax(url, {
193 $.ajax(url, {
194 processData: false,
194 processData: false,
195 cache: false,
195 cache: false,
196 type: "POST",
196 type: "POST",
197 dataType: "json",
197 dataType: "json",
198 success: this._on_success(on_success),
198 success: this._on_success(on_success),
199 error: this._on_error(error)
199 error: this._on_error(error)
200 });
200 });
201 };
201 };
202
202
203 /**
203 /**
204 * Not actually a HTTP request, but useful function nonetheless
204 * Not actually a HTTP request, but useful function nonetheless
205 * for reconnecting to the kernel if the connection is somehow lost
205 * for reconnecting to the kernel if the connection is somehow lost
206 */
206 */
207 Kernel.prototype.reconnect = function () {
207 Kernel.prototype.reconnect = function () {
208 this.events.trigger('status_reconnecting.Kernel');
208 this.events.trigger('status_reconnecting.Kernel');
209 var that = this;
209 var that = this;
210 setTimeout(function () {
210 setTimeout(function () {
211 that.start_channels();
211 that.start_channels();
212 }, 5000);
212 }, 5000);
213 };
213 };
214
214
215 Kernel.prototype._on_success = function (success) {
215 Kernel.prototype._on_success = function (success) {
216 var that = this;
216 var that = this;
217 return function (data, status, xhr) {
217 return function (data, status, xhr) {
218 if (data) {
218 if (data) {
219 that.id = data.id;
219 that.id = data.id;
220 that.name = data.name;
220 that.name = data.name;
221 }
221 }
222 that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id);
222 that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id);
223 if (success) {
223 if (success) {
224 success(data, status, xhr);
224 success(data, status, xhr);
225 }
225 }
226 };
226 };
227 };
227 };
228
228
229 Kernel.prototype._on_error = function (error) {
229 Kernel.prototype._on_error = function (error) {
230 return function (xhr, status, err) {
230 return function (xhr, status, err) {
231 utils.log_ajax_error(xhr, status, err);
231 utils.log_ajax_error(xhr, status, err);
232 if (error) {
232 if (error) {
233 error(xhr, status, err);
233 error(xhr, status, err);
234 }
234 }
235 };
235 };
236 };
236 };
237
237
238 Kernel.prototype._kernel_started = function (json) {
238 Kernel.prototype._kernel_started = function (json) {
239 console.log("Kernel started: ", json.id);
239 console.log("Kernel started: ", json.id);
240 this.events.trigger('status_started.Kernel', {kernel: this});
240 this.events.trigger('status_started.Kernel', {kernel: this});
241 this.start_channels();
241 this.start_channels();
242 };
242 };
243
243
244 Kernel.prototype._kernel_connected = function () {
244 Kernel.prototype._kernel_connected = function () {
245 var that = this;
245 var that = this;
246 console.log('Connected to kernel: ', this.id);
246 console.log('Connected to kernel: ', this.id);
247 this.events.trigger('status_connected.Kernel');
247 this.events.trigger('status_connected.Kernel');
248 this.kernel_info(function () {
248 this.kernel_info(function () {
249 that.events.trigger('status_idle.Kernel');
249 that.events.trigger('status_idle.Kernel');
250 });
250 });
251 };
251 };
252
252
253 Kernel.prototype._kernel_dead = function () {
253 Kernel.prototype._kernel_dead = function () {
254 this.events.trigger('status_dead.Kernel');
254 this.events.trigger('status_dead.Kernel');
255 this.stop_channels();
255 this.stop_channels();
256 };
256 };
257
257
258
258
259 /**
259 /**
260 * Start the `shell`and `iopub` channels.
260 * Start the `shell`and `iopub` channels.
261 * Will stop and restart them if they already exist.
261 * Will stop and restart them if they already exist.
262 *
262 *
263 * @method start_channels
263 * @method start_channels
264 */
264 */
265 Kernel.prototype.start_channels = function () {
265 Kernel.prototype.start_channels = function () {
266 var that = this;
266 var that = this;
267 this.stop_channels();
267 this.stop_channels();
268 var ws_host_url = this.ws_url + this.kernel_url;
268 var ws_host_url = this.ws_url + this.kernel_url;
269 console.log("Starting WebSockets:", ws_host_url);
269 console.log("Starting WebSockets:", ws_host_url);
270 this.channels.shell = new this.WebSocket(
270 this.channels.shell = new this.WebSocket(
271 this.ws_url + utils.url_join_encode(this.kernel_url, "shell")
271 this.ws_url + utils.url_join_encode(this.kernel_url, "shell")
272 );
272 );
273 this.channels.stdin = new this.WebSocket(
273 this.channels.stdin = new this.WebSocket(
274 this.ws_url + utils.url_join_encode(this.kernel_url, "stdin")
274 this.ws_url + utils.url_join_encode(this.kernel_url, "stdin")
275 );
275 );
276 this.channels.iopub = new this.WebSocket(
276 this.channels.iopub = new this.WebSocket(
277 this.ws_url + utils.url_join_encode(this.kernel_url, "iopub")
277 this.ws_url + utils.url_join_encode(this.kernel_url, "iopub")
278 );
278 );
279
279
280 var already_called_onclose = false; // only alert once
280 var already_called_onclose = false; // only alert once
281 var ws_closed_early = function(evt){
281 var ws_closed_early = function(evt){
282 if (already_called_onclose){
282 if (already_called_onclose){
283 return;
283 return;
284 }
284 }
285 already_called_onclose = true;
285 already_called_onclose = true;
286 if ( ! evt.wasClean ){
286 if ( ! evt.wasClean ){
287 that._ws_closed(ws_host_url, true);
287 that._ws_closed(ws_host_url, true);
288 }
288 }
289 };
289 };
290 var ws_closed_late = function(evt){
290 var ws_closed_late = function(evt){
291 if (already_called_onclose){
291 if (already_called_onclose){
292 return;
292 return;
293 }
293 }
294 already_called_onclose = true;
294 already_called_onclose = true;
295 if ( ! evt.wasClean ){
295 if ( ! evt.wasClean ){
296 that._ws_closed(ws_host_url, false);
296 that._ws_closed(ws_host_url, false);
297 }
297 }
298 };
298 };
299 var ws_error = function(evt){
299 var ws_error = function(evt){
300 if (already_called_onclose){
300 if (already_called_onclose){
301 return;
301 return;
302 }
302 }
303 already_called_onclose = true;
303 already_called_onclose = true;
304 that._ws_closed(ws_host_url, false);
304 that._ws_closed(ws_host_url, false);
305 };
305 };
306
306
307 for (var c in this.channels) {
307 for (var c in this.channels) {
308 this.channels[c].onopen = $.proxy(this._ws_opened, this);
308 this.channels[c].onopen = $.proxy(this._ws_opened, this);
309 this.channels[c].onclose = ws_closed_early;
309 this.channels[c].onclose = ws_closed_early;
310 this.channels[c].onerror = ws_error;
310 this.channels[c].onerror = ws_error;
311 }
311 }
312 // switch from early-close to late-close message after 1s
312 // switch from early-close to late-close message after 1s
313 setTimeout(function() {
313 setTimeout(function() {
314 for (var c in that.channels) {
314 for (var c in that.channels) {
315 if (that.channels[c] !== null) {
315 if (that.channels[c] !== null) {
316 that.channels[c].onclose = ws_closed_late;
316 that.channels[c].onclose = ws_closed_late;
317 }
317 }
318 }
318 }
319 }, 1000);
319 }, 1000);
320 this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this);
320 this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this);
321 this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this);
321 this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this);
322 this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this);
322 this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this);
323 };
323 };
324
324
325 /**
325 /**
326 * Handle a websocket entering the open state
326 * Handle a websocket entering the open state
327 * sends session and cookie authentication info as first message.
327 * sends session and cookie authentication info as first message.
328 * Once all sockets are open, signal the Kernel.status_started event.
328 * Once all sockets are open, signal the Kernel.status_started event.
329 * @method _ws_opened
329 * @method _ws_opened
330 */
330 */
331 Kernel.prototype._ws_opened = function (evt) {
331 Kernel.prototype._ws_opened = function (evt) {
332 // send the session id so the Session object Python-side
332 // send the session id so the Session object Python-side
333 // has the same identity
333 // has the same identity
334 evt.target.send(this.session_id + ':' + document.cookie);
334 evt.target.send(this.session_id + ':' + document.cookie);
335
335
336 if (this.is_connected()) {
336 if (this.is_connected()) {
337 // all events ready, trigger started event.
337 // all events ready, trigger started event.
338 this._kernel_connected();
338 this._kernel_connected();
339 }
339 }
340 };
340 };
341
341
342 Kernel.prototype._ws_closed = function(ws_url, early) {
342 Kernel.prototype._ws_closed = function(ws_url, early) {
343 this.stop_channels();
343 this.stop_channels();
344 this.events.trigger('status_disconnected.Kernel');
344 this.events.trigger('status_disconnected.Kernel');
345 if (!early) {
345 if (!early) {
346 this.reconnect();
346 this.reconnect();
347 } else {
347 } else {
348 console.log('WebSocket connection failed: ', ws_url);
348 console.log('WebSocket connection failed: ', ws_url);
349 this.events.trigger('early_disconnect.Kernel', ws_url);
349 this.events.trigger('early_disconnect.Kernel', ws_url);
350 }
350 }
351 };
351 };
352
352
353 /**
353 /**
354 * Stop the websocket channels.
354 * Stop the websocket channels.
355 * @method stop_channels
355 * @method stop_channels
356 */
356 */
357 Kernel.prototype.stop_channels = function () {
357 Kernel.prototype.stop_channels = function () {
358 var that = this;
359 var close = function (c) {
360 return function () {
361 if (that.channels[c].readyState === WebSocket.CLOSED) {
362 that.channels[c] = null;
363 }
364 };
365 };
358 for (var c in this.channels) {
366 for (var c in this.channels) {
359 if ( this.channels[c] !== null ) {
367 if ( this.channels[c] !== null ) {
360 this.channels[c].onclose = null;
368 this.channels[c].onclose = close(c);
361 this.channels[c].close();
369 this.channels[c].close();
362 this.channels[c] = null;
363 }
370 }
364 }
371 }
365 };
372 };
366
373
367 // Main public methods.
374 // Main public methods.
368
375
369 Kernel.prototype.is_connected = function () {
376 Kernel.prototype.is_connected = function () {
370 for (var c in this.channels) {
377 for (var c in this.channels) {
371 // if any channel is not ready, then we're not connected
378 // if any channel is not ready, then we're not connected
372 if (this.channels[c] === null) {
379 if (this.channels[c] === null) {
373 return false;
380 return false;
374 }
381 }
375 if (this.channels[c].readyState !== WebSocket.OPEN) {
382 if (this.channels[c].readyState !== WebSocket.OPEN) {
376 return false;
383 return false;
377 }
384 }
378 }
385 }
379 return true;
386 return true;
380 };
387 };
381
388
389 Kernel.prototype.is_fully_disconnected = function () {
390 for (var c in this.channels) {
391 if (this.channels[c] === null) {
392 return true;
393 }
394 }
395 return false;
396 };
397
382 // send a message on the Kernel's shell channel
398 // send a message on the Kernel's shell channel
383 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
399 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
384 if (!this.is_connected()) {
400 if (!this.is_connected()) {
385 throw new Error("kernel is not connected");
401 throw new Error("kernel is not connected");
386 }
402 }
387 var msg = this._get_msg(msg_type, content, metadata);
403 var msg = this._get_msg(msg_type, content, metadata);
388 this.channels.shell.send(JSON.stringify(msg));
404 this.channels.shell.send(JSON.stringify(msg));
389 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
405 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
390 return msg.header.msg_id;
406 return msg.header.msg_id;
391 };
407 };
392
408
393 /**
409 /**
394 * Get kernel info
410 * Get kernel info
395 *
411 *
396 * @param callback {function}
412 * @param callback {function}
397 * @method kernel_info
413 * @method kernel_info
398 *
414 *
399 * When calling this method, pass a callback function that expects one argument.
415 * When calling this method, pass a callback function that expects one argument.
400 * The callback will be passed the complete `kernel_info_reply` message documented
416 * The callback will be passed the complete `kernel_info_reply` message documented
401 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
417 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
402 */
418 */
403 Kernel.prototype.kernel_info = function (callback) {
419 Kernel.prototype.kernel_info = function (callback) {
404 var callbacks;
420 var callbacks;
405 if (callback) {
421 if (callback) {
406 callbacks = { shell : { reply : callback } };
422 callbacks = { shell : { reply : callback } };
407 }
423 }
408 return this.send_shell_message("kernel_info_request", {}, callbacks);
424 return this.send_shell_message("kernel_info_request", {}, callbacks);
409 };
425 };
410
426
411 /**
427 /**
412 * Get info on an object
428 * Get info on an object
413 *
429 *
414 * @param code {string}
430 * @param code {string}
415 * @param cursor_pos {integer}
431 * @param cursor_pos {integer}
416 * @param callback {function}
432 * @param callback {function}
417 * @method inspect
433 * @method inspect
418 *
434 *
419 * When calling this method, pass a callback function that expects one argument.
435 * When calling this method, pass a callback function that expects one argument.
420 * The callback will be passed the complete `inspect_reply` message documented
436 * The callback will be passed the complete `inspect_reply` message documented
421 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
437 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
422 */
438 */
423 Kernel.prototype.inspect = function (code, cursor_pos, callback) {
439 Kernel.prototype.inspect = function (code, cursor_pos, callback) {
424 var callbacks;
440 var callbacks;
425 if (callback) {
441 if (callback) {
426 callbacks = { shell : { reply : callback } };
442 callbacks = { shell : { reply : callback } };
427 }
443 }
428
444
429 var content = {
445 var content = {
430 code : code,
446 code : code,
431 cursor_pos : cursor_pos,
447 cursor_pos : cursor_pos,
432 detail_level : 0
448 detail_level : 0
433 };
449 };
434 return this.send_shell_message("inspect_request", content, callbacks);
450 return this.send_shell_message("inspect_request", content, callbacks);
435 };
451 };
436
452
437 /**
453 /**
438 * Execute given code into kernel, and pass result to callback.
454 * Execute given code into kernel, and pass result to callback.
439 *
455 *
440 * @async
456 * @async
441 * @method execute
457 * @method execute
442 * @param {string} code
458 * @param {string} code
443 * @param [callbacks] {Object} With the following keys (all optional)
459 * @param [callbacks] {Object} With the following keys (all optional)
444 * @param callbacks.shell.reply {function}
460 * @param callbacks.shell.reply {function}
445 * @param callbacks.shell.payload.[payload_name] {function}
461 * @param callbacks.shell.payload.[payload_name] {function}
446 * @param callbacks.iopub.output {function}
462 * @param callbacks.iopub.output {function}
447 * @param callbacks.iopub.clear_output {function}
463 * @param callbacks.iopub.clear_output {function}
448 * @param callbacks.input {function}
464 * @param callbacks.input {function}
449 * @param {object} [options]
465 * @param {object} [options]
450 * @param [options.silent=false] {Boolean}
466 * @param [options.silent=false] {Boolean}
451 * @param [options.user_expressions=empty_dict] {Dict}
467 * @param [options.user_expressions=empty_dict] {Dict}
452 * @param [options.allow_stdin=false] {Boolean} true|false
468 * @param [options.allow_stdin=false] {Boolean} true|false
453 *
469 *
454 * @example
470 * @example
455 *
471 *
456 * The options object should contain the options for the execute call. Its default
472 * The options object should contain the options for the execute call. Its default
457 * values are:
473 * values are:
458 *
474 *
459 * options = {
475 * options = {
460 * silent : true,
476 * silent : true,
461 * user_expressions : {},
477 * user_expressions : {},
462 * allow_stdin : false
478 * allow_stdin : false
463 * }
479 * }
464 *
480 *
465 * When calling this method pass a callbacks structure of the form:
481 * When calling this method pass a callbacks structure of the form:
466 *
482 *
467 * callbacks = {
483 * callbacks = {
468 * shell : {
484 * shell : {
469 * reply : execute_reply_callback,
485 * reply : execute_reply_callback,
470 * payload : {
486 * payload : {
471 * set_next_input : set_next_input_callback,
487 * set_next_input : set_next_input_callback,
472 * }
488 * }
473 * },
489 * },
474 * iopub : {
490 * iopub : {
475 * output : output_callback,
491 * output : output_callback,
476 * clear_output : clear_output_callback,
492 * clear_output : clear_output_callback,
477 * },
493 * },
478 * input : raw_input_callback
494 * input : raw_input_callback
479 * }
495 * }
480 *
496 *
481 * Each callback will be passed the entire message as a single arugment.
497 * Each callback will be passed the entire message as a single arugment.
482 * Payload handlers will be passed the corresponding payload and the execute_reply message.
498 * Payload handlers will be passed the corresponding payload and the execute_reply message.
483 */
499 */
484 Kernel.prototype.execute = function (code, callbacks, options) {
500 Kernel.prototype.execute = function (code, callbacks, options) {
485 var content = {
501 var content = {
486 code : code,
502 code : code,
487 silent : true,
503 silent : true,
488 store_history : false,
504 store_history : false,
489 user_expressions : {},
505 user_expressions : {},
490 allow_stdin : false
506 allow_stdin : false
491 };
507 };
492 callbacks = callbacks || {};
508 callbacks = callbacks || {};
493 if (callbacks.input !== undefined) {
509 if (callbacks.input !== undefined) {
494 content.allow_stdin = true;
510 content.allow_stdin = true;
495 }
511 }
496 $.extend(true, content, options);
512 $.extend(true, content, options);
497 this.events.trigger('execution_request.Kernel', {kernel: this, content:content});
513 this.events.trigger('execution_request.Kernel', {kernel: this, content:content});
498 return this.send_shell_message("execute_request", content, callbacks);
514 return this.send_shell_message("execute_request", content, callbacks);
499 };
515 };
500
516
501 /**
517 /**
502 * When calling this method, pass a function to be called with the `complete_reply` message
518 * When calling this method, pass a function to be called with the `complete_reply` message
503 * as its only argument when it arrives.
519 * as its only argument when it arrives.
504 *
520 *
505 * `complete_reply` is documented
521 * `complete_reply` is documented
506 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
522 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
507 *
523 *
508 * @method complete
524 * @method complete
509 * @param code {string}
525 * @param code {string}
510 * @param cursor_pos {integer}
526 * @param cursor_pos {integer}
511 * @param callback {function}
527 * @param callback {function}
512 *
528 *
513 */
529 */
514 Kernel.prototype.complete = function (code, cursor_pos, callback) {
530 Kernel.prototype.complete = function (code, cursor_pos, callback) {
515 var callbacks;
531 var callbacks;
516 if (callback) {
532 if (callback) {
517 callbacks = { shell : { reply : callback } };
533 callbacks = { shell : { reply : callback } };
518 }
534 }
519 var content = {
535 var content = {
520 code : code,
536 code : code,
521 cursor_pos : cursor_pos
537 cursor_pos : cursor_pos
522 };
538 };
523 return this.send_shell_message("complete_request", content, callbacks);
539 return this.send_shell_message("complete_request", content, callbacks);
524 };
540 };
525
541
526 Kernel.prototype.send_input_reply = function (input) {
542 Kernel.prototype.send_input_reply = function (input) {
527 if (!this.is_connected()) {
543 if (!this.is_connected()) {
528 throw new Error("kernel is not connected");
544 throw new Error("kernel is not connected");
529 }
545 }
530 var content = {
546 var content = {
531 value : input
547 value : input
532 };
548 };
533 this.events.trigger('input_reply.Kernel', {kernel: this, content:content});
549 this.events.trigger('input_reply.Kernel', {kernel: this, content:content});
534 var msg = this._get_msg("input_reply", content);
550 var msg = this._get_msg("input_reply", content);
535 this.channels.stdin.send(JSON.stringify(msg));
551 this.channels.stdin.send(JSON.stringify(msg));
536 return msg.header.msg_id;
552 return msg.header.msg_id;
537 };
553 };
538
554
539
555
540 // Reply handlers
556 // Reply handlers
541
557
542 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
558 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
543 this._iopub_handlers[msg_type] = callback;
559 this._iopub_handlers[msg_type] = callback;
544 };
560 };
545
561
546 Kernel.prototype.get_iopub_handler = function (msg_type) {
562 Kernel.prototype.get_iopub_handler = function (msg_type) {
547 // get iopub handler for a specific message type
563 // get iopub handler for a specific message type
548 return this._iopub_handlers[msg_type];
564 return this._iopub_handlers[msg_type];
549 };
565 };
550
566
551
567
552 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
568 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
553 // get callbacks for a specific message
569 // get callbacks for a specific message
554 if (msg_id == this.last_msg_id) {
570 if (msg_id == this.last_msg_id) {
555 return this.last_msg_callbacks;
571 return this.last_msg_callbacks;
556 } else {
572 } else {
557 return this._msg_callbacks[msg_id];
573 return this._msg_callbacks[msg_id];
558 }
574 }
559 };
575 };
560
576
561
577
562 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
578 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
563 if (this._msg_callbacks[msg_id] !== undefined ) {
579 if (this._msg_callbacks[msg_id] !== undefined ) {
564 delete this._msg_callbacks[msg_id];
580 delete this._msg_callbacks[msg_id];
565 }
581 }
566 };
582 };
567
583
568 Kernel.prototype._finish_shell = function (msg_id) {
584 Kernel.prototype._finish_shell = function (msg_id) {
569 var callbacks = this._msg_callbacks[msg_id];
585 var callbacks = this._msg_callbacks[msg_id];
570 if (callbacks !== undefined) {
586 if (callbacks !== undefined) {
571 callbacks.shell_done = true;
587 callbacks.shell_done = true;
572 if (callbacks.iopub_done) {
588 if (callbacks.iopub_done) {
573 this.clear_callbacks_for_msg(msg_id);
589 this.clear_callbacks_for_msg(msg_id);
574 }
590 }
575 }
591 }
576 };
592 };
577
593
578 Kernel.prototype._finish_iopub = function (msg_id) {
594 Kernel.prototype._finish_iopub = function (msg_id) {
579 var callbacks = this._msg_callbacks[msg_id];
595 var callbacks = this._msg_callbacks[msg_id];
580 if (callbacks !== undefined) {
596 if (callbacks !== undefined) {
581 callbacks.iopub_done = true;
597 callbacks.iopub_done = true;
582 if (callbacks.shell_done) {
598 if (callbacks.shell_done) {
583 this.clear_callbacks_for_msg(msg_id);
599 this.clear_callbacks_for_msg(msg_id);
584 }
600 }
585 }
601 }
586 };
602 };
587
603
588 /* Set callbacks for a particular message.
604 /* Set callbacks for a particular message.
589 * Callbacks should be a struct of the following form:
605 * Callbacks should be a struct of the following form:
590 * shell : {
606 * shell : {
591 *
607 *
592 * }
608 * }
593
609
594 */
610 */
595 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
611 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
596 this.last_msg_id = msg_id;
612 this.last_msg_id = msg_id;
597 if (callbacks) {
613 if (callbacks) {
598 // shallow-copy mapping, because we will modify it at the top level
614 // shallow-copy mapping, because we will modify it at the top level
599 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
615 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
600 cbcopy.shell = callbacks.shell;
616 cbcopy.shell = callbacks.shell;
601 cbcopy.iopub = callbacks.iopub;
617 cbcopy.iopub = callbacks.iopub;
602 cbcopy.input = callbacks.input;
618 cbcopy.input = callbacks.input;
603 cbcopy.shell_done = (!callbacks.shell);
619 cbcopy.shell_done = (!callbacks.shell);
604 cbcopy.iopub_done = (!callbacks.iopub);
620 cbcopy.iopub_done = (!callbacks.iopub);
605 } else {
621 } else {
606 this.last_msg_callbacks = {};
622 this.last_msg_callbacks = {};
607 }
623 }
608 };
624 };
609
625
610
626
611 Kernel.prototype._handle_shell_reply = function (e) {
627 Kernel.prototype._handle_shell_reply = function (e) {
612 var reply = $.parseJSON(e.data);
628 var reply = $.parseJSON(e.data);
613 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
629 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
614 var content = reply.content;
630 var content = reply.content;
615 var metadata = reply.metadata;
631 var metadata = reply.metadata;
616 var parent_id = reply.parent_header.msg_id;
632 var parent_id = reply.parent_header.msg_id;
617 var callbacks = this.get_callbacks_for_msg(parent_id);
633 var callbacks = this.get_callbacks_for_msg(parent_id);
618 if (!callbacks || !callbacks.shell) {
634 if (!callbacks || !callbacks.shell) {
619 return;
635 return;
620 }
636 }
621 var shell_callbacks = callbacks.shell;
637 var shell_callbacks = callbacks.shell;
622
638
623 // signal that shell callbacks are done
639 // signal that shell callbacks are done
624 this._finish_shell(parent_id);
640 this._finish_shell(parent_id);
625
641
626 if (shell_callbacks.reply !== undefined) {
642 if (shell_callbacks.reply !== undefined) {
627 shell_callbacks.reply(reply);
643 shell_callbacks.reply(reply);
628 }
644 }
629 if (content.payload && shell_callbacks.payload) {
645 if (content.payload && shell_callbacks.payload) {
630 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
646 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
631 }
647 }
632 };
648 };
633
649
634
650
635 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
651 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
636 var l = payloads.length;
652 var l = payloads.length;
637 // Payloads are handled by triggering events because we don't want the Kernel
653 // Payloads are handled by triggering events because we don't want the Kernel
638 // to depend on the Notebook or Pager classes.
654 // to depend on the Notebook or Pager classes.
639 for (var i=0; i<l; i++) {
655 for (var i=0; i<l; i++) {
640 var payload = payloads[i];
656 var payload = payloads[i];
641 var callback = payload_callbacks[payload.source];
657 var callback = payload_callbacks[payload.source];
642 if (callback) {
658 if (callback) {
643 callback(payload, msg);
659 callback(payload, msg);
644 }
660 }
645 }
661 }
646 };
662 };
647
663
648 Kernel.prototype._handle_status_message = function (msg) {
664 Kernel.prototype._handle_status_message = function (msg) {
649 var execution_state = msg.content.execution_state;
665 var execution_state = msg.content.execution_state;
650 var parent_id = msg.parent_header.msg_id;
666 var parent_id = msg.parent_header.msg_id;
651
667
652 // dispatch status msg callbacks, if any
668 // dispatch status msg callbacks, if any
653 var callbacks = this.get_callbacks_for_msg(parent_id);
669 var callbacks = this.get_callbacks_for_msg(parent_id);
654 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
670 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
655 try {
671 try {
656 callbacks.iopub.status(msg);
672 callbacks.iopub.status(msg);
657 } catch (e) {
673 } catch (e) {
658 console.log("Exception in status msg handler", e, e.stack);
674 console.log("Exception in status msg handler", e, e.stack);
659 }
675 }
660 }
676 }
661
677
662 if (execution_state === 'busy') {
678 if (execution_state === 'busy') {
663 this.events.trigger('status_busy.Kernel', {kernel: this});
679 this.events.trigger('status_busy.Kernel', {kernel: this});
664 } else if (execution_state === 'idle') {
680 } else if (execution_state === 'idle') {
665 // signal that iopub callbacks are (probably) done
681 // signal that iopub callbacks are (probably) done
666 // async output may still arrive,
682 // async output may still arrive,
667 // but only for the most recent request
683 // but only for the most recent request
668 this._finish_iopub(parent_id);
684 this._finish_iopub(parent_id);
669
685
670 // trigger status_idle event
686 // trigger status_idle event
671 this.events.trigger('status_idle.Kernel', {kernel: this});
687 this.events.trigger('status_idle.Kernel', {kernel: this});
672 } else if (execution_state === 'restarting') {
688 } else if (execution_state === 'restarting') {
673 // autorestarting is distinct from restarting,
689 // autorestarting is distinct from restarting,
674 // in that it means the kernel died and the server is restarting it.
690 // in that it means the kernel died and the server is restarting it.
675 // status_restarting sets the notification widget,
691 // status_restarting sets the notification widget,
676 // autorestart shows the more prominent dialog.
692 // autorestart shows the more prominent dialog.
677 this.events.trigger('status_autorestarting.Kernel', {kernel: this});
693 this.events.trigger('status_autorestarting.Kernel', {kernel: this});
678 this.events.trigger('status_restarting.Kernel', {kernel: this});
694 this.events.trigger('status_restarting.Kernel', {kernel: this});
679 } else if (execution_state === 'dead') {
695 } else if (execution_state === 'dead') {
680 this.stop_channels();
696 this.stop_channels();
681 this.events.trigger('status_dead.Kernel', {kernel: this});
697 this.events.trigger('status_dead.Kernel', {kernel: this});
682 this.events.trigger('status_restart_failed.Kernel', {kernel: this});
698 this.events.trigger('status_restart_failed.Kernel', {kernel: this});
683 }
699 }
684 };
700 };
685
701
686
702
687 // handle clear_output message
703 // handle clear_output message
688 Kernel.prototype._handle_clear_output = function (msg) {
704 Kernel.prototype._handle_clear_output = function (msg) {
689 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
705 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
690 if (!callbacks || !callbacks.iopub) {
706 if (!callbacks || !callbacks.iopub) {
691 return;
707 return;
692 }
708 }
693 var callback = callbacks.iopub.clear_output;
709 var callback = callbacks.iopub.clear_output;
694 if (callback) {
710 if (callback) {
695 callback(msg);
711 callback(msg);
696 }
712 }
697 };
713 };
698
714
699
715
700 // handle an output message (execute_result, display_data, etc.)
716 // handle an output message (execute_result, display_data, etc.)
701 Kernel.prototype._handle_output_message = function (msg) {
717 Kernel.prototype._handle_output_message = function (msg) {
702 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
718 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
703 if (!callbacks || !callbacks.iopub) {
719 if (!callbacks || !callbacks.iopub) {
704 return;
720 return;
705 }
721 }
706 var callback = callbacks.iopub.output;
722 var callback = callbacks.iopub.output;
707 if (callback) {
723 if (callback) {
708 callback(msg);
724 callback(msg);
709 }
725 }
710 };
726 };
711
727
712 // dispatch IOPub messages to respective handlers.
728 // dispatch IOPub messages to respective handlers.
713 // each message type should have a handler.
729 // each message type should have a handler.
714 Kernel.prototype._handle_iopub_message = function (e) {
730 Kernel.prototype._handle_iopub_message = function (e) {
715 var msg = $.parseJSON(e.data);
731 var msg = $.parseJSON(e.data);
716
732
717 var handler = this.get_iopub_handler(msg.header.msg_type);
733 var handler = this.get_iopub_handler(msg.header.msg_type);
718 if (handler !== undefined) {
734 if (handler !== undefined) {
719 handler(msg);
735 handler(msg);
720 }
736 }
721 };
737 };
722
738
723
739
724 Kernel.prototype._handle_input_request = function (e) {
740 Kernel.prototype._handle_input_request = function (e) {
725 var request = $.parseJSON(e.data);
741 var request = $.parseJSON(e.data);
726 var header = request.header;
742 var header = request.header;
727 var content = request.content;
743 var content = request.content;
728 var metadata = request.metadata;
744 var metadata = request.metadata;
729 var msg_type = header.msg_type;
745 var msg_type = header.msg_type;
730 if (msg_type !== 'input_request') {
746 if (msg_type !== 'input_request') {
731 console.log("Invalid input request!", request);
747 console.log("Invalid input request!", request);
732 return;
748 return;
733 }
749 }
734 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
750 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
735 if (callbacks) {
751 if (callbacks) {
736 if (callbacks.input) {
752 if (callbacks.input) {
737 callbacks.input(request);
753 callbacks.input(request);
738 }
754 }
739 }
755 }
740 };
756 };
741
757
742 // Backwards compatability.
758 // Backwards compatability.
743 IPython.Kernel = Kernel;
759 IPython.Kernel = Kernel;
744
760
745 return {'Kernel': Kernel};
761 return {'Kernel': Kernel};
746 });
762 });
General Comments 0
You need to be logged in to leave comments. Login now