|
|
|
|
|
//============================================================================
|
|
|
// Kernel
|
|
|
//============================================================================
|
|
|
|
|
|
var IPython = (function (IPython) {
|
|
|
|
|
|
var utils = IPython.utils;
|
|
|
|
|
|
var Kernel = function () {
|
|
|
this.kernel_id = null;
|
|
|
this.base_url = "/kernels";
|
|
|
this.kernel_url = null;
|
|
|
this.shell_channel = null;
|
|
|
this.iopub_channel = null;
|
|
|
this.running = false;
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype.get_msg = function (msg_type, content) {
|
|
|
var msg = {
|
|
|
header : {
|
|
|
msg_id : utils.uuid(),
|
|
|
username : "username",
|
|
|
session: this.session_id,
|
|
|
msg_type : msg_type
|
|
|
},
|
|
|
content : content,
|
|
|
parent_header : {}
|
|
|
};
|
|
|
return msg;
|
|
|
}
|
|
|
|
|
|
Kernel.prototype.start = function (notebook_id, callback) {
|
|
|
var that = this;
|
|
|
if (!this.running) {
|
|
|
var qs = $.param({notebook:notebook_id});
|
|
|
$.post(this.base_url + '?' + qs,
|
|
|
function (kernel_id) {
|
|
|
that._handle_start_kernel(kernel_id, callback);
|
|
|
},
|
|
|
'json'
|
|
|
);
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype.restart = function (callback) {
|
|
|
IPython.kernel_status_widget.status_restarting();
|
|
|
var url = this.kernel_url + "/restart";
|
|
|
var that = this;
|
|
|
if (this.running) {
|
|
|
this.stop_channels();
|
|
|
$.post(url,
|
|
|
function (kernel_id) {
|
|
|
that._handle_start_kernel(kernel_id, callback);
|
|
|
},
|
|
|
'json'
|
|
|
);
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype._handle_start_kernel = function (kernel_id, callback) {
|
|
|
this.running = true;
|
|
|
this.kernel_id = kernel_id;
|
|
|
this.kernel_url = this.base_url + "/" + this.kernel_id;
|
|
|
this.start_channels();
|
|
|
callback();
|
|
|
IPython.kernel_status_widget.status_idle();
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype.start_channels = function () {
|
|
|
this.stop_channels();
|
|
|
var ws_url = "ws://127.0.0.1:8888" + this.kernel_url;
|
|
|
this.shell_channel = new WebSocket(ws_url + "/shell");
|
|
|
this.iopub_channel = new WebSocket(ws_url + "/iopub");
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype.stop_channels = function () {
|
|
|
if (this.shell_channel !== null) {
|
|
|
this.shell_channel.close();
|
|
|
this.shell_channel = null;
|
|
|
};
|
|
|
if (this.iopub_channel !== null) {
|
|
|
this.iopub_channel.close();
|
|
|
this.iopub_channel = null;
|
|
|
};
|
|
|
};
|
|
|
|
|
|
Kernel.prototype.execute = function (code) {
|
|
|
var content = {
|
|
|
code : code,
|
|
|
silent : false,
|
|
|
user_variables : [],
|
|
|
user_expressions : {}
|
|
|
};
|
|
|
var msg = this.get_msg("execute_request", content);
|
|
|
this.shell_channel.send(JSON.stringify(msg));
|
|
|
return msg.header.msg_id;
|
|
|
}
|
|
|
|
|
|
|
|
|
Kernel.prototype.complete = function (line, cursor_pos) {
|
|
|
var content = {
|
|
|
text : '',
|
|
|
line : line,
|
|
|
cursor_pos : cursor_pos
|
|
|
};
|
|
|
var msg = this.get_msg("complete_request", content);
|
|
|
this.shell_channel.send(JSON.stringify(msg));
|
|
|
return msg.header.msg_id;
|
|
|
}
|
|
|
|
|
|
|
|
|
Kernel.prototype.interrupt = function () {
|
|
|
if (this.running) {
|
|
|
$.post(this.kernel_url + "/interrupt");
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
Kernel.prototype.kill = function () {
|
|
|
if (this.running) {
|
|
|
this.running = false;
|
|
|
var settings = {
|
|
|
cache : false,
|
|
|
type : "DELETE",
|
|
|
};
|
|
|
$.ajax(this.kernel_url, settings);
|
|
|
};
|
|
|
};
|
|
|
|
|
|
IPython.Kernel = Kernel;
|
|
|
|
|
|
return IPython;
|
|
|
|
|
|
}(IPython));
|
|
|
|
|
|
|