kernel.js
1017 lines
| 33.4 KiB
| application/javascript
|
JavascriptLexer
|
r16359 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r4349 | |||
|
r17198 | define([ | ||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
'services/kernels/js/comm', | ||||
'widgets/js/init', | ||||
|
r17202 | ], function(IPython, $, utils, comm, widgetmanager) { | ||
|
r13187 | "use strict"; | ||
|
r4352 | |||
|
r8768 | /** | ||
|
r18217 | * A Kernel class to communicate with the Python kernel. This | ||
* should generally not be constructed directly, but be created | ||||
* by. the `Session` object. Once created, this object should be | ||||
* used to communicate with the kernel. | ||||
* | ||||
* @class Kernel | ||||
* @param {string} kernel_service_url - the URL to access the kernel REST api | ||||
* @param {string} ws_url - the websockets URL | ||||
* @param {Notebook} notebook - notebook object | ||||
* @param {string} name - the kernel type (e.g. python3) | ||||
|
r8768 | */ | ||
|
r18218 | var Kernel = function (kernel_service_url, ws_url, notebook, name) { | ||
|
r17200 | this.events = notebook.events; | ||
|
r18201 | |||
|
r18218 | this.id = null; | ||
|
r18201 | this.name = name; | ||
|
r18208 | this.channels = { | ||
'shell': null, | ||||
'iopub': null, | ||||
'stdin': null | ||||
}; | ||||
|
r18201 | |||
|
r15242 | this.kernel_service_url = kernel_service_url; | ||
|
r18218 | this.kernel_url = null; | ||
|
r17308 | this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl"); | ||
|
r17303 | if (!this.ws_url) { | ||
// trailing 's' in https will become wss for secure web sockets | ||||
this.ws_url = location.protocol.replace('http', 'ws') + "//" + location.host; | ||||
} | ||||
|
r18201 | |||
|
r13102 | this.username = "username"; | ||
|
r13035 | this.session_id = utils.uuid(); | ||
|
r7168 | this._msg_callbacks = {}; | ||
|
r4612 | if (typeof(WebSocket) !== 'undefined') { | ||
|
r5479 | this.WebSocket = WebSocket; | ||
|
r4612 | } else if (typeof(MozWebSocket) !== 'undefined') { | ||
|
r5479 | this.WebSocket = MozWebSocket; | ||
|
r4611 | } else { | ||
|
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.'); | ||
|
r13187 | } | ||
|
r11611 | this.bind_events(); | ||
|
r13187 | this.init_iopub_handlers(); | ||
|
r17198 | this.comm_manager = new comm.CommManager(this); | ||
|
r17202 | this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook); | ||
|
r16359 | |||
this.last_msg_id = null; | ||||
this.last_msg_callbacks = {}; | ||||
|
r18233 | |||
this._autorestart_attempt = 0; | ||||
|
r18236 | this._reconnect_attempt = 0; | ||
|
r4352 | }; | ||
|
r18217 | /** | ||
* @function _get_msg | ||||
*/ | ||||
|
r13217 | Kernel.prototype._get_msg = function (msg_type, content, metadata) { | ||
|
r4352 | var msg = { | ||
header : { | ||||
msg_id : utils.uuid(), | ||||
|
r4694 | username : this.username, | ||
|
r13035 | session : this.session_id, | ||
|
r16666 | msg_type : msg_type, | ||
version : "5.0" | ||||
|
r4352 | }, | ||
|
r13217 | metadata : metadata || {}, | ||
|
r4352 | content : content, | ||
parent_header : {} | ||||
}; | ||||
return msg; | ||||
|
r5479 | }; | ||
|
r18217 | |||
/** | ||||
* @function bind_events | ||||
*/ | ||||
|
r13187 | Kernel.prototype.bind_events = function () { | ||
|
r11611 | var that = this; | ||
|
r17198 | this.events.on('send_input_reply.Kernel', function(evt, data) { | ||
|
r11611 | that.send_input_reply(data); | ||
}); | ||||
|
r18224 | |||
var record_status = function (evt) { | ||||
console.log('Kernel: ' + evt.type + ' (' + that.id + ')'); | ||||
}; | ||||
|
r18230 | this.events.on('kernel_created.Kernel', record_status); | ||
|
r18238 | this.events.on('kernel_reconnecting.Kernel', record_status); | ||
this.events.on('kernel_connected.Kernel', record_status); | ||||
this.events.on('kernel_starting.Kernel', record_status); | ||||
this.events.on('kernel_restarting.Kernel', record_status); | ||||
this.events.on('kernel_autorestarting.Kernel', record_status); | ||||
this.events.on('kernel_interrupting.Kernel', record_status); | ||||
this.events.on('kernel_disconnected.Kernel', record_status); | ||||
|
r18226 | // these are commented out because they are triggered a lot, but can | ||
// be uncommented for debugging purposes | ||||
|
r18238 | //this.events.on('kernel_idle.Kernel', record_status); | ||
//this.events.on('kernel_busy.Kernel', record_status); | ||||
this.events.on('kernel_ready.Kernel', record_status); | ||||
this.events.on('kernel_killed.Kernel', record_status); | ||||
|
r18224 | this.events.on('kernel_dead.Kernel', record_status); | ||
|
r18233 | |||
|
r18238 | this.events.on('kernel_ready.Kernel', function () { | ||
|
r18233 | that._autorestart_attempt = 0; | ||
}); | ||||
|
r18238 | this.events.on('kernel_connected.Kernel', function () { | ||
|
r18236 | that._reconnect_attempt = 0; | ||
}); | ||||
|
r13187 | }; | ||
|
r18217 | |||
/** | ||||
* Initialize the iopub handlers. | ||||
* | ||||
* @function init_iopub_handlers | ||||
*/ | ||||
|
r13187 | Kernel.prototype.init_iopub_handlers = function () { | ||
|
r16569 | var output_msg_types = ['stream', 'display_data', 'execute_result', 'error']; | ||
|
r13187 | 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)); | ||||
|
r16569 | for (var i=0; i < output_msg_types.length; i++) { | ||
this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this)); | ||||
|
r13187 | } | ||
}; | ||||
|
r4352 | |||
|
r8768 | /** | ||
|
r18201 | * GET /api/kernels | ||
|
r18217 | * | ||
* Get the list of running kernels. | ||||
* | ||||
* @function list | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r8768 | */ | ||
|
r18201 | Kernel.prototype.list = function (success, error) { | ||
$.ajax(this.kernel_service_url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: success, | ||||
error: this._on_error(error) | ||||
}); | ||||
|
r13103 | }; | ||
|
r4545 | |||
|
r8768 | /** | ||
|
r18201 | * POST /api/kernels | ||
|
r18213 | * | ||
|
r18217 | * Start a new kernel. | ||
* | ||||
|
r18213 | * In general this shouldn't be used -- the kernel should be | ||
* started through the session API. If you use this function and | ||||
* are also using the session API then your session and kernel | ||||
* WILL be out of sync! | ||||
|
r18217 | * | ||
* @function start | ||||
* @param {params} [Object] - parameters to include in the query string | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r18201 | */ | ||
|
r18213 | Kernel.prototype.start = function (params, success, error) { | ||
var url = this.kernel_service_url; | ||||
var qs = $.param(params || {}); // query string for sage math stuff | ||||
if (qs !== "") { | ||||
url = url + "?" + qs; | ||||
} | ||||
|
r18201 | var that = this; | ||
var on_success = function (data, status, xhr) { | ||||
|
r18230 | that.events.trigger('kernel_created.Kernel', {kernel: that}); | ||
that._kernel_created(data); | ||||
|
r18201 | if (success) { | ||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
|
r18213 | $.ajax(url, { | ||
|
r18201 | processData: false, | ||
cache: false, | ||||
type: "POST", | ||||
|
r18213 | data: JSON.stringify({name: this.name}), | ||
|
r18201 | dataType: "json", | ||
success: this._on_success(on_success), | ||||
error: this._on_error(error) | ||||
}); | ||||
|
r18213 | |||
return url; | ||||
|
r18201 | }; | ||
/** | ||||
* GET /api/kernels/[:kernel_id] | ||||
|
r18217 | * | ||
* Get information about the kernel. | ||||
* | ||||
* @function get_info | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r8768 | */ | ||
|
r18201 | Kernel.prototype.get_info = function (success, error) { | ||
$.ajax(this.kernel_url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: this._on_success(success), | ||||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
/** | ||||
* DELETE /api/kernels/[:kernel_id] | ||||
|
r18217 | * | ||
* Shutdown the kernel. | ||||
* | ||||
* If you are also using sessions, then this function shoul NOT be | ||||
* used. Instead, use Session.delete. Otherwise, the session and | ||||
* kernel WILL be out of sync. | ||||
* | ||||
* @function kill | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r18201 | */ | ||
Kernel.prototype.kill = function (success, error) { | ||||
|
r18238 | this.events.trigger('kernel_killed.Kernel', {kernel: this}); | ||
|
r18206 | this._kernel_dead(); | ||
|
r18201 | $.ajax(this.kernel_url, { | ||
processData: false, | ||||
cache: false, | ||||
type: "DELETE", | ||||
dataType: "json", | ||||
|
r18206 | success: this._on_success(success), | ||
|
r18201 | error: this._on_error(error) | ||
}); | ||||
}; | ||||
/** | ||||
* POST /api/kernels/[:kernel_id]/interrupt | ||||
|
r18217 | * | ||
* Interrupt the kernel. | ||||
* | ||||
* @function interrupt | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r18201 | */ | ||
Kernel.prototype.interrupt = function (success, error) { | ||||
|
r18238 | this.events.trigger('kernel_interrupting.Kernel', {kernel: this}); | ||
|
r18220 | |||
var that = this; | ||||
var on_success = function (data, status, xhr) { | ||||
|
r18225 | // get kernel info so we know what state the kernel is in | ||
|
r18230 | that.kernel_info(); | ||
|
r18220 | if (success) { | ||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
|
r18201 | var url = utils.url_join_encode(this.kernel_url, 'interrupt'); | ||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "POST", | ||||
dataType: "json", | ||||
|
r18220 | success: this._on_success(on_success), | ||
|
r18201 | error: this._on_error(error) | ||
}); | ||||
}; | ||||
/** | ||||
* POST /api/kernels/[:kernel_id]/restart | ||||
|
r18217 | * | ||
* Restart the kernel. | ||||
* | ||||
* @function interrupt | ||||
* @param {function} [success] - function executed on ajax success | ||||
* @param {function} [error] - functon executed on ajax error | ||||
|
r18201 | */ | ||
Kernel.prototype.restart = function (success, error) { | ||||
|
r18238 | this.events.trigger('kernel_restarting.Kernel', {kernel: this}); | ||
|
r18207 | this.stop_channels(); | ||
|
r18201 | var that = this; | ||
var on_success = function (data, status, xhr) { | ||||
|
r18230 | that.events.trigger('kernel_created.Kernel', {kernel: that}); | ||
that._kernel_created(data); | ||||
|
r18201 | if (success) { | ||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
|
r18220 | var on_error = function (xhr, status, err) { | ||
that.events.trigger('kernel_dead.Kernel', {kernel: that}); | ||||
that._kernel_dead(); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
} | ||||
}; | ||||
|
r18201 | var url = utils.url_join_encode(this.kernel_url, 'restart'); | ||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "POST", | ||||
dataType: "json", | ||||
|
r18205 | success: this._on_success(on_success), | ||
|
r18220 | error: this._on_error(on_error) | ||
|
r18201 | }); | ||
|
r4352 | }; | ||
|
r18207 | /** | ||
|
r18217 | * Reconnect to a disconnected kernel. This is not actually a | ||
* standard HTTP request, but useful function nonetheless for | ||||
* reconnecting to the kernel if the connection is somehow lost. | ||||
* | ||||
* @function reconnect | ||||
|
r18207 | */ | ||
Kernel.prototype.reconnect = function () { | ||||
|
r18238 | this.events.trigger('kernel_reconnecting.Kernel', {kernel: this}); | ||
|
r18224 | setTimeout($.proxy(this.start_channels, this), 3000); | ||
|
r18207 | }; | ||
|
r18217 | /** | ||
* Handle a successful AJAX request by updating the kernel id and | ||||
* name from the response, and then optionally calling a provided | ||||
* callback. | ||||
* | ||||
* @function _on_success | ||||
* @param {function} success - callback | ||||
*/ | ||||
|
r18201 | Kernel.prototype._on_success = function (success) { | ||
var that = this; | ||||
return function (data, status, xhr) { | ||||
|
r18205 | if (data) { | ||
that.id = data.id; | ||||
that.name = data.name; | ||||
} | ||||
|
r18201 | that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id); | ||
if (success) { | ||||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
}; | ||||
|
r18217 | /** | ||
* Handle a failed AJAX request by logging the error message, and | ||||
* then optionally calling a provided callback. | ||||
* | ||||
* @function _on_error | ||||
* @param {function} error - callback | ||||
*/ | ||||
|
r18201 | Kernel.prototype._on_error = function (error) { | ||
return function (xhr, status, err) { | ||||
utils.log_ajax_error(xhr, status, err); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
} | ||||
}; | ||||
}; | ||||
|
r4352 | |||
|
r18217 | /** | ||
|
r18220 | * Perform necessary tasks once the kernel has been started, | ||
* including actually connecting to the kernel. | ||||
|
r18217 | * | ||
|
r18230 | * @function _kernel_created | ||
|
r18218 | * @param {Object} data - information about the kernel including id | ||
|
r18217 | */ | ||
|
r18230 | Kernel.prototype._kernel_created = function (data) { | ||
|
r18218 | this.id = data.id; | ||
this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id); | ||||
|
r4545 | this.start_channels(); | ||
|
r4352 | }; | ||
|
r18217 | /** | ||
* Perform necessary tasks once the connection to the kernel has | ||||
|
r18220 | * been established. This includes requesting information about | ||
* the kernel. | ||||
|
r18217 | * | ||
* @function _kernel_connected | ||||
*/ | ||||
|
r18207 | Kernel.prototype._kernel_connected = function () { | ||
|
r18238 | this.events.trigger('kernel_connected.Kernel', {kernel: this}); | ||
this.events.trigger('kernel_starting.Kernel', {kernel: this}); | ||||
|
r18225 | // get kernel info so we know what state the kernel is in | ||
var that = this; | ||||
this.kernel_info(function () { | ||||
|
r18238 | that.events.trigger('kernel_ready.Kernel', {kernel: this}); | ||
|
r18225 | }); | ||
|
r18206 | }; | ||
|
r18217 | /** | ||
|
r18220 | * Perform necessary tasks after the kernel has died. This closing | ||
* communication channels to the kernel if they are still somehow | ||||
* open. | ||||
|
r18217 | * | ||
* @function _kernel_dead | ||||
*/ | ||||
|
r18206 | Kernel.prototype._kernel_dead = function () { | ||
this.stop_channels(); | ||||
|
r18201 | }; | ||
|
r7168 | |||
|
r8768 | /** | ||
* Start the `shell`and `iopub` channels. | ||||
* Will stop and restart them if they already exist. | ||||
* | ||||
|
r18217 | * @function start_channels | ||
|
r8768 | */ | ||
|
r4545 | Kernel.prototype.start_channels = function () { | ||
|
r5253 | var that = this; | ||
|
r4545 | this.stop_channels(); | ||
|
r17303 | var ws_host_url = this.ws_url + this.kernel_url; | ||
|
r18220 | |||
|
r15400 | console.log("Starting WebSockets:", ws_host_url); | ||
|
r18220 | |||
|
r18208 | this.channels.shell = new this.WebSocket( | ||
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "shell") | ||
|
r15242 | ); | ||
|
r18208 | this.channels.stdin = new this.WebSocket( | ||
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "stdin") | ||
|
r15242 | ); | ||
|
r18208 | this.channels.iopub = new this.WebSocket( | ||
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "iopub") | ||
|
r15242 | ); | ||
|
r12254 | |||
|
r5253 | var already_called_onclose = false; // only alert once | ||
|
r8839 | var ws_closed_early = function(evt){ | ||
|
r5253 | if (already_called_onclose){ | ||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
if ( ! evt.wasClean ){ | ||||
|
r18220 | // If the websocket was closed early, that could mean | ||
// that the kernel is actually dead. Try getting | ||||
// information about the kernel from the API call -- | ||||
// if that fails, then assume the kernel is dead, | ||||
// otherwise just follow the typical websocket closed | ||||
// protocol. | ||||
that.get_info(function () { | ||||
that._ws_closed(ws_host_url, false); | ||||
}, function () { | ||||
that.events.trigger('kernel_dead.Kernel', {kernel: this}); | ||||
that._kernel_dead(); | ||||
}); | ||||
|
r5253 | } | ||
|
r5479 | }; | ||
|
r8839 | var ws_closed_late = function(evt){ | ||
|
r5253 | if (already_called_onclose){ | ||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
if ( ! evt.wasClean ){ | ||||
|
r18201 | that._ws_closed(ws_host_url, false); | ||
|
r5253 | } | ||
|
r5479 | }; | ||
|
r17676 | var ws_error = function(evt){ | ||
if (already_called_onclose){ | ||||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
|
r18220 | that._ws_closed(ws_host_url, true); | ||
|
r17676 | }; | ||
|
r18208 | |||
for (var c in this.channels) { | ||||
this.channels[c].onopen = $.proxy(this._ws_opened, this); | ||||
this.channels[c].onclose = ws_closed_early; | ||||
this.channels[c].onerror = ws_error; | ||||
|
r10366 | } | ||
|
r5253 | // switch from early-close to late-close message after 1s | ||
|
r9222 | setTimeout(function() { | ||
|
r18208 | for (var c in that.channels) { | ||
if (that.channels[c] !== null) { | ||||
that.channels[c].onclose = ws_closed_late; | ||||
|
r10366 | } | ||
|
r9222 | } | ||
|
r5253 | }, 1000); | ||
|
r18208 | this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this); | ||
this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this); | ||||
this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this); | ||||
|
r4545 | }; | ||
|
r4352 | |||
|
r8768 | /** | ||
|
r18217 | * Handle a websocket entering the open state sends session and | ||
* cookie authentication info as first message. | ||||
* | ||||
* @function _ws_opened | ||||
|
r12254 | */ | ||
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); | ||||
|
r18201 | |||
if (this.is_connected()) { | ||||
// all events ready, trigger started event. | ||||
|
r18208 | this._kernel_connected(); | ||
|
r12254 | } | ||
}; | ||||
|
r18217 | |||
/** | ||||
* Handle a websocket entering the closed state. This closes the | ||||
|
r18220 | * other communication channels if they are open. If the websocket | ||
* was not closed due to an error, try to reconnect to the kernel. | ||||
|
r18217 | * | ||
* @function _ws_closed | ||||
* @param {string} ws_url - the websocket url | ||||
|
r18220 | * @param {bool} error - whether the connection was closed due to an error | ||
|
r18217 | */ | ||
|
r18220 | Kernel.prototype._ws_closed = function(ws_url, error) { | ||
|
r18207 | this.stop_channels(); | ||
|
r18220 | |||
|
r18238 | this.events.trigger('kernel_disconnected.Kernel', {kernel: this}); | ||
|
r18236 | if (error) { | ||
|
r18207 | console.log('WebSocket connection failed: ', ws_url); | ||
|
r18236 | this._reconnect_attempt = this._reconnect_attempt + 1; | ||
|
r18238 | this.events.trigger('kernel_connection_failed.Kernel', {kernel: this, ws_url: ws_url, attempt: this._reconnect_attempt}); | ||
|
r18207 | } | ||
|
r18236 | this.reconnect(); | ||
|
r18207 | }; | ||
|
r12254 | /** | ||
|
r18217 | * Close the websocket channels. After successful close, the value | ||
* in `this.channels[channel_name]` will be null. | ||||
* | ||||
* @function stop_channels | ||||
|
r8768 | */ | ||
|
r4545 | Kernel.prototype.stop_channels = function () { | ||
|
r18209 | var that = this; | ||
var close = function (c) { | ||||
return function () { | ||||
if (that.channels[c].readyState === WebSocket.CLOSED) { | ||||
that.channels[c] = null; | ||||
} | ||||
}; | ||||
}; | ||||
|
r18208 | for (var c in this.channels) { | ||
if ( this.channels[c] !== null ) { | ||||
|
r18220 | if (this.channels[c].readyState === WebSocket.OPEN) { | ||
this.channels[c].onclose = close(c); | ||||
this.channels[c].close(); | ||||
} else { | ||||
close(c)(); | ||||
} | ||||
|
r10366 | } | ||
|
r13187 | } | ||
|
r4545 | }; | ||
|
r18217 | /** | ||
* Check whether there is a connection to the kernel. This | ||||
* function only returns true if all channel objects have been | ||||
* created and have a state of WebSocket.OPEN. | ||||
* | ||||
* @function is_connected | ||||
* @returns {bool} - whether there is a connection | ||||
*/ | ||||
|
r18201 | Kernel.prototype.is_connected = function () { | ||
|
r18208 | for (var c in this.channels) { | ||
|
r18201 | // if any channel is not ready, then we're not connected | ||
|
r18208 | if (this.channels[c] === null) { | ||
|
r18201 | return false; | ||
} | ||||
|
r18208 | if (this.channels[c].readyState !== WebSocket.OPEN) { | ||
|
r18201 | return false; | ||
} | ||||
} | ||||
return true; | ||||
}; | ||||
|
r18209 | |||
|
r18217 | /** | ||
* Check whether the connection to the kernel has been completely | ||||
* severed. This function only returns true if all channel objects | ||||
* are null. | ||||
* | ||||
* @function is_fully_disconnected | ||||
* @returns {bool} - whether the kernel is fully disconnected | ||||
*/ | ||||
|
r18209 | Kernel.prototype.is_fully_disconnected = function () { | ||
for (var c in this.channels) { | ||||
if (this.channels[c] === null) { | ||||
return true; | ||||
} | ||||
} | ||||
return false; | ||||
}; | ||||
|
r13187 | |||
|
r18217 | /** | ||
* Send a message on the Kernel's shell channel | ||||
* | ||||
* @function send_shell_message | ||||
*/ | ||||
|
r13217 | Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) { | ||
|
r18201 | if (!this.is_connected()) { | ||
throw new Error("kernel is not connected"); | ||||
} | ||||
|
r13217 | var msg = this._get_msg(msg_type, content, metadata); | ||
|
r18208 | this.channels.shell.send(JSON.stringify(msg)); | ||
|
r13187 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | ||
return msg.header.msg_id; | ||||
|
r13207 | }; | ||
|
r7168 | |||
|
r8768 | /** | ||
|
r14681 | * Get kernel info | ||
* | ||||
|
r18217 | * @function kernel_info | ||
|
r14681 | * @param callback {function} | ||
* | ||||
* When calling this method, pass a callback function that expects one argument. | ||||
* The callback will be passed the complete `kernel_info_reply` message documented | ||||
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info) | ||||
*/ | ||||
Kernel.prototype.kernel_info = function (callback) { | ||||
var callbacks; | ||||
if (callback) { | ||||
callbacks = { shell : { reply : callback } }; | ||||
} | ||||
return this.send_shell_message("kernel_info_request", {}, callbacks); | ||||
}; | ||||
/** | ||||
|
r13208 | * Get info on an object | ||
|
r8768 | * | ||
|
r13212 | * When calling this method, pass a callback function that expects one argument. | ||
|
r16587 | * The callback will be passed the complete `inspect_reply` message documented | ||
|
r13212 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) | ||
|
r18217 | * | ||
* @function inspect | ||||
* @param code {string} | ||||
* @param cursor_pos {integer} | ||||
* @param callback {function} | ||||
|
r8768 | */ | ||
|
r16587 | Kernel.prototype.inspect = function (code, cursor_pos, callback) { | ||
|
r13208 | var callbacks; | ||
if (callback) { | ||||
callbacks = { shell : { reply : callback } }; | ||||
} | ||||
|
r16580 | var content = { | ||
code : code, | ||||
cursor_pos : cursor_pos, | ||||
|
r18201 | detail_level : 0 | ||
|
r16580 | }; | ||
|
r16587 | return this.send_shell_message("inspect_request", content, callbacks); | ||
|
r13187 | }; | ||
|
r5397 | |||
|
r8768 | /** | ||
* Execute given code into kernel, and pass result to callback. | ||||
* | ||||
* @async | ||||
|
r18217 | * @function execute | ||
|
r8768 | * @param {string} code | ||
|
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} | ||||
|
r8768 | * @param {object} [options] | ||
* @param [options.silent=false] {Boolean} | ||||
* @param [options.user_expressions=empty_dict] {Dict} | ||||
* @param [options.allow_stdin=false] {Boolean} true|false | ||||
* | ||||
* @example | ||||
* | ||||
|
r18217 | * The options object should contain the options for the execute | ||
* call. Its default values are: | ||||
|
r8768 | * | ||
* options = { | ||||
* silent : true, | ||||
* user_expressions : {}, | ||||
* allow_stdin : false | ||||
* } | ||||
* | ||||
|
r18217 | * When calling this method pass a callbacks structure of the | ||
* form: | ||||
|
r8768 | * | ||
* callbacks = { | ||||
|
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 | ||||
|
r8768 | * } | ||
* | ||||
|
r18217 | * 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. | ||||
|
r8768 | */ | ||
|
r7176 | Kernel.prototype.execute = function (code, callbacks, options) { | ||
|
r4352 | var content = { | ||
code : code, | ||||
|
r7176 | silent : true, | ||
|
r11857 | store_history : false, | ||
|
r4975 | user_expressions : {}, | ||
|
r10368 | allow_stdin : false | ||
|
r4352 | }; | ||
|
r10594 | callbacks = callbacks || {}; | ||
|
r13207 | if (callbacks.input !== undefined) { | ||
|
r10368 | content.allow_stdin = true; | ||
} | ||||
|
r13187 | $.extend(true, content, options); | ||
|
r17198 | this.events.trigger('execution_request.Kernel', {kernel: this, content:content}); | ||
|
r13187 | return this.send_shell_message("execute_request", content, callbacks); | ||
|
r5479 | }; | ||
|
r4352 | |||
|
r8768 | /** | ||
|
r18217 | * When calling this method, pass a function to be called with the | ||
* `complete_reply` message as its only argument when it arrives. | ||||
|
r8768 | * | ||
|
r13208 | * `complete_reply` is documented | ||
|
r8768 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) | ||
* | ||||
|
r18217 | * @function complete | ||
|
r16580 | * @param code {string} | ||
|
r8768 | * @param cursor_pos {integer} | ||
|
r13208 | * @param callback {function} | ||
|
r8768 | */ | ||
|
r16580 | Kernel.prototype.complete = function (code, cursor_pos, callback) { | ||
|
r13208 | var callbacks; | ||
if (callback) { | ||||
callbacks = { shell : { reply : callback } }; | ||||
} | ||||
|
r4388 | var content = { | ||
|
r16580 | code : code, | ||
|
r18201 | cursor_pos : cursor_pos | ||
|
r4388 | }; | ||
|
r13187 | return this.send_shell_message("complete_request", content, callbacks); | ||
|
r5479 | }; | ||
|
r4388 | |||
|
r18217 | /** | ||
* @function send_input_reply | ||||
*/ | ||||
|
r10368 | Kernel.prototype.send_input_reply = function (input) { | ||
|
r18201 | if (!this.is_connected()) { | ||
throw new Error("kernel is not connected"); | ||||
} | ||||
|
r10366 | var content = { | ||
|
r18201 | value : input | ||
|
r10366 | }; | ||
|
r17198 | this.events.trigger('input_reply.Kernel', {kernel: this, content:content}); | ||
|
r10366 | var msg = this._get_msg("input_reply", content); | ||
|
r18208 | this.channels.stdin.send(JSON.stringify(msg)); | ||
|
r10366 | return msg.header.msg_id; | ||
}; | ||||
|
r18217 | /** | ||
* @function register_iopub_handler | ||||
*/ | ||||
|
r13187 | Kernel.prototype.register_iopub_handler = function (msg_type, callback) { | ||
this._iopub_handlers[msg_type] = callback; | ||||
}; | ||||
|
r18217 | /** | ||
* Get the iopub handler for a specific message type. | ||||
* | ||||
* @function get_iopub_handler | ||||
*/ | ||||
|
r13187 | Kernel.prototype.get_iopub_handler = function (msg_type) { | ||
return this._iopub_handlers[msg_type]; | ||||
}; | ||||
|
r18217 | /** | ||
* Get callbacks for a specific message. | ||||
* | ||||
* @function get_callbacks_for_msg | ||||
*/ | ||||
|
r7168 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { | ||
|
r16359 | if (msg_id == this.last_msg_id) { | ||
return this.last_msg_callbacks; | ||||
} else { | ||||
return this._msg_callbacks[msg_id]; | ||||
} | ||||
|
r7168 | }; | ||
|
r18217 | /** | ||
* Clear callbacks for a specific message. | ||||
* | ||||
* @function 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]; | ||||
} | ||||
}; | ||||
|
r13207 | |||
|
r18217 | /** | ||
* @function _finish_shell | ||||
*/ | ||||
|
r16359 | Kernel.prototype._finish_shell = function (msg_id) { | ||
var callbacks = this._msg_callbacks[msg_id]; | ||||
if (callbacks !== undefined) { | ||||
callbacks.shell_done = true; | ||||
if (callbacks.iopub_done) { | ||||
this.clear_callbacks_for_msg(msg_id); | ||||
} | ||||
} | ||||
}; | ||||
|
r18217 | /** | ||
* @function _finish_iopub | ||||
*/ | ||||
|
r16359 | Kernel.prototype._finish_iopub = function (msg_id) { | ||
var callbacks = this._msg_callbacks[msg_id]; | ||||
if (callbacks !== undefined) { | ||||
callbacks.iopub_done = true; | ||||
|
r16599 | if (callbacks.shell_done) { | ||
|
r16359 | this.clear_callbacks_for_msg(msg_id); | ||
} | ||||
} | ||||
}; | ||||
|
r18217 | /** | ||
* Set callbacks for a particular message. | ||||
|
r13207 | * Callbacks should be a struct of the following form: | ||
* shell : { | ||||
* | ||||
* } | ||||
|
r18217 | * | ||
* @function set_callbacks_for_msg | ||||
|
r13207 | */ | ||
|
r7168 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { | ||
|
r16359 | this.last_msg_id = msg_id; | ||
|
r13187 | if (callbacks) { | ||
|
r13207 | // shallow-copy mapping, because we will modify it at the top level | ||
|
r16359 | var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {}; | ||
|
r13207 | cbcopy.shell = callbacks.shell; | ||
cbcopy.iopub = callbacks.iopub; | ||||
cbcopy.input = callbacks.input; | ||||
|
r16359 | cbcopy.shell_done = (!callbacks.shell); | ||
cbcopy.iopub_done = (!callbacks.iopub); | ||||
} else { | ||||
this.last_msg_callbacks = {}; | ||||
|
r13187 | } | ||
|
r12555 | }; | ||
|
r7168 | |||
|
r18217 | /** | ||
* @function _handle_shell_reply | ||||
*/ | ||||
|
r7168 | Kernel.prototype._handle_shell_reply = function (e) { | ||
|
r8839 | var reply = $.parseJSON(e.data); | ||
|
r17198 | this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply}); | ||
|
r7168 | var content = reply.content; | ||
|
r7952 | var metadata = reply.metadata; | ||
|
r13207 | var parent_id = reply.parent_header.msg_id; | ||
var callbacks = this.get_callbacks_for_msg(parent_id); | ||||
if (!callbacks || !callbacks.shell) { | ||||
return; | ||||
|
r13187 | } | ||
|
r13207 | var shell_callbacks = callbacks.shell; | ||
|
r16359 | // signal that shell callbacks are done | ||
this._finish_shell(parent_id); | ||||
|
r13207 | |||
if (shell_callbacks.reply !== undefined) { | ||||
shell_callbacks.reply(reply); | ||||
} | ||||
if (content.payload && shell_callbacks.payload) { | ||||
this._handle_payloads(content.payload, shell_callbacks.payload, reply); | ||||
|
r7168 | } | ||
}; | ||||
|
r18217 | /** | ||
* @function _handle_payloads | ||||
*/ | ||||
|
r13207 | Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) { | ||
var l = payloads.length; | ||||
|
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++) { | ||||
|
r13207 | var payload = payloads[i]; | ||
var callback = payload_callbacks[payload.source]; | ||||
if (callback) { | ||||
callback(payload, msg); | ||||
|
r7168 | } | ||
|
r13187 | } | ||
|
r7168 | }; | ||
|
r18217 | /** | ||
* @function _handle_status_message | ||||
*/ | ||||
|
r13187 | Kernel.prototype._handle_status_message = function (msg) { | ||
var execution_state = msg.content.execution_state; | ||||
|
r13231 | var parent_id = msg.parent_header.msg_id; | ||
// dispatch status msg callbacks, if any | ||||
var callbacks = this.get_callbacks_for_msg(parent_id); | ||||
if (callbacks && callbacks.iopub && callbacks.iopub.status) { | ||||
try { | ||||
callbacks.iopub.status(msg); | ||||
} catch (e) { | ||||
|
r14499 | console.log("Exception in status msg handler", e, e.stack); | ||
|
r13231 | } | ||
} | ||||
|
r13187 | if (execution_state === 'busy') { | ||
|
r18238 | this.events.trigger('kernel_busy.Kernel', {kernel: this}); | ||
|
r18215 | |||
|
r13187 | } else if (execution_state === 'idle') { | ||
|
r16359 | // signal that iopub callbacks are (probably) done | ||
// async output may still arrive, | ||||
// but only for the most recent request | ||||
this._finish_iopub(parent_id); | ||||
|
r13231 | // trigger status_idle event | ||
|
r18238 | this.events.trigger('kernel_idle.Kernel', {kernel: this}); | ||
|
r18215 | |||
|
r18220 | } else if (execution_state === 'starting') { | ||
|
r18238 | this.events.trigger('kernel_starting.Kernel', {kernel: this}); | ||
|
r18230 | var that = this; | ||
this.kernel_info(function () { | ||||
|
r18238 | that.events.trigger('kernel_ready.Kernel', {kernel: this}); | ||
|
r18230 | }); | ||
|
r18220 | |||
|
r13187 | } else if (execution_state === 'restarting') { | ||
// autorestarting is distinct from restarting, | ||||
// in that it means the kernel died and the server is restarting it. | ||||
|
r18238 | // kernel_restarting sets the notification widget, | ||
|
r13187 | // autorestart shows the more prominent dialog. | ||
|
r18233 | this._autorestart_attempt = this._autorestart_attempt + 1; | ||
|
r18238 | this.events.trigger('kernel_restarting.Kernel', {kernel: this}); | ||
this.events.trigger('kernel_autorestarting.Kernel', {kernel: this, attempt: this._autorestart_attempt}); | ||||
|
r18215 | |||
|
r13187 | } else if (execution_state === 'dead') { | ||
|
r18220 | this.events.trigger('kernel_dead.Kernel', {kernel: this}); | ||
|
r18215 | this._kernel_dead(); | ||
|
r13187 | } | ||
}; | ||||
|
r18217 | /** | ||
* Handle clear_output message | ||||
* | ||||
* @function _handle_clear_output | ||||
*/ | ||||
|
r13187 | Kernel.prototype._handle_clear_output = function (msg) { | ||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); | ||||
|
r13207 | if (!callbacks || !callbacks.iopub) { | ||
|
r13187 | return; | ||
} | ||||
|
r13214 | var callback = callbacks.iopub.clear_output; | ||
|
r13207 | if (callback) { | ||
callback(msg); | ||||
|
r13187 | } | ||
}; | ||||
|
r7168 | |||
|
r18217 | /** | ||
* handle an output message (execute_result, display_data, etc.) | ||||
* | ||||
* @function _handle_output_message | ||||
*/ | ||||
|
r13187 | Kernel.prototype._handle_output_message = function (msg) { | ||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); | ||||
|
r13207 | if (!callbacks || !callbacks.iopub) { | ||
|
r7168 | return; | ||
} | ||||
|
r13207 | var callback = callbacks.iopub.output; | ||
if (callback) { | ||||
callback(msg); | ||||
|
r13187 | } | ||
}; | ||||
|
r18217 | /** | ||
* Dispatch IOPub messages to respective handlers. Each message | ||||
* type should have a handler. | ||||
* | ||||
* @function _handle_iopub_message | ||||
*/ | ||||
|
r13187 | 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); | ||||
} | ||||
|
r7168 | }; | ||
|
r18217 | /** | ||
* @function _handle_input_request | ||||
*/ | ||||
|
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; | ||||
} | ||||
|
r10368 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); | ||
|
r13207 | if (callbacks) { | ||
if (callbacks.input) { | ||||
callbacks.input(request); | ||||
|
r10368 | } | ||
|
r13187 | } | ||
|
r10366 | }; | ||
|
r17200 | // Backwards compatability. | ||
|
r4352 | IPython.Kernel = Kernel; | ||
|
r17201 | return {'Kernel': Kernel}; | ||
|
r17198 | }); | ||