|
|
|
|
@ -9,12 +9,22 @@
|
|
|
|
|
// Kernel
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An extendable module that provide base functionnality to create cell for notebook.
|
|
|
|
|
* @module IPython
|
|
|
|
|
* @namespace IPython
|
|
|
|
|
* @submodule Kernel
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var IPython = (function (IPython) {
|
|
|
|
|
|
|
|
|
|
var utils = IPython.utils;
|
|
|
|
|
|
|
|
|
|
// Initialization and connection.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A Kernel Class to communicate with the Python kernel
|
|
|
|
|
* @Class Kernel
|
|
|
|
|
*/
|
|
|
|
|
var Kernel = function (base_url) {
|
|
|
|
|
this.kernel_id = null;
|
|
|
|
|
this.shell_channel = null;
|
|
|
|
|
@ -50,6 +60,10 @@ var IPython = (function (IPython) {
|
|
|
|
|
return msg;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start the Python kernel
|
|
|
|
|
* @method start
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.start = function (notebook_id) {
|
|
|
|
|
var that = this;
|
|
|
|
|
if (!this.running) {
|
|
|
|
|
@ -62,7 +76,14 @@ var IPython = (function (IPython) {
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restart the python kernel.
|
|
|
|
|
*
|
|
|
|
|
* Emit a 'status_restarting.Kernel' event with
|
|
|
|
|
* the current object as parameter
|
|
|
|
|
*
|
|
|
|
|
* @method restart
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.restart = function () {
|
|
|
|
|
$([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
|
|
|
|
|
var that = this;
|
|
|
|
|
@ -122,6 +143,12 @@ var IPython = (function (IPython) {
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start the `shell`and `iopub` channels.
|
|
|
|
|
* Will stop and restart them if they already exist.
|
|
|
|
|
*
|
|
|
|
|
* @method start_channels
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.start_channels = function () {
|
|
|
|
|
var that = this;
|
|
|
|
|
this.stop_channels();
|
|
|
|
|
@ -162,7 +189,10 @@ var IPython = (function (IPython) {
|
|
|
|
|
}, 1000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start the `shell`and `iopub` channels.
|
|
|
|
|
* @method stop_channels
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.stop_channels = function () {
|
|
|
|
|
if (this.shell_channel !== null) {
|
|
|
|
|
this.shell_channel.onclose = function (evt) {};
|
|
|
|
|
@ -178,17 +208,28 @@ var IPython = (function (IPython) {
|
|
|
|
|
|
|
|
|
|
// Main public methods.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get info on object asynchronoulsy
|
|
|
|
|
*
|
|
|
|
|
* @async
|
|
|
|
|
* @param objname {string}
|
|
|
|
|
* @param callback {dict}
|
|
|
|
|
* @method object_info_request
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* When calling this method pass a callbacks structure of the form:
|
|
|
|
|
*
|
|
|
|
|
* callbacks = {
|
|
|
|
|
* 'object_info_reply': object_info_reply_callback
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* The `object_info_reply_callback` will be passed the content object of the
|
|
|
|
|
*
|
|
|
|
|
* `object_into_reply` message documented in
|
|
|
|
|
* [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.object_info_request = function (objname, callbacks) {
|
|
|
|
|
// When calling this method pass a callbacks structure of the form:
|
|
|
|
|
//
|
|
|
|
|
// callbacks = {
|
|
|
|
|
// 'object_info_reply': object_into_reply_callback
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// The object_info_reply_callback will be passed the content object of the
|
|
|
|
|
// object_into_reply message documented here:
|
|
|
|
|
//
|
|
|
|
|
// http://ipython.org/ipython-doc/dev/development/messaging.html#object-information
|
|
|
|
|
if(typeof(objname)!=null && objname!=null)
|
|
|
|
|
{
|
|
|
|
|
var content = {
|
|
|
|
|
@ -202,42 +243,61 @@ var IPython = (function (IPython) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute given code into kernel, and pass result to callback.
|
|
|
|
|
*
|
|
|
|
|
* @async
|
|
|
|
|
* @method execute
|
|
|
|
|
* @param {string} code
|
|
|
|
|
* @param callback {Object} With the following keys
|
|
|
|
|
* @param callback.'execute_reply' {function}
|
|
|
|
|
* @param callback.'output' {function}
|
|
|
|
|
* @param callback.'clear_output' {function}
|
|
|
|
|
* @param callback.'set_next_input' {function}
|
|
|
|
|
* @param {object} [options]
|
|
|
|
|
* @param [options.silent=false] {Boolean}
|
|
|
|
|
* @param [options.user_expressions=empty_dict] {Dict}
|
|
|
|
|
* @param [options.user_variables=empty_list] {List od Strings}
|
|
|
|
|
* @param [options.allow_stdin=false] {Boolean} true|false
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* The options object should contain the options for the execute call. Its default
|
|
|
|
|
* values are:
|
|
|
|
|
*
|
|
|
|
|
* options = {
|
|
|
|
|
* silent : true,
|
|
|
|
|
* user_variables : [],
|
|
|
|
|
* user_expressions : {},
|
|
|
|
|
* allow_stdin : false
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* When calling this method pass a callbacks structure of the form:
|
|
|
|
|
*
|
|
|
|
|
* callbacks = {
|
|
|
|
|
* 'execute_reply': execute_reply_callback,
|
|
|
|
|
* 'output': output_callback,
|
|
|
|
|
* 'clear_output': clear_output_callback,
|
|
|
|
|
* 'set_next_input': set_next_input_callback
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* The `execute_reply_callback` will be passed the content and metadata
|
|
|
|
|
* objects of the `execute_reply` message documented
|
|
|
|
|
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute)
|
|
|
|
|
*
|
|
|
|
|
* The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr')
|
|
|
|
|
* of the output and the content and metadata objects of the PUB/SUB channel that contains the
|
|
|
|
|
* output:
|
|
|
|
|
*
|
|
|
|
|
* http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket
|
|
|
|
|
*
|
|
|
|
|
* The `clear_output_callback` will be passed a content object that contains
|
|
|
|
|
* stdout, stderr and other fields that are booleans, as well as the metadata object.
|
|
|
|
|
*
|
|
|
|
|
* The `set_next_input_callback` will be passed the text that should become the next
|
|
|
|
|
* input cell.
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.execute = function (code, callbacks, options) {
|
|
|
|
|
// The options object should contain the options for the execute call. Its default
|
|
|
|
|
// values are:
|
|
|
|
|
//
|
|
|
|
|
// options = {
|
|
|
|
|
// silent : true,
|
|
|
|
|
// user_variables : [],
|
|
|
|
|
// user_expressions : {},
|
|
|
|
|
// allow_stdin : false
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// When calling this method pass a callbacks structure of the form:
|
|
|
|
|
//
|
|
|
|
|
// callbacks = {
|
|
|
|
|
// 'execute_reply': execute_reply_callback,
|
|
|
|
|
// 'output': output_callback,
|
|
|
|
|
// 'clear_output': clear_output_callback,
|
|
|
|
|
// 'set_next_input': set_next_input_callback
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// The execute_reply_callback will be passed the content and metadata objects of the execute_reply
|
|
|
|
|
// message documented here:
|
|
|
|
|
//
|
|
|
|
|
// http://ipython.org/ipython-doc/dev/development/messaging.html#execute
|
|
|
|
|
//
|
|
|
|
|
// The output_callback will be passed msg_type ('stream','display_data','pyout','pyerr')
|
|
|
|
|
// of the output and the content and metadata objects of the PUB/SUB channel that contains the
|
|
|
|
|
// output:
|
|
|
|
|
//
|
|
|
|
|
// http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket
|
|
|
|
|
//
|
|
|
|
|
// The clear_output_callback will be passed a content object that contains
|
|
|
|
|
// stdout, stderr and other fields that are booleans, as well as the metadata object.
|
|
|
|
|
//
|
|
|
|
|
// The set_next_input_callback will be passed the text that should become the next
|
|
|
|
|
// input cell.
|
|
|
|
|
|
|
|
|
|
var content = {
|
|
|
|
|
code : code,
|
|
|
|
|
@ -254,18 +314,25 @@ var IPython = (function (IPython) {
|
|
|
|
|
return msg.header.msg_id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When calling this method pass a callbacks structure of the form:
|
|
|
|
|
*
|
|
|
|
|
* callbacks = {
|
|
|
|
|
* 'complete_reply': complete_reply_callback
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* The `complete_reply_callback` will be passed the content object of the
|
|
|
|
|
* `complete_reply` message documented
|
|
|
|
|
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
|
|
|
|
|
*
|
|
|
|
|
* @method complete
|
|
|
|
|
* @param line {integer}
|
|
|
|
|
* @param cursor_pos {integer}
|
|
|
|
|
* @param {dict} callbacks
|
|
|
|
|
* @param callbacks.complete_reply {function} `complete_reply_callback`
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
Kernel.prototype.complete = function (line, cursor_pos, callbacks) {
|
|
|
|
|
// When calling this method pass a callbacks structure of the form:
|
|
|
|
|
//
|
|
|
|
|
// callbacks = {
|
|
|
|
|
// 'complete_reply': complete_reply_callback
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// The complete_reply_callback will be passed the content object of the
|
|
|
|
|
// complete_reply message documented here:
|
|
|
|
|
//
|
|
|
|
|
// http://ipython.org/ipython-doc/dev/development/messaging.html#complete
|
|
|
|
|
callbacks = callbacks || {};
|
|
|
|
|
var content = {
|
|
|
|
|
text : '',
|
|
|
|
|
|