kernel.js
762 lines
| 24.7 KiB
| application/javascript
|
JavascriptLexer
MinRK
|
r16359 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r4349 | |||
Jonathan Frederic
|
r17198 | define([ | ||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'services/kernels/js/comm', | ||||
'widgets/js/init', | ||||
Jonathan Frederic
|
r17202 | ], function(IPython, $, utils, comm, widgetmanager) { | ||
MinRK
|
r13187 | "use strict"; | ||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r7168 | // Initialization and connection. | ||
Matthias BUSSONNIER
|
r8768 | /** | ||
* A Kernel Class to communicate with the Python kernel | ||||
* @Class Kernel | ||||
*/ | ||||
Jessica B. Hamrick
|
r18201 | var Kernel = function (kernel_service_url, ws_url, notebook, id, name) { | ||
Jonathan Frederic
|
r17200 | this.events = notebook.events; | ||
Jessica B. Hamrick
|
r18201 | |||
this.id = id; | ||||
this.name = name; | ||||
Jessica B. Hamrick
|
r18208 | this.channels = { | ||
'shell': null, | ||||
'iopub': null, | ||||
'stdin': null | ||||
}; | ||||
Jessica B. Hamrick
|
r18201 | |||
MinRK
|
r15242 | this.kernel_service_url = kernel_service_url; | ||
Jessica B. Hamrick
|
r18201 | this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id); | ||
MinRK
|
r17308 | this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl"); | ||
MinRK
|
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; | ||||
} | ||||
Jessica B. Hamrick
|
r18201 | |||
MinRK
|
r13102 | this.username = "username"; | ||
Zachary Sailer
|
r13035 | this.session_id = utils.uuid(); | ||
Brian Granger
|
r7168 | this._msg_callbacks = {}; | ||
Brian E. Granger
|
r4612 | if (typeof(WebSocket) !== 'undefined') { | ||
Stefan van der Walt
|
r5479 | this.WebSocket = WebSocket; | ||
Brian E. Granger
|
r4612 | } else if (typeof(MozWebSocket) !== 'undefined') { | ||
Stefan van der Walt
|
r5479 | this.WebSocket = MozWebSocket; | ||
Brian E. Granger
|
r4611 | } else { | ||
MinRK
|
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
|
r13187 | } | ||
MinRK
|
r11611 | this.bind_events(); | ||
MinRK
|
r13187 | this.init_iopub_handlers(); | ||
Jonathan Frederic
|
r17198 | this.comm_manager = new comm.CommManager(this); | ||
Jonathan Frederic
|
r17202 | this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook); | ||
MinRK
|
r16359 | |||
this.last_msg_id = null; | ||||
this.last_msg_callbacks = {}; | ||||
Brian E. Granger
|
r4352 | }; | ||
MinRK
|
r13217 | Kernel.prototype._get_msg = function (msg_type, content, metadata) { | ||
Brian E. Granger
|
r4352 | var msg = { | ||
header : { | ||||
msg_id : utils.uuid(), | ||||
MinRK
|
r4694 | username : this.username, | ||
Zachary Sailer
|
r13035 | session : this.session_id, | ||
MinRK
|
r16666 | msg_type : msg_type, | ||
version : "5.0" | ||||
Brian E. Granger
|
r4352 | }, | ||
MinRK
|
r13217 | metadata : metadata || {}, | ||
Brian E. Granger
|
r4352 | content : content, | ||
parent_header : {} | ||||
}; | ||||
return msg; | ||||
Stefan van der Walt
|
r5479 | }; | ||
MinRK
|
r11611 | |||
MinRK
|
r13187 | Kernel.prototype.bind_events = function () { | ||
MinRK
|
r11611 | var that = this; | ||
Jonathan Frederic
|
r17198 | this.events.on('send_input_reply.Kernel', function(evt, data) { | ||
MinRK
|
r11611 | that.send_input_reply(data); | ||
}); | ||||
MinRK
|
r13187 | }; | ||
// Initialize the iopub handlers | ||||
Kernel.prototype.init_iopub_handlers = function () { | ||||
MinRK
|
r16569 | var output_msg_types = ['stream', 'display_data', 'execute_result', 'error']; | ||
MinRK
|
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)); | ||||
MinRK
|
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)); | ||||
MinRK
|
r13187 | } | ||
}; | ||||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
Jessica B. Hamrick
|
r18201 | * GET /api/kernels | ||
Matthias BUSSONNIER
|
r8768 | */ | ||
Jessica B. Hamrick
|
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) | ||||
}); | ||||
MinRK
|
r13103 | }; | ||
Brian E. Granger
|
r4545 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
Jessica B. Hamrick
|
r18201 | * POST /api/kernels | ||
*/ | ||||
Kernel.prototype.start = function (success, error) { | ||||
var that = this; | ||||
var on_success = function (data, status, xhr) { | ||||
that._kernel_started(data); | ||||
if (success) { | ||||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
$.ajax(this.kernel_service_url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "POST", | ||||
dataType: "json", | ||||
success: this._on_success(on_success), | ||||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
/** | ||||
* GET /api/kernels/[:kernel_id] | ||||
Matthias BUSSONNIER
|
r8768 | */ | ||
Jessica B. Hamrick
|
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] | ||||
*/ | ||||
Kernel.prototype.kill = function (success, error) { | ||||
Jessica B. Hamrick
|
r18206 | this._kernel_dead(); | ||
Jessica B. Hamrick
|
r18201 | $.ajax(this.kernel_url, { | ||
processData: false, | ||||
cache: false, | ||||
type: "DELETE", | ||||
dataType: "json", | ||||
Jessica B. Hamrick
|
r18206 | success: this._on_success(success), | ||
Jessica B. Hamrick
|
r18201 | error: this._on_error(error) | ||
}); | ||||
}; | ||||
/** | ||||
* POST /api/kernels/[:kernel_id]/interrupt | ||||
*/ | ||||
Kernel.prototype.interrupt = function (success, error) { | ||||
this.events.trigger('status_interrupting.Kernel', {kernel: this}); | ||||
var url = utils.url_join_encode(this.kernel_url, 'interrupt'); | ||||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "POST", | ||||
dataType: "json", | ||||
success: this._on_success(success), | ||||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
/** | ||||
* POST /api/kernels/[:kernel_id]/restart | ||||
*/ | ||||
Kernel.prototype.restart = function (success, error) { | ||||
Jessica B. Hamrick
|
r18207 | this.events.trigger('status_restarting.Kernel', {kernel: this}); | ||
this.stop_channels(); | ||||
Jessica B. Hamrick
|
r18201 | var that = this; | ||
var on_success = function (data, status, xhr) { | ||||
Jessica B. Hamrick
|
r18206 | that._kernel_started(data, status, xhr); | ||
Jessica B. Hamrick
|
r18201 | if (success) { | ||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
var url = utils.url_join_encode(this.kernel_url, 'restart'); | ||||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "POST", | ||||
dataType: "json", | ||||
Jessica B. Hamrick
|
r18205 | success: this._on_success(on_success), | ||
Jessica B. Hamrick
|
r18201 | error: this._on_error(error) | ||
}); | ||||
Brian E. Granger
|
r4352 | }; | ||
Jessica B. Hamrick
|
r18207 | /** | ||
* Not actually a HTTP request, but useful function nonetheless | ||||
* for reconnecting to the kernel if the connection is somehow lost | ||||
*/ | ||||
Kernel.prototype.reconnect = function () { | ||||
this.events.trigger('status_reconnecting.Kernel'); | ||||
var that = this; | ||||
setTimeout(function () { | ||||
that.start_channels(); | ||||
}, 5000); | ||||
}; | ||||
Jessica B. Hamrick
|
r18201 | Kernel.prototype._on_success = function (success) { | ||
var that = this; | ||||
return function (data, status, xhr) { | ||||
Jessica B. Hamrick
|
r18205 | if (data) { | ||
that.id = data.id; | ||||
that.name = data.name; | ||||
} | ||||
Jessica B. Hamrick
|
r18201 | that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id); | ||
if (success) { | ||||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
}; | ||||
Kernel.prototype._on_error = function (error) { | ||||
return function (xhr, status, err) { | ||||
utils.log_ajax_error(xhr, status, err); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
} | ||||
}; | ||||
}; | ||||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r7168 | Kernel.prototype._kernel_started = function (json) { | ||
Zachary Sailer
|
r13015 | console.log("Kernel started: ", json.id); | ||
Jessica B. Hamrick
|
r18201 | this.events.trigger('status_started.Kernel', {kernel: this}); | ||
Brian E. Granger
|
r4545 | this.start_channels(); | ||
Brian E. Granger
|
r4352 | }; | ||
Jessica B. Hamrick
|
r18207 | Kernel.prototype._kernel_connected = function () { | ||
var that = this; | ||||
Jessica B. Hamrick
|
r18208 | console.log('Connected to kernel: ', this.id); | ||
Jessica B. Hamrick
|
r18207 | this.events.trigger('status_connected.Kernel'); | ||
this.kernel_info(function () { | ||||
that.events.trigger('status_idle.Kernel'); | ||||
}); | ||||
Jessica B. Hamrick
|
r18206 | }; | ||
Kernel.prototype._kernel_dead = function () { | ||||
Jessica B. Hamrick
|
r18201 | this.events.trigger('status_dead.Kernel'); | ||
Jessica B. Hamrick
|
r18206 | this.stop_channels(); | ||
Jessica B. Hamrick
|
r18201 | }; | ||
Brian Granger
|
r7168 | |||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
* Start the `shell`and `iopub` channels. | ||||
* Will stop and restart them if they already exist. | ||||
* | ||||
* @method start_channels | ||||
*/ | ||||
Brian E. Granger
|
r4545 | Kernel.prototype.start_channels = function () { | ||
MinRK
|
r5253 | var that = this; | ||
Brian E. Granger
|
r4545 | this.stop_channels(); | ||
MinRK
|
r17303 | var ws_host_url = this.ws_url + this.kernel_url; | ||
MinRK
|
r15400 | console.log("Starting WebSockets:", ws_host_url); | ||
Jessica B. Hamrick
|
r18208 | this.channels.shell = new this.WebSocket( | ||
MinRK
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "shell") | ||
MinRK
|
r15242 | ); | ||
Jessica B. Hamrick
|
r18208 | this.channels.stdin = new this.WebSocket( | ||
MinRK
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "stdin") | ||
MinRK
|
r15242 | ); | ||
Jessica B. Hamrick
|
r18208 | this.channels.iopub = new this.WebSocket( | ||
MinRK
|
r17303 | this.ws_url + utils.url_join_encode(this.kernel_url, "iopub") | ||
MinRK
|
r15242 | ); | ||
MinRK
|
r12254 | |||
MinRK
|
r5253 | var already_called_onclose = false; // only alert once | ||
Mikhail Korobov
|
r8839 | var ws_closed_early = function(evt){ | ||
MinRK
|
r5253 | if (already_called_onclose){ | ||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
if ( ! evt.wasClean ){ | ||||
Jessica B. Hamrick
|
r18201 | that._ws_closed(ws_host_url, true); | ||
MinRK
|
r5253 | } | ||
Stefan van der Walt
|
r5479 | }; | ||
Mikhail Korobov
|
r8839 | var ws_closed_late = function(evt){ | ||
MinRK
|
r5253 | if (already_called_onclose){ | ||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
if ( ! evt.wasClean ){ | ||||
Jessica B. Hamrick
|
r18201 | that._ws_closed(ws_host_url, false); | ||
MinRK
|
r5253 | } | ||
Stefan van der Walt
|
r5479 | }; | ||
MinRK
|
r17676 | var ws_error = function(evt){ | ||
if (already_called_onclose){ | ||||
return; | ||||
} | ||||
already_called_onclose = true; | ||||
Jessica B. Hamrick
|
r18201 | that._ws_closed(ws_host_url, false); | ||
MinRK
|
r17676 | }; | ||
Jessica B. Hamrick
|
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; | ||||
MinRK
|
r10366 | } | ||
MinRK
|
r5253 | // switch from early-close to late-close message after 1s | ||
Brian E. Granger
|
r9222 | setTimeout(function() { | ||
Jessica B. Hamrick
|
r18208 | for (var c in that.channels) { | ||
if (that.channels[c] !== null) { | ||||
that.channels[c].onclose = ws_closed_late; | ||||
MinRK
|
r10366 | } | ||
Brian E. Granger
|
r9222 | } | ||
MinRK
|
r5253 | }, 1000); | ||
Jessica B. Hamrick
|
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); | ||||
Brian E. Granger
|
r4545 | }; | ||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
MinRK
|
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); | ||||
Jessica B. Hamrick
|
r18201 | |||
if (this.is_connected()) { | ||||
// all events ready, trigger started event. | ||||
Jessica B. Hamrick
|
r18208 | this._kernel_connected(); | ||
MinRK
|
r12254 | } | ||
}; | ||||
Jessica B. Hamrick
|
r18207 | Kernel.prototype._ws_closed = function(ws_url, early) { | ||
this.stop_channels(); | ||||
this.events.trigger('status_disconnected.Kernel'); | ||||
if (!early) { | ||||
this.reconnect(); | ||||
} else { | ||||
console.log('WebSocket connection failed: ', ws_url); | ||||
this.events.trigger('early_disconnect.Kernel', ws_url); | ||||
} | ||||
}; | ||||
MinRK
|
r12254 | /** | ||
* Stop the websocket channels. | ||||
Matthias BUSSONNIER
|
r8768 | * @method stop_channels | ||
*/ | ||||
Brian E. Granger
|
r4545 | Kernel.prototype.stop_channels = function () { | ||
Jessica B. Hamrick
|
r18209 | var that = this; | ||
var close = function (c) { | ||||
return function () { | ||||
if (that.channels[c].readyState === WebSocket.CLOSED) { | ||||
that.channels[c] = null; | ||||
} | ||||
}; | ||||
}; | ||||
Jessica B. Hamrick
|
r18208 | for (var c in this.channels) { | ||
if ( this.channels[c] !== null ) { | ||||
Jessica B. Hamrick
|
r18209 | this.channels[c].onclose = close(c); | ||
Jessica B. Hamrick
|
r18208 | this.channels[c].close(); | ||
MinRK
|
r10366 | } | ||
MinRK
|
r13187 | } | ||
Brian E. Granger
|
r4545 | }; | ||
Brian Granger
|
r7168 | // Main public methods. | ||
Jessica B. Hamrick
|
r18201 | |||
Kernel.prototype.is_connected = function () { | ||||
Jessica B. Hamrick
|
r18208 | for (var c in this.channels) { | ||
Jessica B. Hamrick
|
r18201 | // if any channel is not ready, then we're not connected | ||
Jessica B. Hamrick
|
r18208 | if (this.channels[c] === null) { | ||
Jessica B. Hamrick
|
r18201 | return false; | ||
} | ||||
Jessica B. Hamrick
|
r18208 | if (this.channels[c].readyState !== WebSocket.OPEN) { | ||
Jessica B. Hamrick
|
r18201 | return false; | ||
} | ||||
} | ||||
return true; | ||||
}; | ||||
Jessica B. Hamrick
|
r18209 | |||
Kernel.prototype.is_fully_disconnected = function () { | ||||
for (var c in this.channels) { | ||||
if (this.channels[c] === null) { | ||||
return true; | ||||
} | ||||
} | ||||
return false; | ||||
}; | ||||
MinRK
|
r13187 | |||
// send a message on the Kernel's shell channel | ||||
MinRK
|
r13217 | Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) { | ||
Jessica B. Hamrick
|
r18201 | if (!this.is_connected()) { | ||
throw new Error("kernel is not connected"); | ||||
} | ||||
MinRK
|
r13217 | var msg = this._get_msg(msg_type, content, metadata); | ||
Jessica B. Hamrick
|
r18208 | this.channels.shell.send(JSON.stringify(msg)); | ||
MinRK
|
r13187 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | ||
return msg.header.msg_id; | ||||
MinRK
|
r13207 | }; | ||
Brian Granger
|
r7168 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
Matthias BUSSONNIER
|
r14681 | * Get kernel info | ||
* | ||||
* @param callback {function} | ||||
MinRK
|
r16587 | * @method kernel_info | ||
Matthias BUSSONNIER
|
r14681 | * | ||
* 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); | ||||
}; | ||||
/** | ||||
MinRK
|
r13208 | * Get info on an object | ||
Matthias BUSSONNIER
|
r8768 | * | ||
MinRK
|
r16580 | * @param code {string} | ||
* @param cursor_pos {integer} | ||||
MinRK
|
r13208 | * @param callback {function} | ||
MinRK
|
r16587 | * @method inspect | ||
Matthias BUSSONNIER
|
r8768 | * | ||
MinRK
|
r13212 | * When calling this method, pass a callback function that expects one argument. | ||
MinRK
|
r16587 | * The callback will be passed the complete `inspect_reply` message documented | ||
MinRK
|
r13212 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) | ||
Matthias BUSSONNIER
|
r8768 | */ | ||
MinRK
|
r16587 | Kernel.prototype.inspect = function (code, cursor_pos, callback) { | ||
MinRK
|
r13208 | var callbacks; | ||
if (callback) { | ||||
callbacks = { shell : { reply : callback } }; | ||||
} | ||||
MinRK
|
r16580 | var content = { | ||
code : code, | ||||
cursor_pos : cursor_pos, | ||||
Jessica B. Hamrick
|
r18201 | detail_level : 0 | ||
MinRK
|
r16580 | }; | ||
MinRK
|
r16587 | return this.send_shell_message("inspect_request", content, callbacks); | ||
MinRK
|
r13187 | }; | ||
Matthias BUSSONNIER
|
r5397 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
* Execute given code into kernel, and pass result to callback. | ||||
* | ||||
* @async | ||||
* @method execute | ||||
* @param {string} code | ||||
MinRK
|
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
|
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 | ||||
* | ||||
* The options object should contain the options for the execute call. Its default | ||||
* values are: | ||||
* | ||||
* options = { | ||||
* silent : true, | ||||
* user_expressions : {}, | ||||
* allow_stdin : false | ||||
* } | ||||
* | ||||
* When calling this method pass a callbacks structure of the form: | ||||
* | ||||
* callbacks = { | ||||
MinRK
|
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
|
r8768 | * } | ||
* | ||||
MinRK
|
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
|
r8768 | */ | ||
Brian Granger
|
r7176 | Kernel.prototype.execute = function (code, callbacks, options) { | ||
Brian E. Granger
|
r4352 | var content = { | ||
code : code, | ||||
Brian Granger
|
r7176 | silent : true, | ||
MinRK
|
r11857 | store_history : false, | ||
MinRK
|
r4975 | user_expressions : {}, | ||
MinRK
|
r10368 | allow_stdin : false | ||
Brian E. Granger
|
r4352 | }; | ||
Matthias BUSSONNIER
|
r10594 | callbacks = callbacks || {}; | ||
MinRK
|
r13207 | if (callbacks.input !== undefined) { | ||
MinRK
|
r10368 | content.allow_stdin = true; | ||
} | ||||
MinRK
|
r13187 | $.extend(true, content, options); | ||
Jonathan Frederic
|
r17198 | this.events.trigger('execution_request.Kernel', {kernel: this, content:content}); | ||
MinRK
|
r13187 | return this.send_shell_message("execute_request", content, callbacks); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
MinRK
|
r13212 | * When calling this method, pass a function to be called with the `complete_reply` message | ||
MinRK
|
r13208 | * as its only argument when it arrives. | ||
Matthias BUSSONNIER
|
r8768 | * | ||
MinRK
|
r13208 | * `complete_reply` is documented | ||
Matthias BUSSONNIER
|
r8768 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) | ||
* | ||||
* @method complete | ||||
MinRK
|
r16580 | * @param code {string} | ||
Matthias BUSSONNIER
|
r8768 | * @param cursor_pos {integer} | ||
MinRK
|
r13208 | * @param callback {function} | ||
Matthias BUSSONNIER
|
r8768 | * | ||
*/ | ||||
MinRK
|
r16580 | Kernel.prototype.complete = function (code, cursor_pos, callback) { | ||
MinRK
|
r13208 | var callbacks; | ||
if (callback) { | ||||
callbacks = { shell : { reply : callback } }; | ||||
} | ||||
Brian Granger
|
r4388 | var content = { | ||
MinRK
|
r16580 | code : code, | ||
Jessica B. Hamrick
|
r18201 | cursor_pos : cursor_pos | ||
Brian Granger
|
r4388 | }; | ||
MinRK
|
r13187 | return this.send_shell_message("complete_request", content, callbacks); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4388 | |||
MinRK
|
r10368 | Kernel.prototype.send_input_reply = function (input) { | ||
Jessica B. Hamrick
|
r18201 | if (!this.is_connected()) { | ||
throw new Error("kernel is not connected"); | ||||
} | ||||
MinRK
|
r10366 | var content = { | ||
Jessica B. Hamrick
|
r18201 | value : input | ||
MinRK
|
r10366 | }; | ||
Jonathan Frederic
|
r17198 | this.events.trigger('input_reply.Kernel', {kernel: this, content:content}); | ||
MinRK
|
r10366 | var msg = this._get_msg("input_reply", content); | ||
Jessica B. Hamrick
|
r18208 | this.channels.stdin.send(JSON.stringify(msg)); | ||
MinRK
|
r10366 | return msg.header.msg_id; | ||
}; | ||||
// Reply handlers | ||||
Brian Granger
|
r7168 | |||
MinRK
|
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
|
r7168 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { | ||
MinRK
|
r13187 | // get callbacks for a specific message | ||
MinRK
|
r16359 | if (msg_id == this.last_msg_id) { | ||
return this.last_msg_callbacks; | ||||
} else { | ||||
return this._msg_callbacks[msg_id]; | ||||
} | ||||
Brian Granger
|
r7168 | }; | ||
MinRK
|
r12555 | Kernel.prototype.clear_callbacks_for_msg = function (msg_id) { | ||
if (this._msg_callbacks[msg_id] !== undefined ) { | ||||
delete this._msg_callbacks[msg_id]; | ||||
} | ||||
}; | ||||
MinRK
|
r13207 | |||
MinRK
|
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); | ||||
} | ||||
} | ||||
}; | ||||
Kernel.prototype._finish_iopub = function (msg_id) { | ||||
var callbacks = this._msg_callbacks[msg_id]; | ||||
if (callbacks !== undefined) { | ||||
callbacks.iopub_done = true; | ||||
MinRK
|
r16599 | if (callbacks.shell_done) { | ||
MinRK
|
r16359 | this.clear_callbacks_for_msg(msg_id); | ||
} | ||||
} | ||||
}; | ||||
MinRK
|
r13207 | /* Set callbacks for a particular message. | ||
* Callbacks should be a struct of the following form: | ||||
* shell : { | ||||
* | ||||
* } | ||||
*/ | ||||
Brian Granger
|
r7168 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { | ||
MinRK
|
r16359 | this.last_msg_id = msg_id; | ||
MinRK
|
r13187 | if (callbacks) { | ||
MinRK
|
r13207 | // shallow-copy mapping, because we will modify it at the top level | ||
MinRK
|
r16359 | var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {}; | ||
MinRK
|
r13207 | cbcopy.shell = callbacks.shell; | ||
cbcopy.iopub = callbacks.iopub; | ||||
cbcopy.input = callbacks.input; | ||||
MinRK
|
r16359 | cbcopy.shell_done = (!callbacks.shell); | ||
cbcopy.iopub_done = (!callbacks.iopub); | ||||
} else { | ||||
this.last_msg_callbacks = {}; | ||||
MinRK
|
r13187 | } | ||
MinRK
|
r12555 | }; | ||
Brian Granger
|
r7168 | |||
Kernel.prototype._handle_shell_reply = function (e) { | ||||
Mikhail Korobov
|
r8839 | var reply = $.parseJSON(e.data); | ||
Jonathan Frederic
|
r17198 | this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply}); | ||
Brian Granger
|
r7168 | var content = reply.content; | ||
Jason Grout
|
r7952 | var metadata = reply.metadata; | ||
MinRK
|
r13207 | var parent_id = reply.parent_header.msg_id; | ||
var callbacks = this.get_callbacks_for_msg(parent_id); | ||||
if (!callbacks || !callbacks.shell) { | ||||
return; | ||||
MinRK
|
r13187 | } | ||
MinRK
|
r13207 | var shell_callbacks = callbacks.shell; | ||
MinRK
|
r16359 | // signal that shell callbacks are done | ||
this._finish_shell(parent_id); | ||||
MinRK
|
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); | ||||
Brian Granger
|
r7168 | } | ||
}; | ||||
MinRK
|
r13207 | Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) { | ||
var l = payloads.length; | ||||
Brian Granger
|
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
|
r13207 | var payload = payloads[i]; | ||
var callback = payload_callbacks[payload.source]; | ||||
if (callback) { | ||||
callback(payload, msg); | ||||
Brian Granger
|
r7168 | } | ||
MinRK
|
r13187 | } | ||
Brian Granger
|
r7168 | }; | ||
MinRK
|
r13187 | Kernel.prototype._handle_status_message = function (msg) { | ||
var execution_state = msg.content.execution_state; | ||||
MinRK
|
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) { | ||||
Jason Grout
|
r14499 | console.log("Exception in status msg handler", e, e.stack); | ||
MinRK
|
r13231 | } | ||
} | ||||
MinRK
|
r13187 | if (execution_state === 'busy') { | ||
Jonathan Frederic
|
r17198 | this.events.trigger('status_busy.Kernel', {kernel: this}); | ||
MinRK
|
r13187 | } else if (execution_state === 'idle') { | ||
MinRK
|
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); | ||||
MinRK
|
r13231 | // trigger status_idle event | ||
Jonathan Frederic
|
r17198 | this.events.trigger('status_idle.Kernel', {kernel: this}); | ||
MinRK
|
r13187 | } 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. | ||||
Jonathan Frederic
|
r17198 | this.events.trigger('status_autorestarting.Kernel', {kernel: this}); | ||
this.events.trigger('status_restarting.Kernel', {kernel: this}); | ||||
MinRK
|
r13187 | } else if (execution_state === 'dead') { | ||
this.stop_channels(); | ||||
Jonathan Frederic
|
r17198 | this.events.trigger('status_dead.Kernel', {kernel: this}); | ||
Jessica B. Hamrick
|
r18060 | this.events.trigger('status_restart_failed.Kernel', {kernel: this}); | ||
MinRK
|
r13187 | } | ||
}; | ||||
// handle clear_output message | ||||
Kernel.prototype._handle_clear_output = function (msg) { | ||||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); | ||||
MinRK
|
r13207 | if (!callbacks || !callbacks.iopub) { | ||
MinRK
|
r13187 | return; | ||
} | ||||
MinRK
|
r13214 | var callback = callbacks.iopub.clear_output; | ||
MinRK
|
r13207 | if (callback) { | ||
callback(msg); | ||||
MinRK
|
r13187 | } | ||
}; | ||||
Brian Granger
|
r7168 | |||
MinRK
|
r13187 | |||
MinRK
|
r16568 | // handle an output message (execute_result, display_data, etc.) | ||
MinRK
|
r13187 | Kernel.prototype._handle_output_message = function (msg) { | ||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id); | ||||
MinRK
|
r13207 | if (!callbacks || !callbacks.iopub) { | ||
Brian Granger
|
r7168 | return; | ||
} | ||||
MinRK
|
r13207 | var callback = callbacks.iopub.output; | ||
if (callback) { | ||||
callback(msg); | ||||
MinRK
|
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
|
r7168 | }; | ||
MinRK
|
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
|
r10368 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); | ||
MinRK
|
r13207 | if (callbacks) { | ||
if (callbacks.input) { | ||||
callbacks.input(request); | ||||
MinRK
|
r10368 | } | ||
MinRK
|
r13187 | } | ||
MinRK
|
r10366 | }; | ||
Jonathan Frederic
|
r17200 | // Backwards compatability. | ||
Brian E. Granger
|
r4352 | IPython.Kernel = Kernel; | ||
Jonathan Frederic
|
r17201 | return {'Kernel': Kernel}; | ||
Jonathan Frederic
|
r17198 | }); | ||