##// END OF EJS Templates
Improvements to kernel.js...
MinRK -
Show More
@@ -16,7 +16,8 b''
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19
19 "use strict";
20
20 var utils = IPython.utils;
21 var utils = IPython.utils;
21
22
22 // Initialization and connection.
23 // Initialization and connection.
@@ -41,8 +42,10 b' var IPython = (function (IPython) {'
41 this.WebSocket = MozWebSocket;
42 this.WebSocket = MozWebSocket;
42 } else {
43 } else {
43 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.');
44 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.');
44 };
45 }
46
45 this.bind_events();
47 this.bind_events();
48 this.init_iopub_handlers();
46 };
49 };
47
50
48
51
@@ -61,12 +64,25 b' var IPython = (function (IPython) {'
61 return msg;
64 return msg;
62 };
65 };
63
66
64 Kernel.prototype.bind_events = function() {
67 Kernel.prototype.bind_events = function () {
65 var that = this;
68 var that = this;
66 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
69 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
67 that.send_input_reply(data);
70 that.send_input_reply(data);
68 });
71 });
69 }
72 };
73
74 // Initialize the iopub handlers
75
76 Kernel.prototype.init_iopub_handlers = function () {
77 var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
78 this._iopub_handlers = {};
79 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
80 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
81
82 for (var i=0; i < output_types.length; i++) {
83 this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
84 }
85 };
70
86
71 /**
87 /**
72 * Start the Python kernel
88 * Start the Python kernel
@@ -81,7 +97,7 b' var IPython = (function (IPython) {'
81 $.proxy(this._kernel_started, this),
97 $.proxy(this._kernel_started, this),
82 'json'
98 'json'
83 );
99 );
84 };
100 }
85 };
101 };
86
102
87 /**
103 /**
@@ -101,7 +117,7 b' var IPython = (function (IPython) {'
101 $.proxy(this._kernel_started, this),
117 $.proxy(this._kernel_started, this),
102 'json'
118 'json'
103 );
119 );
104 };
120 }
105 };
121 };
106
122
107
123
@@ -110,11 +126,11 b' var IPython = (function (IPython) {'
110 this.running = true;
126 this.running = true;
111 this.kernel_id = json.id;
127 this.kernel_id = json.id;
112 var ws_url = json.ws_url;
128 var ws_url = json.ws_url;
113 if (ws_url.match(/wss?:\/\//) == null) {
129 if (ws_url.match(/wss?:\/\//) === null) {
114 // trailing 's' in https will become wss for secure web sockets
130 // trailing 's' in https will become wss for secure web sockets
115 prot = location.protocol.replace('http', 'ws') + "//";
131 var prot = location.protocol.replace('http', 'ws') + "//";
116 ws_url = prot + location.host + ws_url;
132 ws_url = prot + location.host + ws_url;
117 };
133 }
118 this.ws_url = ws_url;
134 this.ws_url = ws_url;
119 this.kernel_url = utils.url_path_join(this.base_url, this.kernel_id);
135 this.kernel_url = utils.url_path_join(this.base_url, this.kernel_id);
120 this.start_channels();
136 this.start_channels();
@@ -176,7 +192,7 b' var IPython = (function (IPython) {'
176 }
192 }
177 }, 1000);
193 }, 1000);
178 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
194 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
179 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this);
195 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
180 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
196 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
181 };
197 };
182
198
@@ -208,14 +224,22 b' var IPython = (function (IPython) {'
208 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
224 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
209 for (var i=0; i < channels.length; i++) {
225 for (var i=0; i < channels.length; i++) {
210 if ( channels[i] !== null ) {
226 if ( channels[i] !== null ) {
211 channels[i].onclose = function (evt) {};
227 channels[i].onclose = null;
212 channels[i].close();
228 channels[i].close();
213 }
229 }
214 };
230 }
215 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
231 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
216 };
232 };
217
233
218 // Main public methods.
234 // Main public methods.
235
236 // send a message on the Kernel's shell channel
237 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks) {
238 var msg = this._get_msg(msg_type, content);
239 this.shell_channel.send(JSON.stringify(msg));
240 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
241 return msg.header.msg_id;
242 }
219
243
220 /**
244 /**
221 * Get info on object asynchronoulsy
245 * Get info on object asynchronoulsy
@@ -239,19 +263,15 b' var IPython = (function (IPython) {'
239 * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
263 * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
240 */
264 */
241 Kernel.prototype.object_info_request = function (objname, callbacks) {
265 Kernel.prototype.object_info_request = function (objname, callbacks) {
242 if(typeof(objname)!=null && objname!=null)
266 if (typeof(objname) !== null && objname !== null) {
243 {
244 var content = {
267 var content = {
245 oname : objname.toString(),
268 oname : objname.toString(),
246 detail_level : 0,
269 detail_level : 0,
247 };
270 };
248 var msg = this._get_msg("object_info_request", content);
271 return this.send_shell_message("object_info_request", content, callbacks);
249 this.shell_channel.send(JSON.stringify(msg));
250 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
251 return msg.header.msg_id;
252 }
272 }
253 return;
273 return;
254 }
274 };
255
275
256 /**
276 /**
257 * Execute given code into kernel, and pass result to callback.
277 * Execute given code into kernel, and pass result to callback.
@@ -323,12 +343,9 b' var IPython = (function (IPython) {'
323 if (callbacks.input_request !== undefined) {
343 if (callbacks.input_request !== undefined) {
324 content.allow_stdin = true;
344 content.allow_stdin = true;
325 }
345 }
326 $.extend(true, content, options)
346 $.extend(true, content, options);
327 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
347 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
328 var msg = this._get_msg("execute_request", content);
348 return this.send_shell_message("execute_request", content, callbacks);
329 this.shell_channel.send(JSON.stringify(msg));
330 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
331 return msg.header.msg_id;
332 };
349 };
333
350
334 /**
351 /**
@@ -357,10 +374,7 b' var IPython = (function (IPython) {'
357 block : null,
374 block : null,
358 cursor_pos : cursor_pos
375 cursor_pos : cursor_pos
359 };
376 };
360 var msg = this._get_msg("complete_request", content);
377 return this.send_shell_message("complete_request", content, callbacks);
361 this.shell_channel.send(JSON.stringify(msg));
362 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
363 return msg.header.msg_id;
364 };
378 };
365
379
366
380
@@ -368,7 +382,7 b' var IPython = (function (IPython) {'
368 if (this.running) {
382 if (this.running) {
369 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
383 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
370 $.post(this.kernel_url + "/interrupt");
384 $.post(this.kernel_url + "/interrupt");
371 };
385 }
372 };
386 };
373
387
374
388
@@ -380,7 +394,7 b' var IPython = (function (IPython) {'
380 type : "DELETE"
394 type : "DELETE"
381 };
395 };
382 $.ajax(this.kernel_url, settings);
396 $.ajax(this.kernel_url, settings);
383 };
397 }
384 };
398 };
385
399
386 Kernel.prototype.send_input_reply = function (input) {
400 Kernel.prototype.send_input_reply = function (input) {
@@ -396,9 +410,19 b' var IPython = (function (IPython) {'
396
410
397 // Reply handlers
411 // Reply handlers
398
412
413 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
414 this._iopub_handlers[msg_type] = callback;
415 };
416
417 Kernel.prototype.get_iopub_handler = function (msg_type) {
418 // get iopub handler for a specific message type
419 return this._iopub_handlers[msg_type];
420 };
421
422
399 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
423 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
400 var callbacks = this._msg_callbacks[msg_id];
424 // get callbacks for a specific message
401 return callbacks;
425 return this._msg_callbacks[msg_id];
402 };
426 };
403
427
404
428
@@ -410,7 +434,9 b' var IPython = (function (IPython) {'
410
434
411
435
412 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
436 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
413 this._msg_callbacks[msg_id] = callbacks || {};
437 if (callbacks) {
438 this._msg_callbacks[msg_id] = callbacks;
439 }
414 };
440 };
415
441
416
442
@@ -427,7 +453,7 b' var IPython = (function (IPython) {'
427 if (cb !== undefined) {
453 if (cb !== undefined) {
428 cb(content, metadata);
454 cb(content, metadata);
429 }
455 }
430 };
456 }
431
457
432 if (content.payload !== undefined) {
458 if (content.payload !== undefined) {
433 var payload = content.payload || [];
459 var payload = content.payload || [];
@@ -442,56 +468,70 b' var IPython = (function (IPython) {'
442 // to depend on the Notebook or Pager classes.
468 // to depend on the Notebook or Pager classes.
443 for (var i=0; i<l; i++) {
469 for (var i=0; i<l; i++) {
444 if (payload[i].source === 'page') {
470 if (payload[i].source === 'page') {
445 var data = {'text':payload[i].text}
471 var data = {'text':payload[i].text};
446 $([IPython.events]).trigger('open_with_text.Pager', data);
472 $([IPython.events]).trigger('open_with_text.Pager', data);
447 } else if (payload[i].source === 'set_next_input') {
473 } else if (payload[i].source === 'set_next_input') {
448 if (callbacks.set_next_input !== undefined) {
474 if (callbacks.set_next_input !== undefined) {
449 callbacks.set_next_input(payload[i].text)
475 callbacks.set_next_input(payload[i].text);
450 }
476 }
451 }
477 }
452 };
478 }
453 };
479 };
454
480
481 Kernel.prototype._handle_status_message = function (msg) {
482 var execution_state = msg.content.execution_state;
483 if (execution_state === 'busy') {
484 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
485 } else if (execution_state === 'idle') {
486 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
487 } else if (execution_state === 'restarting') {
488 // autorestarting is distinct from restarting,
489 // in that it means the kernel died and the server is restarting it.
490 // status_restarting sets the notification widget,
491 // autorestart shows the more prominent dialog.
492 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
493 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
494 } else if (execution_state === 'dead') {
495 this.stop_channels();
496 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
497 }
498 };
499
500
501 // handle clear_output message
502 Kernel.prototype._handle_clear_output = function (msg) {
503 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
504 if (callbacks === undefined) {
505 return;
506 }
507 var callback = callbacks['clear_output'];
508 if (callback !== undefined) {
509 callback(msg.content, msg.metadata);
510 }
511 };
455
512
456 Kernel.prototype._handle_iopub_reply = function (e) {
513
457 var reply = $.parseJSON(e.data);
514 // handle an output message (pyout, display_data, etc.)
458 var content = reply.content;
515 Kernel.prototype._handle_output_message = function (msg) {
459 var msg_type = reply.header.msg_type;
516 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
460 var metadata = reply.metadata;
517 if (callbacks === undefined) {
461 var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
462 if (msg_type !== 'status' && callbacks === undefined) {
463 // Message not from one of this notebook's cells and there are no
464 // callbacks to handle it.
465 return;
518 return;
466 }
519 }
467 var output_types = ['stream','display_data','pyout','pyerr'];
520 var callback = callbacks['output'];
468 if (output_types.indexOf(msg_type) >= 0) {
521 if (callback !== undefined) {
469 var cb = callbacks['output'];
522 callback(msg.header.msg_type, msg.content, msg.metadata);
470 if (cb !== undefined) {
523 }
471 cb(msg_type, content, metadata);
524 };
472 }
525
473 } else if (msg_type === 'status') {
526 // dispatch IOPub messages to respective handlers.
474 if (content.execution_state === 'busy') {
527 // each message type should have a handler.
475 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
528 Kernel.prototype._handle_iopub_message = function (e) {
476 } else if (content.execution_state === 'idle') {
529 var msg = $.parseJSON(e.data);
477 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
530
478 } else if (content.execution_state === 'restarting') {
531 var handler = this.get_iopub_handler(msg.header.msg_type);
479 // autorestarting is distinct from restarting,
532 if (handler !== undefined) {
480 // in that it means the kernel died and the server is restarting it.
533 handler(msg);
481 // status_restarting sets the notification widget,
534 }
482 // autorestart shows the more prominent dialog.
483 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
484 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
485 } else if (content.execution_state === 'dead') {
486 this.stop_channels();
487 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
488 };
489 } else if (msg_type === 'clear_output') {
490 var cb = callbacks['clear_output'];
491 if (cb !== undefined) {
492 cb(content, metadata);
493 }
494 };
495 };
535 };
496
536
497
537
@@ -511,7 +551,7 b' var IPython = (function (IPython) {'
511 if (cb !== undefined) {
551 if (cb !== undefined) {
512 cb(content, metadata);
552 cb(content, metadata);
513 }
553 }
514 };
554 }
515 };
555 };
516
556
517
557
General Comments 0
You need to be logged in to leave comments. Login now