##// END OF EJS Templates
handle undefined when sorting quick help...
handle undefined when sorting quick help since undefined is neither less than nor greater than anything in Javascript, the sort function was treating it as equal to everything, causing inconsistent behavior, depending on the sort algorithm of the browser. This ensures undefined elements are sorted last in the sequence.

File last commit:

r20621:f936f880
r20837:320fde26
Show More
kernel.js
1064 lines | 35.8 KiB | application/javascript | JavascriptLexer
MinRK
allow async output on the most recent request...
r16359 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Jonathan Frederic
Almost done!...
r17198 define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Jonathan Frederic
Almost done!...
r17198 'base/js/utils',
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 './comm',
'./serialize',
'widgets/js/init'
], function(IPython, $, utils, comm, serialize, widgetmanager) {
MinRK
Improvements to kernel.js...
r13187 "use strict";
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
Jessica B. Hamrick
Add docstrings to kernel.js
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)
Matthias BUSSONNIER
start docummenting kernel
r8768 */
Jessica B. Hamrick
Don't actually change kernel constructor signature
r18218 var Kernel = function (kernel_service_url, ws_url, notebook, name) {
Jonathan Frederic
MWE,...
r17200 this.events = notebook.events;
Jessica B. Hamrick
Clean up kernel.js
r18201
Jessica B. Hamrick
Don't actually change kernel constructor signature
r18218 this.id = null;
Jessica B. Hamrick
Clean up kernel.js
r18201 this.name = name;
Min RK
use single WebSocket connection for all channels...
r19824 this.ws = null;
Jessica B. Hamrick
Clean up kernel.js
r18201
MinRK
fix url encoding in services...
r15242 this.kernel_service_url = kernel_service_url;
Jessica B. Hamrick
Don't actually change kernel constructor signature
r18218 this.kernel_url = null;
MinRK
pass ws_url to kernel constructor...
r17308 this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl");
MinRK
restore websocket_url configurable...
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
Clean up kernel.js
r18201
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 = {};
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 this._msg_queue = Promise.resolve();
Thomas Kluyver
Move language info from kernelspec to kernel_info_reply
r18468 this.info_reply = {}; // kernel_info_reply stored here after starting
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
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();
Jonathan Frederic
Almost done!...
r17198 this.comm_manager = new comm.CommManager(this);
Jonathan Frederic
Fix imports of "modules",...
r17202 this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook);
MinRK
allow async output on the most recent request...
r16359
this.last_msg_id = null;
this.last_msg_callbacks = {};
Jessica B. Hamrick
Test for autorestart and failed autorestart
r18233
this._autorestart_attempt = 0;
Jessica B. Hamrick
Keep trying to reconnect
r18236 this._reconnect_attempt = 0;
Min RK
add sticky `Connection lost` notification...
r18730 this.reconnect_limit = 7;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _get_msg
*/
MinRK
support binary message from javascript
r18332 Kernel.prototype._get_msg = function (msg_type, content, metadata, buffers) {
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,
MinRK
add version key to js message headers
r16666 msg_type : msg_type,
version : "5.0"
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 },
MinRK
add message metadata to comm and kernel.send_shell_message
r13217 metadata : metadata || {},
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 content : content,
MinRK
support binary message from javascript
r18332 buffers : buffers || [],
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 parent_header : {}
};
return msg;
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217
/**
* @function bind_events
*/
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.bind_events = function () {
MinRK
bind kernel events in Kernel.bind_events...
r11611 var that = this;
Jonathan Frederic
Almost done!...
r17198 this.events.on('send_input_reply.Kernel', function(evt, data) {
MinRK
bind kernel events in Kernel.bind_events...
r11611 that.send_input_reply(data);
});
Jessica B. Hamrick
Better way of logging events
r18224
Jessica B. Hamrick
Fix bugs with this and that
r18239 var record_status = function (evt, info) {
console.log('Kernel: ' + evt.type + ' (' + info.kernel.id + ')');
Jessica B. Hamrick
Better way of logging events
r18224 };
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 this.events.on('kernel_created.Kernel', record_status);
Jessica B. Hamrick
Rename all status_event to kernel_event
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);
Jessica B. Hamrick
Remove debugging statements
r18226 // these are commented out because they are triggered a lot, but can
// be uncommented for debugging purposes
Jessica B. Hamrick
Rename all status_event to kernel_event
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);
Jessica B. Hamrick
Better way of logging events
r18224 this.events.on('kernel_dead.Kernel', record_status);
Jessica B. Hamrick
Test for autorestart and failed autorestart
r18233
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.on('kernel_ready.Kernel', function () {
Jessica B. Hamrick
Test for autorestart and failed autorestart
r18233 that._autorestart_attempt = 0;
});
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.on('kernel_connected.Kernel', function () {
Jessica B. Hamrick
Keep trying to reconnect
r18236 that._reconnect_attempt = 0;
});
MinRK
Improvements to kernel.js...
r13187 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217
/**
* Initialize the iopub handlers.
*
* @function init_iopub_handlers
*/
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.init_iopub_handlers = function () {
MinRK
pyerr -> error
r16569 var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
MinRK
Improvements to kernel.js...
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));
Nathan Heijermans
This feature was discussed in #6123, but it doesn't look like anything was ever incorporated into the IPython Notebook....
r19164 this.register_iopub_handler('execute_input', $.proxy(this._handle_input_message, this));
MinRK
Improvements to kernel.js...
r13187
MinRK
pyerr -> error
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
Improvements to kernel.js...
r13187 }
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
start docummenting kernel
r8768 /**
Jessica B. Hamrick
Clean up kernel.js
r18201 * GET /api/kernels
Jessica B. Hamrick
Add docstrings to kernel.js
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
Matthias BUSSONNIER
start docummenting kernel
r8768 */
Jessica B. Hamrick
Clean up kernel.js
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
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 /**
Jessica B. Hamrick
Clean up kernel.js
r18201 * POST /api/kernels
Jessica B. Hamrick
Handle query string in Kernel.start
r18213 *
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 * Start a new kernel.
*
Jessica B. Hamrick
Handle query string in Kernel.start
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!
Jessica B. Hamrick
Add docstrings to kernel.js
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
Jessica B. Hamrick
Clean up kernel.js
r18201 */
Jessica B. Hamrick
Handle query string in Kernel.start
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;
}
Min RK
don't trigger kernel_starting after kernel_connected...
r20303 this.events.trigger('kernel_starting.Kernel', {kernel: this});
Jessica B. Hamrick
Clean up kernel.js
r18201 var that = this;
var on_success = function (data, status, xhr) {
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 that.events.trigger('kernel_created.Kernel', {kernel: that});
that._kernel_created(data);
Jessica B. Hamrick
Clean up kernel.js
r18201 if (success) {
success(data, status, xhr);
}
};
Jessica B. Hamrick
Handle query string in Kernel.start
r18213 $.ajax(url, {
Jessica B. Hamrick
Clean up kernel.js
r18201 processData: false,
cache: false,
type: "POST",
Jessica B. Hamrick
Handle query string in Kernel.start
r18213 data: JSON.stringify({name: this.name}),
Jessica B. Hamrick
Clean up kernel.js
r18201 dataType: "json",
success: this._on_success(on_success),
error: this._on_error(error)
});
Jessica B. Hamrick
Handle query string in Kernel.start
r18213
return url;
Jessica B. Hamrick
Clean up kernel.js
r18201 };
/**
* GET /api/kernels/[:kernel_id]
Jessica B. Hamrick
Add docstrings to kernel.js
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
Matthias BUSSONNIER
start docummenting kernel
r8768 */
Jessica B. Hamrick
Clean up kernel.js
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]
Jessica B. Hamrick
Add docstrings to kernel.js
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
Jessica B. Hamrick
Clean up kernel.js
r18201 */
Kernel.prototype.kill = function (success, error) {
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_killed.Kernel', {kernel: this});
Jessica B. Hamrick
Fix shutdown test
r18206 this._kernel_dead();
Jessica B. Hamrick
Clean up kernel.js
r18201 $.ajax(this.kernel_url, {
processData: false,
cache: false,
type: "DELETE",
dataType: "json",
Jessica B. Hamrick
Fix shutdown test
r18206 success: this._on_success(success),
Jessica B. Hamrick
Clean up kernel.js
r18201 error: this._on_error(error)
});
};
/**
* POST /api/kernels/[:kernel_id]/interrupt
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 *
* Interrupt the kernel.
*
* @function interrupt
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up kernel.js
r18201 */
Kernel.prototype.interrupt = function (success, error) {
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_interrupting.Kernel', {kernel: this});
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220
var that = this;
var on_success = function (data, status, xhr) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* get kernel info so we know what state the kernel is in
*/
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 that.kernel_info();
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 if (success) {
success(data, status, xhr);
}
};
Jessica B. Hamrick
Clean up kernel.js
r18201 var url = utils.url_join_encode(this.kernel_url, 'interrupt');
$.ajax(url, {
processData: false,
cache: false,
type: "POST",
dataType: "json",
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 success: this._on_success(on_success),
Jessica B. Hamrick
Clean up kernel.js
r18201 error: this._on_error(error)
});
};
Kernel.prototype.restart = function (success, error) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* POST /api/kernels/[:kernel_id]/restart
*
* Restart the kernel.
*
* @function interrupt
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_restarting.Kernel', {kernel: this});
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 this.stop_channels();
Jessica B. Hamrick
Clean up kernel.js
r18201 var that = this;
var on_success = function (data, status, xhr) {
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 that.events.trigger('kernel_created.Kernel', {kernel: that});
that._kernel_created(data);
Jessica B. Hamrick
Clean up kernel.js
r18201 if (success) {
success(data, status, xhr);
}
};
Jessica B. Hamrick
Make kernel js events clearer and more consistent
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);
}
};
Jessica B. Hamrick
Clean up kernel.js
r18201 var url = utils.url_join_encode(this.kernel_url, 'restart');
$.ajax(url, {
processData: false,
cache: false,
type: "POST",
dataType: "json",
Jessica B. Hamrick
Fix bugs in kernel.js
r18205 success: this._on_success(on_success),
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 error: this._on_error(on_error)
Jessica B. Hamrick
Clean up kernel.js
r18201 });
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 Kernel.prototype.reconnect = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* 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
*/
Min RK
add exponential falloff for reconnect...
r18728 if (this.is_connected()) {
return;
}
Min RK
add sticky `Connection lost` notification...
r18730 this._reconnect_attempt = this._reconnect_attempt + 1;
this.events.trigger('kernel_reconnecting.Kernel', {
kernel: this,
attempt: this._reconnect_attempt,
});
Min RK
add exponential falloff for reconnect...
r18728 this.start_channels();
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 };
Jessica B. Hamrick
Clean up kernel.js
r18201 Kernel.prototype._on_success = function (success) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* 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
*/
Jessica B. Hamrick
Clean up kernel.js
r18201 var that = this;
return function (data, status, xhr) {
Jessica B. Hamrick
Fix bugs in kernel.js
r18205 if (data) {
that.id = data.id;
that.name = data.name;
}
Jessica B. Hamrick
Clean up kernel.js
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) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Handle a failed AJAX request by logging the error message, and
* then optionally calling a provided callback.
*
* @function _on_error
* @param {function} error - callback
*/
Jessica B. Hamrick
Clean up kernel.js
r18201 return function (xhr, status, err) {
utils.log_ajax_error(xhr, status, err);
if (error) {
error(xhr, status, err);
}
};
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 Kernel.prototype._kernel_created = function (data) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Perform necessary tasks once the kernel has been started,
* including actually connecting to the kernel.
*
* @function _kernel_created
* @param {Object} data - information about the kernel including id
*/
Jessica B. Hamrick
Don't actually change kernel constructor signature
r18218 this.id = data.id;
this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.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 };
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 Kernel.prototype._kernel_connected = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Perform necessary tasks once the connection to the kernel has
* been established. This includes requesting information about
* the kernel.
*
* @function _kernel_connected
*/
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_connected.Kernel', {kernel: this});
Jessica B. Hamrick
Fix event tests
r18225 // get kernel info so we know what state the kernel is in
var that = this;
Thomas Kluyver
Move language info from kernelspec to kernel_info_reply
r18468 this.kernel_info(function (reply) {
that.info_reply = reply.content;
Jessica B. Hamrick
Fix bugs with this and that
r18239 that.events.trigger('kernel_ready.Kernel', {kernel: that});
Jessica B. Hamrick
Fix event tests
r18225 });
Jessica B. Hamrick
Fix shutdown test
r18206 };
Kernel.prototype._kernel_dead = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Perform necessary tasks after the kernel has died. This closing
* communication channels to the kernel if they are still somehow
* open.
*
* @function _kernel_dead
*/
Jessica B. Hamrick
Fix shutdown test
r18206 this.stop_channels();
Jessica B. Hamrick
Clean up kernel.js
r18201 };
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 Kernel.prototype.start_channels = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
Min RK
use single WebSocket connection for all channels...
r19824 * Start the websocket channels.
Matthias Bussonnier
Move js donc into function themselves....
r19102 * Will stop and restart them if they already exist.
*
* @function start_channels
*/
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();
MinRK
restore websocket_url configurable...
r17303 var ws_host_url = this.ws_url + this.kernel_url;
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220
MinRK
remove websocket url...
r15400 console.log("Starting WebSockets:", ws_host_url);
MinRK
remove on_first_message authentication...
r18277
Min RK
use single WebSocket connection for all channels...
r19824 this.ws = new this.WebSocket([
MinRK
remove on_first_message authentication...
r18277 that.ws_url,
Min RK
use single WebSocket connection for all channels...
r19824 utils.url_join_encode(that.kernel_url, 'channels'),
MinRK
remove on_first_message authentication...
r18277 "?session_id=" + that.session_id
Min RK
use single WebSocket connection for all channels...
r19824 ].join('')
);
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 ){
Jessica B. Hamrick
Make kernel js events clearer and more consistent
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 () {
Jessica B. Hamrick
Fix bugs with this and that
r18239 that.events.trigger('kernel_dead.Kernel', {kernel: that});
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 that._kernel_dead();
});
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 ){
Jessica B. Hamrick
Clean up kernel.js
r18201 that._ws_closed(ws_host_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
improve indicators and handling of dead kernels and broken websocket connections...
r17676 var ws_error = function(evt){
if (already_called_onclose){
return;
}
already_called_onclose = true;
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 that._ws_closed(ws_host_url, true);
MinRK
improve indicators and handling of dead kernels and broken websocket connections...
r17676 };
Jessica B. Hamrick
Move channels into their own object
r18208
Min RK
use single WebSocket connection for all channels...
r19824 this.ws.onopen = $.proxy(this._ws_opened, this);
this.ws.onclose = ws_closed_early;
this.ws.onerror = ws_error;
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() {
Min RK
use single WebSocket connection for all channels...
r19824 if (that.ws !== null) {
that.ws.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);
Min RK
use single WebSocket connection for all channels...
r19824 this.ws.onmessage = $.proxy(this._handle_ws_message, 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
MinRK
trigger `Kernel.status_started` after websockets open...
r12254 Kernel.prototype._ws_opened = function (evt) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Handle a websocket entering the open state,
Min RK
use single WebSocket connection for all channels...
r19824 * signaling that the kernel is connected when websocket is open.
Matthias Bussonnier
Move js donc into function themselves....
r19102 *
* @function _ws_opened
*/
Jessica B. Hamrick
Clean up kernel.js
r18201 if (this.is_connected()) {
// all events ready, trigger started event.
Jessica B. Hamrick
Move channels into their own object
r18208 this._kernel_connected();
MinRK
trigger `Kernel.status_started` after websockets open...
r12254 }
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 Kernel.prototype._ws_closed = function(ws_url, error) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
Min RK
use single WebSocket connection for all channels...
r19824 * Handle a websocket entering the closed state. If the websocket
Matthias Bussonnier
Move js donc into function themselves....
r19102 * was not closed due to an error, try to reconnect to the kernel.
*
* @function _ws_closed
* @param {string} ws_url - the websocket url
* @param {bool} error - whether the connection was closed due to an error
*/
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 this.stop_channels();
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_disconnected.Kernel', {kernel: this});
Jessica B. Hamrick
Keep trying to reconnect
r18236 if (error) {
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 console.log('WebSocket connection failed: ', ws_url);
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_connection_failed.Kernel', {kernel: this, ws_url: ws_url, attempt: this._reconnect_attempt});
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 }
Min RK
add sticky `Connection lost` notification...
r18730 this._schedule_reconnect();
};
Kernel.prototype._schedule_reconnect = function () {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* function to call when kernel connection is lost
* schedules reconnect, or fires 'connection_dead' if reconnect limit is hit
*/
Min RK
add sticky `Connection lost` notification...
r18730 if (this._reconnect_attempt < this.reconnect_limit) {
Min RK
add exponential falloff for reconnect...
r18728 var timeout = Math.pow(2, this._reconnect_attempt);
console.log("Connection lost, reconnecting in " + timeout + " seconds.");
setTimeout($.proxy(this.reconnect, this), 1e3 * timeout);
} else {
Min RK
add sticky `Connection lost` notification...
r18730 this.events.trigger('kernel_connection_dead.Kernel', {
kernel: this,
reconnect_attempt: this._reconnect_attempt,
});
Min RK
add exponential falloff for reconnect...
r18728 console.log("Failed to reconnect, giving up.");
}
Jessica B. Hamrick
Better respect for abstraction barriers
r18207 };
Min RK
add sticky `Connection lost` notification...
r18730
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 Kernel.prototype.stop_channels = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
Min RK
use single WebSocket connection for all channels...
r19824 * Close the websocket. After successful close, the value
* in `this.ws` will be null.
Matthias Bussonnier
Move js donc into function themselves....
r19102 *
* @function stop_channels
*/
Jessica B. Hamrick
Only set channels to null when they are fully closed
r18209 var that = this;
Min RK
use single WebSocket connection for all channels...
r19824 var close = function () {
if (that.ws && that.ws.readyState === WebSocket.CLOSED) {
that.ws = null;
}
Jessica B. Hamrick
Only set channels to null when they are fully closed
r18209 };
Min RK
use single WebSocket connection for all channels...
r19824 if (this.ws !== null) {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.onclose = close;
this.ws.close();
} else {
close();
MinRK
add stdin to notebook...
r10366 }
MinRK
Improvements to kernel.js...
r13187 }
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 };
Jessica B. Hamrick
Clean up kernel.js
r18201 Kernel.prototype.is_connected = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Check whether there is a connection to the kernel. This
Min RK
use single WebSocket connection for all channels...
r19824 * function only returns true if websocket has been
* created and has a state of WebSocket.OPEN.
Matthias Bussonnier
Move js donc into function themselves....
r19102 *
* @function is_connected
* @returns {bool} - whether there is a connection
*/
Min RK
use single WebSocket connection for all channels...
r19824 // if any channel is not ready, then we're not connected
if (this.ws === null) {
return false;
}
if (this.ws.readyState !== WebSocket.OPEN) {
return false;
Jessica B. Hamrick
Clean up kernel.js
r18201 }
return true;
};
Jessica B. Hamrick
Only set channels to null when they are fully closed
r18209
Kernel.prototype.is_fully_disconnected = function () {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* 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
*/
Min RK
use single WebSocket connection for all channels...
r19824 return (this.ws === null);
Jessica B. Hamrick
Only set channels to null when they are fully closed
r18209 };
MinRK
Improvements to kernel.js...
r13187
MinRK
support binary message from javascript
r18332 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata, buffers) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Send a message on the Kernel's shell channel
*
* @function send_shell_message
*/
Jessica B. Hamrick
Clean up kernel.js
r18201 if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
MinRK
support binary message from javascript
r18332 var msg = this._get_msg(msg_type, content, metadata, buffers);
Min RK
use single WebSocket connection for all channels...
r19824 msg.channel = 'shell';
this.ws.send(serialize.serialize(msg));
MinRK
Improvements to kernel.js...
r13187 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
add js kernel_info request
r14681 Kernel.prototype.kernel_info = function (callback) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Get kernel info
*
* @function kernel_info
* @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)
*/
Matthias BUSSONNIER
add js kernel_info request
r14681 var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
return this.send_shell_message("kernel_info_request", {}, callbacks);
};
MinRK
s/object_info_request/inspect_request
r16587 Kernel.prototype.inspect = function (code, cursor_pos, callback) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Get info on an object
*
* When calling this method, pass a callback function that expects one argument.
* The callback will be passed the complete `inspect_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
*
* @function inspect
* @param code {string}
* @param cursor_pos {integer}
* @param callback {function}
*/
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
MinRK
update completion_ and objection_info_request...
r16580 var content = {
code : code,
cursor_pos : cursor_pos,
Jessica B. Hamrick
Clean up kernel.js
r18201 detail_level : 0
MinRK
update completion_ and objection_info_request...
r16580 };
MinRK
s/object_info_request/inspect_request
r16587 return this.send_shell_message("inspect_request", content, callbacks);
MinRK
Improvements to kernel.js...
r13187 };
Matthias BUSSONNIER
Add Tootip to notebook....
r5397
Brian Granger
Adding options to Kernel.execute with a default of silent=true.
r7176 Kernel.prototype.execute = function (code, callbacks, options) {
Matthias Bussonnier
Move js donc into function themselves....
r19102 /**
* Execute given code into kernel, and pass result to callback.
*
* @async
* @function execute
* @param {string} code
* @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}
* @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 = {
* 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
* }
*
* 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.
*/
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,
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);
Jessica B. Hamrick
Fix bugs with this and that
r18239 this.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 /**
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 * When calling this method, pass a function to be called with the
* `complete_reply` message 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)
*
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 * @function complete
MinRK
update completion_ and objection_info_request...
r16580 * @param code {string}
Matthias BUSSONNIER
start docummenting kernel
r8768 * @param cursor_pos {integer}
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 * @param callback {function}
Matthias BUSSONNIER
start docummenting kernel
r8768 */
MinRK
update completion_ and objection_info_request...
r16580 Kernel.prototype.complete = function (code, cursor_pos, callback) {
MinRK
only pass shell.reply callback to oinfo / complete...
r13208 var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
Brian Granger
Added complete method of JS kernel object.
r4388 var content = {
MinRK
update completion_ and objection_info_request...
r16580 code : code,
Jessica B. Hamrick
Clean up kernel.js
r18201 cursor_pos : cursor_pos
Brian Granger
Added complete method of JS kernel object.
r4388 };
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
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function send_input_reply
*/
MinRK
use inline raw_input instead of a dialog
r10368 Kernel.prototype.send_input_reply = function (input) {
Jessica B. Hamrick
Clean up kernel.js
r18201 if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
MinRK
add stdin to notebook...
r10366 var content = {
Jessica B. Hamrick
Clean up kernel.js
r18201 value : input
MinRK
add stdin to notebook...
r10366 };
Jessica B. Hamrick
Fix bugs with this and that
r18239 this.events.trigger('input_reply.Kernel', {kernel: this, content: content});
MinRK
add stdin to notebook...
r10366 var msg = this._get_msg("input_reply", content);
Min RK
use single WebSocket connection for all channels...
r19824 msg.channel = 'stdin';
this.ws.send(serialize.serialize(msg));
MinRK
add stdin to notebook...
r10366 return msg.header.msg_id;
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function register_iopub_handler
*/
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
this._iopub_handlers[msg_type] = callback;
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* Get the iopub handler for a specific message type.
*
* @function get_iopub_handler
*/
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype.get_iopub_handler = function (msg_type) {
return this._iopub_handlers[msg_type];
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* Get callbacks for a specific message.
*
* @function get_callbacks_for_msg
*/
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
MinRK
allow async output on the most recent request...
r16359 if (msg_id == this.last_msg_id) {
return this.last_msg_callbacks;
} else {
return this._msg_callbacks[msg_id];
}
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* Clear callbacks for a specific message.
*
* @function clear_callbacks_for_msg
*/
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
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _finish_shell
*/
MinRK
allow async output on the most recent request...
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);
}
}
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _finish_iopub
*/
MinRK
allow async output on the most recent request...
r16359 Kernel.prototype._finish_iopub = function (msg_id) {
var callbacks = this._msg_callbacks[msg_id];
if (callbacks !== undefined) {
callbacks.iopub_done = true;
MinRK
remove an inappropriate `!`...
r16599 if (callbacks.shell_done) {
MinRK
allow async output on the most recent request...
r16359 this.clear_callbacks_for_msg(msg_id);
}
}
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* Set callbacks for a particular message.
MinRK
refactor js callbacks...
r13207 * Callbacks should be a struct of the following form:
* shell : {
*
* }
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 *
* @function set_callbacks_for_msg
MinRK
refactor js callbacks...
r13207 */
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
MinRK
allow async output on the most recent request...
r16359 this.last_msg_id = msg_id;
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
MinRK
allow async output on the most recent request...
r16359 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
MinRK
refactor js callbacks...
r13207 cbcopy.shell = callbacks.shell;
cbcopy.iopub = callbacks.iopub;
cbcopy.input = callbacks.input;
MinRK
allow async output on the most recent request...
r16359 cbcopy.shell_done = (!callbacks.shell);
cbcopy.iopub_done = (!callbacks.iopub);
} else {
this.last_msg_callbacks = {};
MinRK
Improvements to kernel.js...
r13187 }
MinRK
add Kernel.clear_callbacks_for_msg
r12555 };
Min RK
use single WebSocket connection for all channels...
r19824
Kernel.prototype._handle_ws_message = function (e) {
Jason Grout
Handle kernel messages synchronously...
r20621 var that = this;
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 this._msg_queue = this._msg_queue.then(function() {
return serialize.deserialize(e.data);
Jason Grout
Handle kernel messages synchronously...
r20621 }).then(function(msg) {return that._finish_ws_message(msg);})
Jason Grout
Fix race condition in javascript kernel message processing...
r20441 .catch(utils.reject("Couldn't process kernel message", true));
MinRK
support buffers in comm messages...
r18329 };
Min RK
use single WebSocket connection for all channels...
r19824 Kernel.prototype._finish_ws_message = function (msg) {
switch (msg.channel) {
case 'shell':
Jason Grout
Handle kernel messages synchronously...
r20621 return this._handle_shell_reply(msg);
Min RK
use single WebSocket connection for all channels...
r19824 break;
case 'iopub':
Jason Grout
Handle kernel messages synchronously...
r20621 return this._handle_iopub_message(msg);
Min RK
use single WebSocket connection for all channels...
r19824 break;
case 'stdin':
Jason Grout
Handle kernel messages synchronously...
r20621 return this._handle_input_request(msg);
Min RK
use single WebSocket connection for all channels...
r19824 break;
default:
console.error("unrecognized message channel", msg.channel, msg);
}
};
Kernel.prototype._handle_shell_reply = function (reply) {
MinRK
support buffers in comm messages...
r18329 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
Jason Grout
Handle kernel messages synchronously...
r20621 var that = this;
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);
Jason Grout
Handle kernel messages synchronously...
r20621 var promise = Promise.resolve();
MinRK
refactor js callbacks...
r13207 if (!callbacks || !callbacks.shell) {
return;
MinRK
Improvements to kernel.js...
r13187 }
MinRK
refactor js callbacks...
r13207 var shell_callbacks = callbacks.shell;
MinRK
allow async output on the most recent request...
r16359 // signal that shell callbacks are done
this._finish_shell(parent_id);
MinRK
refactor js callbacks...
r13207
if (shell_callbacks.reply !== undefined) {
Jason Grout
Handle kernel messages synchronously...
r20621 promise = promise.then(function() {return shell_callbacks.reply(reply)});
MinRK
refactor js callbacks...
r13207 }
if (content.payload && shell_callbacks.payload) {
Jason Grout
Handle kernel messages synchronously...
r20621 promise = promise.then(function() {
return that._handle_payloads(content.payload, shell_callbacks.payload, reply);
});
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 }
Jason Grout
Handle kernel messages synchronously...
r20621 return promise;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _handle_payloads
*/
MinRK
refactor js callbacks...
r13207 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
Jason Grout
Handle kernel messages synchronously...
r20621 var promise = [];
MinRK
refactor js callbacks...
r13207 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) {
Jason Grout
Handle kernel messages synchronously...
r20621 promise.push(callback(payload, msg));
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 }
MinRK
Improvements to kernel.js...
r13187 }
Jason Grout
Handle kernel messages synchronously...
r20621 return Promise.all(promise);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _handle_status_message
*/
MinRK
Improvements to kernel.js...
r13187 Kernel.prototype._handle_status_message = function (msg) {
var execution_state = msg.content.execution_state;
MinRK
allow callbacks on status messages
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
log the error stack for a kernel javascript error message
r14499 console.log("Exception in status msg handler", e, e.stack);
MinRK
allow callbacks on status messages
r13231 }
}
MinRK
Improvements to kernel.js...
r13187 if (execution_state === 'busy') {
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_busy.Kernel', {kernel: this});
Jessica B. Hamrick
Add no_kernel.Kernel event
r18215
MinRK
Improvements to kernel.js...
r13187 } else if (execution_state === 'idle') {
MinRK
allow async output on the most recent request...
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
allow callbacks on status messages
r13231 // trigger status_idle event
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_idle.Kernel', {kernel: this});
Jessica B. Hamrick
Add no_kernel.Kernel event
r18215
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 } else if (execution_state === 'starting') {
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_starting.Kernel', {kernel: this});
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 var that = this;
Thomas Kluyver
Move language info from kernelspec to kernel_info_reply
r18468 this.kernel_info(function (reply) {
that.info_reply = reply.content;
Jessica B. Hamrick
One more this/that change
r18240 that.events.trigger('kernel_ready.Kernel', {kernel: that});
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 });
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220
MinRK
Improvements to kernel.js...
r13187 } else if (execution_state === 'restarting') {
// autorestarting is distinct from restarting,
// in that it means the kernel died and the server is restarting it.
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 // kernel_restarting sets the notification widget,
MinRK
Improvements to kernel.js...
r13187 // autorestart shows the more prominent dialog.
Jessica B. Hamrick
Test for autorestart and failed autorestart
r18233 this._autorestart_attempt = this._autorestart_attempt + 1;
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_restarting.Kernel', {kernel: this});
this.events.trigger('kernel_autorestarting.Kernel', {kernel: this, attempt: this._autorestart_attempt});
Jessica B. Hamrick
Add no_kernel.Kernel event
r18215
MinRK
Improvements to kernel.js...
r13187 } else if (execution_state === 'dead') {
Jessica B. Hamrick
Make kernel js events clearer and more consistent
r18220 this.events.trigger('kernel_dead.Kernel', {kernel: this});
Jessica B. Hamrick
Add no_kernel.Kernel event
r18215 this._kernel_dead();
MinRK
Improvements to kernel.js...
r13187 }
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* Handle clear_output message
*
* @function _handle_clear_output
*/
MinRK
Improvements to kernel.js...
r13187 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
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* handle an output message (execute_result, display_data, etc.)
*
* @function _handle_output_message
*/
MinRK
Improvements to kernel.js...
r13187 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) {
Nathan Heijermans
Backing out all changes to the UI and notebook.js....
r19515 // The message came from another client. Let the UI decide what to
// do with it.
this.events.trigger('received_unsolicited_message.Kernel', msg);
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 }
};
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
Nathan Heijermans
This feature was discussed in #6123, but it doesn't look like anything was ever incorporated into the IPython Notebook....
r19164 * Handle an input message (execute_input).
*
* @function _handle_input message
*/
Kernel.prototype._handle_input_message = function (msg) {
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
Nathan Heijermans
Backing out all changes to the UI and notebook.js....
r19515 if (!callbacks) {
Nathan Heijermans
This feature was discussed in #6123, but it doesn't look like anything was ever incorporated into the IPython Notebook....
r19164 // The message came from another client. Let the UI decide what to
// do with it.
Nathan Heijermans
Backing out all changes to the UI and notebook.js....
r19515 this.events.trigger('received_unsolicited_message.Kernel', msg);
Nathan Heijermans
This feature was discussed in #6123, but it doesn't look like anything was ever incorporated into the IPython Notebook....
r19164 }
};
/**
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 * Dispatch IOPub messages to respective handlers. Each message
* type should have a handler.
*
* @function _handle_iopub_message
*/
Min RK
use single WebSocket connection for all channels...
r19824 Kernel.prototype._handle_iopub_message = function (msg) {
MinRK
Improvements to kernel.js...
r13187 var handler = this.get_iopub_handler(msg.header.msg_type);
if (handler !== undefined) {
Jason Grout
Handle kernel messages synchronously...
r20621 return handler(msg);
MinRK
Improvements to kernel.js...
r13187 }
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 };
Jessica B. Hamrick
Add docstrings to kernel.js
r18217 /**
* @function _handle_input_request
*/
Min RK
use single WebSocket connection for all channels...
r19824 Kernel.prototype._handle_input_request = function (request) {
MinRK
add stdin to notebook...
r10366 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 };
Jonathan Frederic
MWE,...
r17200 // Backwards compatability.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.Kernel = Kernel;
Jonathan Frederic
Return dicts instead of classes,...
r17201 return {'Kernel': Kernel};
Jonathan Frederic
Almost done!...
r17198 });