##// END OF EJS Templates
move comm.js to base
move comm.js to base

File last commit:

r13214:d2ffe72c
r13216:a681d2b0
Show More
kernel.js
573 lines | 19.1 KiB | application/javascript | JavascriptLexer
Brian E. Granger
More review changes....
r4609 //----------------------------------------------------------------------------
// Copyright (C) 2008-2011 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
//============================================================================
// Kernel
//============================================================================
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* @module IPython
* @namespace IPython
* @submodule Kernel
*/
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var IPython = (function (IPython) {
MinRK
Improvements to kernel.js...
r13187 "use strict";
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var utils = IPython.utils;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // Initialization and connection.
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* A Kernel Class to communicate with the Python kernel
* @Class Kernel
*/
Zachary Sailer
session manager restructuring...
r13035 var Kernel = function (base_url) {
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.kernel_id = null;
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 this.shell_channel = null;
this.iopub_channel = null;
MinRK
add stdin to notebook...
r10366 this.stdin_channel = null;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 this.base_url = base_url;
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 this.running = false;
MinRK
fixup bad rebase
r13102 this.username = "username";
Zachary Sailer
session manager restructuring...
r13035 this.session_id = utils.uuid();
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 this._msg_callbacks = {};
Brian E. Granger
Better WebSocket detection added.
r4612 if (typeof(WebSocket) !== 'undefined') {
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 this.WebSocket = WebSocket;
Brian E. Granger
Better WebSocket detection added.
r4612 } else if (typeof(MozWebSocket) !== 'undefined') {
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 this.WebSocket = MozWebSocket;
Brian E. Granger
Adding code to handle MozWebSocket for FF 6.
r4611 } else {
MinRK
alert client on failed and lost web socket connections...
r5253 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.');
MinRK
Improvements to kernel.js...
r13187 }
MinRK
bind kernel events in Kernel.bind_events...
r11611 this.bind_events();
MinRK
Improvements to kernel.js...
r13187 this.init_iopub_handlers();
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype._get_msg = function (msg_type, content) {
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var msg = {
header : {
msg_id : utils.uuid(),
MinRK
fix undefined 'session_id' member in kernel.js
r4694 username : this.username,
Zachary Sailer
session manager restructuring...
r13035 session : this.session_id,
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 msg_type : msg_type
},
MinRK
add metadata to javascript msg spec implementation
r7958 metadata : {},
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 content : content,
parent_header : {}
};
return msg;
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
MinRK
bind kernel events in Kernel.bind_events...
r11611
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.bind_events = function () {
MinRK
bind kernel events in Kernel.bind_events...
r11611 var that = this;
$([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
that.send_input_reply(data);
});
MinRK
Improvements to kernel.js...
r13187 };
// Initialize the iopub handlers
Kernel.prototype.init_iopub_handlers = function () {
var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
this._iopub_handlers = {};
this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
for (var i=0; i < output_types.length; i++) {
this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
}
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* Start the Python kernel
* @method start
*/
MinRK
review pass on multidir js
r13103 Kernel.prototype.start = function (params) {
params = params || {};
if (!this.running) {
var qs = $.param(params);
var url = this.base_url + '?' + qs;
$.post(url,
$.proxy(this._kernel_started, this),
'json'
);
MinRK
Improvements to kernel.js...
r13187 }
MinRK
review pass on multidir js
r13103 };
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* Restart the python kernel.
*
* Emit a 'status_restarting.Kernel' event with
* the current object as parameter
*
* @method restart
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype.restart = function () {
Matthias BUSSONNIER
change all trigger parameter to (event,data)
r8677 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 if (this.running) {
this.stop_channels();
MinRK
review pass on multidir js
r13103 var url = utils.url_path_join(this.kernel_url, "restart");
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 $.post(url,
MinRK
review pass on multidir js
r13103 $.proxy(this._kernel_started, this),
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 'json'
);
MinRK
Improvements to kernel.js...
r13187 }
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype._kernel_started = function (json) {
Zachary Sailer
change standard money keys
r13015 console.log("Kernel started: ", json.id);
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 this.running = true;
Zachary Sailer
change standard money keys
r13015 this.kernel_id = json.id;
MinRK
switch default ws_url logic to js side...
r10807 var ws_url = json.ws_url;
MinRK
Improvements to kernel.js...
r13187 if (ws_url.match(/wss?:\/\//) === null) {
Paul Ivanov
fix cell execution in firefox, closes #3447...
r10979 // trailing 's' in https will become wss for secure web sockets
MinRK
Improvements to kernel.js...
r13187 var prot = location.protocol.replace('http', 'ws') + "//";
Paul Ivanov
fix cell execution in firefox, closes #3447...
r10979 ws_url = prot + location.host + ws_url;
MinRK
Improvements to kernel.js...
r13187 }
MinRK
switch default ws_url logic to js side...
r10807 this.ws_url = ws_url;
MinRK
review pass on multidir js
r13103 this.kernel_url = utils.url_path_join(this.base_url, this.kernel_id);
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 this.start_channels();
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
Brian E. Granger
Refactoring WebSocket connection failure logic....
r9222 Kernel.prototype._websocket_closed = function(ws_url, early) {
this.stop_channels();
MinRK
review pass on multidir js
r13103 $([IPython.events]).trigger('websocket_closed.Kernel',
Brian E. Granger
Refactoring WebSocket connection failure logic....
r9222 {ws_url: ws_url, kernel: this, early: early}
);
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* Start the `shell`and `iopub` channels.
* Will stop and restart them if they already exist.
*
* @method start_channels
*/
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 Kernel.prototype.start_channels = function () {
MinRK
alert client on failed and lost web socket connections...
r5253 var that = this;
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 this.stop_channels();
Brian E. Granger
WebSocket url is now passed to browser when a kernel is started.
r4572 var ws_url = this.ws_url + this.kernel_url;
Brian E. Granger
Refactoring WebSocket connection failure logic....
r9222 console.log("Starting WebSockets:", ws_url);
Brian E. Granger
Adding code to handle MozWebSocket for FF 6.
r4611 this.shell_channel = new this.WebSocket(ws_url + "/shell");
MinRK
add stdin to notebook...
r10366 this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
Brian E. Granger
Adding code to handle MozWebSocket for FF 6.
r4611 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
MinRK
trigger `Kernel.status_started` after websockets open...
r12254
MinRK
alert client on failed and lost web socket connections...
r5253 var already_called_onclose = false; // only alert once
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var ws_closed_early = function(evt){
MinRK
alert client on failed and lost web socket connections...
r5253 if (already_called_onclose){
return;
}
already_called_onclose = true;
if ( ! evt.wasClean ){
MinRK
use jQuery dialog instead of alert()
r5255 that._websocket_closed(ws_url, true);
MinRK
alert client on failed and lost web socket connections...
r5253 }
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var ws_closed_late = function(evt){
MinRK
alert client on failed and lost web socket connections...
r5253 if (already_called_onclose){
return;
}
already_called_onclose = true;
if ( ! evt.wasClean ){
MinRK
use jQuery dialog instead of alert()
r5255 that._websocket_closed(ws_url, false);
MinRK
alert client on failed and lost web socket connections...
r5253 }
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
MinRK
add stdin to notebook...
r10366 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
for (var i=0; i < channels.length; i++) {
MinRK
trigger `Kernel.status_started` after websockets open...
r12254 channels[i].onopen = $.proxy(this._ws_opened, this);
MinRK
add stdin to notebook...
r10366 channels[i].onclose = ws_closed_early;
}
MinRK
alert client on failed and lost web socket connections...
r5253 // switch from early-close to late-close message after 1s
Brian E. Granger
Refactoring WebSocket connection failure logic....
r9222 setTimeout(function() {
MinRK
add stdin to notebook...
r10366 for (var i=0; i < channels.length; i++) {
if (channels[i] !== null) {
channels[i].onclose = ws_closed_late;
}
Brian E. Granger
Refactoring WebSocket connection failure logic....
r9222 }
MinRK
alert client on failed and lost web socket connections...
r5253 }, 1000);
MinRK
add stdin to notebook...
r10366 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
MinRK
Improvements to kernel.js...
r13187 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
MinRK
add stdin to notebook...
r10366 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
MinRK
trigger `Kernel.status_started` after websockets open...
r12254 * Handle a websocket entering the open state
* sends session and cookie authentication info as first message.
* Once all sockets are open, signal the Kernel.status_started event.
* @method _ws_opened
*/
Kernel.prototype._ws_opened = function (evt) {
// send the session id so the Session object Python-side
// has the same identity
evt.target.send(this.session_id + ':' + document.cookie);
var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
for (var i=0; i < channels.length; i++) {
// if any channel is not ready, don't trigger event.
if ( !channels[i].readyState ) return;
}
// all events ready, trigger started event.
$([IPython.events]).trigger('status_started.Kernel', {kernel: this});
};
/**
* Stop the websocket channels.
Matthias BUSSONNIER
start docummenting kernel
r8768 * @method stop_channels
*/
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 Kernel.prototype.stop_channels = function () {
MinRK
add stdin to notebook...
r10366 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
for (var i=0; i < channels.length; i++) {
if ( channels[i] !== null ) {
MinRK
Improvements to kernel.js...
r13187 channels[i].onclose = null;
MinRK
add stdin to notebook...
r10366 channels[i].close();
}
MinRK
Improvements to kernel.js...
r13187 }
MinRK
add stdin to notebook...
r10366 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // Main public methods.
MinRK
Improvements to kernel.js...
r13187
// send a message on the Kernel's shell channel
Kernel.prototype.send_shell_message = function (msg_type, content, callbacks) {
var msg = this._get_msg(msg_type, content);
this.shell_channel.send(JSON.stringify(msg));
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
return msg.header.msg_id;
MinRK
refactor js callbacks...
r13207 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * Get info on an object
Matthias BUSSONNIER
start docummenting kernel
r8768 *
* @param objname {string}
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * @param callback {function}
* @method object_info
Matthias BUSSONNIER
start docummenting kernel
r8768 *
MinRK
update callback structure in js commands
r13212 * When calling this method, pass a callback function that expects one argument.
* The callback will be passed the complete `object_info_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
Matthias BUSSONNIER
start docummenting kernel
r8768 */
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 Kernel.prototype.object_info = function (objname, callback) {
var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
MinRK
Improvements to kernel.js...
r13187 if (typeof(objname) !== null && objname !== null) {
Matthias BUSSONNIER
handle null objectname on tooltip
r5409 var content = {
oname : objname.toString(),
MinRK
comply with the message spec in object_info requests...
r11694 detail_level : 0,
Matthias BUSSONNIER
handle null objectname on tooltip
r5409 };
MinRK
Improvements to kernel.js...
r13187 return this.send_shell_message("object_info_request", content, callbacks);
Matthias BUSSONNIER
handle null objectname on tooltip
r5409 }
return;
MinRK
Improvements to kernel.js...
r13187 };
Matthias BUSSONNIER
Add Tootip to notebook....
r5397
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
* Execute given code into kernel, and pass result to callback.
*
* @async
* @method execute
* @param {string} code
MinRK
update callback structure in js commands
r13212 * @param [callbacks] {Object} With the following keys (all optional)
* @param callbacks.shell.reply {function}
* @param callbacks.shell.payload.[payload_name] {function}
* @param callbacks.iopub.output {function}
* @param callbacks.iopub.clear_output {function}
* @param callbacks.input {function}
Matthias BUSSONNIER
start docummenting kernel
r8768 * @param {object} [options]
* @param [options.silent=false] {Boolean}
* @param [options.user_expressions=empty_dict] {Dict}
* @param [options.user_variables=empty_list] {List od Strings}
* @param [options.allow_stdin=false] {Boolean} true|false
*
* @example
*
* The options object should contain the options for the execute call. Its default
* values are:
*
* options = {
* silent : true,
* user_variables : [],
* user_expressions : {},
* allow_stdin : false
* }
*
* When calling this method pass a callbacks structure of the form:
*
* callbacks = {
MinRK
update callback structure in js commands
r13212 * shell : {
* reply : execute_reply_callback,
* payload : {
* set_next_input : set_next_input_callback,
* }
* },
* iopub : {
* output : output_callback,
* clear_output : clear_output_callback,
* },
* input : raw_input_callback
Matthias BUSSONNIER
start docummenting kernel
r8768 * }
*
MinRK
update callback structure in js commands
r13212 * Each callback will be passed the entire message as a single arugment.
* Payload handlers will be passed the corresponding payload and the execute_reply message.
Matthias BUSSONNIER
start docummenting kernel
r8768 */
Brian Granger
Adding options to Kernel.execute with a default of silent=true.
r7176 Kernel.prototype.execute = function (code, callbacks, options) {
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var content = {
code : code,
Brian Granger
Adding options to Kernel.execute with a default of silent=true.
r7176 silent : true,
MinRK
add missing store_history key to Notebook execute_requests
r11857 store_history : false,
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 user_variables : [],
MinRK
fix missing trailing comma in kernel.js
r4975 user_expressions : {},
MinRK
use inline raw_input instead of a dialog
r10368 allow_stdin : false
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Matthias BUSSONNIER
fix callbacks as optional in js kernel.execute...
r10594 callbacks = callbacks || {};
MinRK
refactor js callbacks...
r13207 if (callbacks.input !== undefined) {
MinRK
use inline raw_input instead of a dialog
r10368 content.allow_stdin = true;
}
MinRK
Improvements to kernel.js...
r13187 $.extend(true, content, options);
Matthias BUSSONNIER
change all trigger parameter to (event,data)
r8677 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
MinRK
Improvements to kernel.js...
r13187 return this.send_shell_message("execute_request", content, callbacks);
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
MinRK
update callback structure in js commands
r13212 * When calling this method, pass a function to be called with the `complete_reply` message
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * as its only argument when it arrives.
Matthias BUSSONNIER
start docummenting kernel
r8768 *
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * `complete_reply` is documented
Matthias BUSSONNIER
start docummenting kernel
r8768 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
*
* @method complete
* @param line {integer}
* @param cursor_pos {integer}
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * @param callback {function}
Matthias BUSSONNIER
start docummenting kernel
r8768 *
*/
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 Kernel.prototype.complete = function (line, cursor_pos, callback) {
var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
Brian Granger
Added complete method of JS kernel object.
r4388 var content = {
text : '',
line : line,
MinRK
add missing block key in complete_request
r11695 block : null,
Brian Granger
Added complete method of JS kernel object.
r4388 cursor_pos : cursor_pos
};
MinRK
Improvements to kernel.js...
r13187 return this.send_shell_message("complete_request", content, callbacks);
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Brian Granger
Added complete method of JS kernel object.
r4388
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 Kernel.prototype.interrupt = function () {
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 if (this.running) {
Matthias BUSSONNIER
change all trigger parameter to (event,data)
r8677 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 $.post(this.kernel_url + "/interrupt");
MinRK
Improvements to kernel.js...
r13187 }
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349 };
Brian E. Granger
Using beforeunload to save at exit and kill the kernel.
r4496 Kernel.prototype.kill = function () {
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 if (this.running) {
this.running = false;
var settings = {
cache : false,
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 type : "DELETE"
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 };
$.ajax(this.kernel_url, settings);
MinRK
Improvements to kernel.js...
r13187 }
Brian E. Granger
Using beforeunload to save at exit and kill the kernel.
r4496 };
MinRK
use inline raw_input instead of a dialog
r10368 Kernel.prototype.send_input_reply = function (input) {
MinRK
add stdin to notebook...
r10366 var content = {
value : input,
};
$([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
var msg = this._get_msg("input_reply", content);
this.stdin_channel.send(JSON.stringify(msg));
return msg.header.msg_id;
};
// Reply handlers
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
this._iopub_handlers[msg_type] = callback;
};
Kernel.prototype.get_iopub_handler = function (msg_type) {
// get iopub handler for a specific message type
return this._iopub_handlers[msg_type];
};
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
MinRK
Improvements to kernel.js...
r13187 // get callbacks for a specific message
return this._msg_callbacks[msg_id];
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
MinRK
add Kernel.clear_callbacks_for_msg
r12555 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
if (this._msg_callbacks[msg_id] !== undefined ) {
delete this._msg_callbacks[msg_id];
}
};
MinRK
refactor js callbacks...
r13207
/* Set callbacks for a particular message.
* Callbacks should be a struct of the following form:
* shell : {
*
* }
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
MinRK
Improvements to kernel.js...
r13187 if (callbacks) {
MinRK
refactor js callbacks...
r13207 // shallow-copy mapping, because we will modify it at the top level
var cbcopy = this._msg_callbacks[msg_id] = {};
cbcopy.shell = callbacks.shell;
cbcopy.iopub = callbacks.iopub;
cbcopy.input = callbacks.input;
this._msg_callbacks[msg_id] = cbcopy;
MinRK
Improvements to kernel.js...
r13187 }
MinRK
add Kernel.clear_callbacks_for_msg
r12555 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
Kernel.prototype._handle_shell_reply = function (e) {
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var reply = $.parseJSON(e.data);
Matthias BUSSONNIER
change all trigger parameter to (event,data)
r8677 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var content = reply.content;
Jason Grout
Add an optional metadata attribute to all messages and add a session-level default metadata attribute.
r7952 var metadata = reply.metadata;
MinRK
refactor js callbacks...
r13207 var parent_id = reply.parent_header.msg_id;
var callbacks = this.get_callbacks_for_msg(parent_id);
if (!callbacks || !callbacks.shell) {
return;
MinRK
Improvements to kernel.js...
r13187 }
MinRK
refactor js callbacks...
r13207 var shell_callbacks = callbacks.shell;
// clear callbacks on shell
delete callbacks.shell;
delete callbacks.input;
if (!callbacks.iopub) {
this.clear_callbacks_for_msg(parent_id);
}
if (shell_callbacks.reply !== undefined) {
shell_callbacks.reply(reply);
}
if (content.payload && shell_callbacks.payload) {
this._handle_payloads(content.payload, shell_callbacks.payload, reply);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 }
};
MinRK
refactor js callbacks...
r13207 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
var l = payloads.length;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // Payloads are handled by triggering events because we don't want the Kernel
// to depend on the Notebook or Pager classes.
for (var i=0; i<l; i++) {
MinRK
refactor js callbacks...
r13207 var payload = payloads[i];
var callback = payload_callbacks[payload.source];
if (callback) {
callback(payload, msg);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 }
MinRK
Improvements to kernel.js...
r13187 }
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype._handle_status_message = function (msg) {
var execution_state = msg.content.execution_state;
if (execution_state === 'busy') {
$([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
} else if (execution_state === 'idle') {
MinRK
refactor js callbacks...
r13207 // clear callbacks
var parent_id = msg.parent_header.msg_id;
var callbacks = this.get_callbacks_for_msg(parent_id);
if (callbacks !== undefined) {
delete callbacks.iopub;
delete callbacks.input;
if (!callbacks.shell) {
this.clear_callbacks_for_msg(parent_id);
}
}
MinRK
Improvements to kernel.js...
r13187 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
} else if (execution_state === 'restarting') {
// autorestarting is distinct from restarting,
// in that it means the kernel died and the server is restarting it.
// status_restarting sets the notification widget,
// autorestart shows the more prominent dialog.
$([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
$([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
} else if (execution_state === 'dead') {
this.stop_channels();
$([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
}
};
// handle clear_output message
Kernel.prototype._handle_clear_output = function (msg) {
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
MinRK
refactor js callbacks...
r13207 if (!callbacks || !callbacks.iopub) {
MinRK
Improvements to kernel.js...
r13187 return;
}
MinRK
get clear_output callback properly
r13214 var callback = callbacks.iopub.clear_output;
MinRK
refactor js callbacks...
r13207 if (callback) {
callback(msg);
MinRK
Improvements to kernel.js...
r13187 }
};
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
MinRK
Improvements to kernel.js...
r13187
// handle an output message (pyout, display_data, etc.)
Kernel.prototype._handle_output_message = function (msg) {
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
MinRK
refactor js callbacks...
r13207 if (!callbacks || !callbacks.iopub) {
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 return;
}
MinRK
refactor js callbacks...
r13207 var callback = callbacks.iopub.output;
if (callback) {
callback(msg);
MinRK
Improvements to kernel.js...
r13187 }
};
// dispatch IOPub messages to respective handlers.
// each message type should have a handler.
Kernel.prototype._handle_iopub_message = function (e) {
var msg = $.parseJSON(e.data);
var handler = this.get_iopub_handler(msg.header.msg_type);
if (handler !== undefined) {
handler(msg);
}
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
MinRK
add stdin to notebook...
r10366 Kernel.prototype._handle_input_request = function (e) {
var request = $.parseJSON(e.data);
var header = request.header;
var content = request.content;
var metadata = request.metadata;
var msg_type = header.msg_type;
if (msg_type !== 'input_request') {
console.log("Invalid input request!", request);
return;
}
MinRK
use inline raw_input instead of a dialog
r10368 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
MinRK
refactor js callbacks...
r13207 if (callbacks) {
if (callbacks.input) {
callbacks.input(request);
MinRK
use inline raw_input instead of a dialog
r10368 }
MinRK
Improvements to kernel.js...
r13187 }
MinRK
add stdin to notebook...
r10366 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.Kernel = Kernel;
return IPython;
}(IPython));