##// END OF EJS Templates
Improvements to kernel.js...
MinRK -
Show More
@@ -16,7 +16,8
16 16 */
17 17
18 18 var IPython = (function (IPython) {
19
19 "use strict";
20
20 21 var utils = IPython.utils;
21 22
22 23 // Initialization and connection.
@@ -41,8 +42,10 var IPython = (function (IPython) {
41 42 this.WebSocket = MozWebSocket;
42 43 } else {
43 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 47 this.bind_events();
48 this.init_iopub_handlers();
46 49 };
47 50
48 51
@@ -61,12 +64,25 var IPython = (function (IPython) {
61 64 return msg;
62 65 };
63 66
64 Kernel.prototype.bind_events = function() {
67 Kernel.prototype.bind_events = function () {
65 68 var that = this;
66 69 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
67 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 88 * Start the Python kernel
@@ -81,7 +97,7 var IPython = (function (IPython) {
81 97 $.proxy(this._kernel_started, this),
82 98 'json'
83 99 );
84 };
100 }
85 101 };
86 102
87 103 /**
@@ -101,7 +117,7 var IPython = (function (IPython) {
101 117 $.proxy(this._kernel_started, this),
102 118 'json'
103 119 );
104 };
120 }
105 121 };
106 122
107 123
@@ -110,11 +126,11 var IPython = (function (IPython) {
110 126 this.running = true;
111 127 this.kernel_id = json.id;
112 128 var ws_url = json.ws_url;
113 if (ws_url.match(/wss?:\/\//) == null) {
129 if (ws_url.match(/wss?:\/\//) === null) {
114 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 132 ws_url = prot + location.host + ws_url;
117 };
133 }
118 134 this.ws_url = ws_url;
119 135 this.kernel_url = utils.url_path_join(this.base_url, this.kernel_id);
120 136 this.start_channels();
@@ -176,7 +192,7 var IPython = (function (IPython) {
176 192 }
177 193 }, 1000);
178 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 196 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
181 197 };
182 198
@@ -208,14 +224,22 var IPython = (function (IPython) {
208 224 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
209 225 for (var i=0; i < channels.length; i++) {
210 226 if ( channels[i] !== null ) {
211 channels[i].onclose = function (evt) {};
227 channels[i].onclose = null;
212 228 channels[i].close();
213 229 }
214 };
230 }
215 231 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
216 232 };
217 233
218 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 245 * Get info on object asynchronoulsy
@@ -239,19 +263,15 var IPython = (function (IPython) {
239 263 * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
240 264 */
241 265 Kernel.prototype.object_info_request = function (objname, callbacks) {
242 if(typeof(objname)!=null && objname!=null)
243 {
266 if (typeof(objname) !== null && objname !== null) {
244 267 var content = {
245 268 oname : objname.toString(),
246 269 detail_level : 0,
247 270 };
248 var msg = this._get_msg("object_info_request", content);
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;
271 return this.send_shell_message("object_info_request", content, callbacks);
252 272 }
253 273 return;
254 }
274 };
255 275
256 276 /**
257 277 * Execute given code into kernel, and pass result to callback.
@@ -323,12 +343,9 var IPython = (function (IPython) {
323 343 if (callbacks.input_request !== undefined) {
324 344 content.allow_stdin = true;
325 345 }
326 $.extend(true, content, options)
346 $.extend(true, content, options);
327 347 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
328 var msg = this._get_msg("execute_request", content);
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;
348 return this.send_shell_message("execute_request", content, callbacks);
332 349 };
333 350
334 351 /**
@@ -357,10 +374,7 var IPython = (function (IPython) {
357 374 block : null,
358 375 cursor_pos : cursor_pos
359 376 };
360 var msg = this._get_msg("complete_request", content);
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;
377 return this.send_shell_message("complete_request", content, callbacks);
364 378 };
365 379
366 380
@@ -368,7 +382,7 var IPython = (function (IPython) {
368 382 if (this.running) {
369 383 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
370 384 $.post(this.kernel_url + "/interrupt");
371 };
385 }
372 386 };
373 387
374 388
@@ -380,7 +394,7 var IPython = (function (IPython) {
380 394 type : "DELETE"
381 395 };
382 396 $.ajax(this.kernel_url, settings);
383 };
397 }
384 398 };
385 399
386 400 Kernel.prototype.send_input_reply = function (input) {
@@ -396,9 +410,19 var IPython = (function (IPython) {
396 410
397 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 423 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
400 var callbacks = this._msg_callbacks[msg_id];
401 return callbacks;
424 // get callbacks for a specific message
425 return this._msg_callbacks[msg_id];
402 426 };
403 427
404 428
@@ -410,7 +434,9 var IPython = (function (IPython) {
410 434
411 435
412 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 var IPython = (function (IPython) {
427 453 if (cb !== undefined) {
428 454 cb(content, metadata);
429 455 }
430 };
456 }
431 457
432 458 if (content.payload !== undefined) {
433 459 var payload = content.payload || [];
@@ -442,56 +468,70 var IPython = (function (IPython) {
442 468 // to depend on the Notebook or Pager classes.
443 469 for (var i=0; i<l; i++) {
444 470 if (payload[i].source === 'page') {
445 var data = {'text':payload[i].text}
471 var data = {'text':payload[i].text};
446 472 $([IPython.events]).trigger('open_with_text.Pager', data);
447 473 } else if (payload[i].source === 'set_next_input') {
448 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) {
457 var reply = $.parseJSON(e.data);
458 var content = reply.content;
459 var msg_type = reply.header.msg_type;
460 var metadata = reply.metadata;
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.
513
514 // handle an output message (pyout, display_data, etc.)
515 Kernel.prototype._handle_output_message = function (msg) {
516 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
517 if (callbacks === undefined) {
465 518 return;
466 519 }
467 var output_types = ['stream','display_data','pyout','pyerr'];
468 if (output_types.indexOf(msg_type) >= 0) {
469 var cb = callbacks['output'];
470 if (cb !== undefined) {
471 cb(msg_type, content, metadata);
472 }
473 } else if (msg_type === 'status') {
474 if (content.execution_state === 'busy') {
475 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
476 } else if (content.execution_state === 'idle') {
477 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
478 } else if (content.execution_state === 'restarting') {
479 // autorestarting is distinct from restarting,
480 // in that it means the kernel died and the server is restarting it.
481 // status_restarting sets the notification widget,
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 };
520 var callback = callbacks['output'];
521 if (callback !== undefined) {
522 callback(msg.header.msg_type, msg.content, msg.metadata);
523 }
524 };
525
526 // dispatch IOPub messages to respective handlers.
527 // each message type should have a handler.
528 Kernel.prototype._handle_iopub_message = function (e) {
529 var msg = $.parseJSON(e.data);
530
531 var handler = this.get_iopub_handler(msg.header.msg_type);
532 if (handler !== undefined) {
533 handler(msg);
534 }
495 535 };
496 536
497 537
@@ -511,7 +551,7 var IPython = (function (IPython) {
511 551 if (cb !== undefined) {
512 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