kernel.js
106 lines
| 2.8 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4349 | |||
//============================================================================ | ||||
// Kernel | ||||
//============================================================================ | ||||
Brian E. Granger
|
r4352 | var IPython = (function (IPython) { | ||
Brian E. Granger
|
r4349 | |||
Brian E. Granger
|
r4352 | var utils = IPython.utils; | ||
var Kernel = function () { | ||||
this.kernel_id = null; | ||||
this.base_url = "/kernels"; | ||||
this.kernel_url = null; | ||||
}; | ||||
Kernel.prototype.get_msg = function (msg_type, content) { | ||||
var msg = { | ||||
header : { | ||||
msg_id : utils.uuid(), | ||||
Brian E. Granger
|
r4389 | username : "username", | ||
Brian E. Granger
|
r4352 | session: this.session_id, | ||
msg_type : msg_type | ||||
}, | ||||
content : content, | ||||
parent_header : {} | ||||
}; | ||||
return msg; | ||||
} | ||||
Brian E. Granger
|
r4494 | Kernel.prototype.start_kernel = function (notebook_id, callback) { | ||
Brian E. Granger
|
r4352 | var that = this; | ||
Brian E. Granger
|
r4494 | var qs = $.param({notebook:notebook_id}); | ||
$.post(this.base_url + '?' + qs, | ||||
Brian E. Granger
|
r4352 | function (kernel_id) { | ||
Brian E. Granger
|
r4353 | that._handle_start_kernel(kernel_id, callback); | ||
Brian E. Granger
|
r4352 | }, | ||
'json' | ||||
); | ||||
}; | ||||
Brian E. Granger
|
r4353 | Kernel.prototype._handle_start_kernel = function (kernel_id, callback) { | ||
Brian E. Granger
|
r4352 | this.kernel_id = kernel_id; | ||
this.kernel_url = this.base_url + "/" + this.kernel_id; | ||||
this._start_channels(); | ||||
Brian E. Granger
|
r4353 | callback(); | ||
Brian E. Granger
|
r4352 | }; | ||
Kernel.prototype._start_channels = function () { | ||||
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.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; | ||||
} | ||||
Brian Granger
|
r4388 | 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; | ||||
} | ||||
Brian E. Granger
|
r4352 | Kernel.prototype.interrupt = function () { | ||
$.post(this.kernel_url + "/interrupt"); | ||||
Brian E. Granger
|
r4349 | }; | ||
Brian E. Granger
|
r4352 | |||
Kernel.prototype.restart = function () { | ||||
Brian E. Granger
|
r4372 | IPython.kernel_status_widget.status_restarting(); | ||
Brian E. Granger
|
r4352 | url = this.kernel_url + "/restart" | ||
var that = this; | ||||
$.post(url, function (kernel_id) { | ||||
console.log("Kernel restarted: " + kernel_id); | ||||
that.kernel_id = kernel_id; | ||||
that.kernel_url = that.base_url + "/" + that.kernel_id; | ||||
Brian E. Granger
|
r4372 | IPython.kernel_status_widget.status_idle(); | ||
Brian E. Granger
|
r4352 | }, 'json'); | ||
Brian E. Granger
|
r4349 | }; | ||
Brian E. Granger
|
r4352 | IPython.Kernel = Kernel; | ||
return IPython; | ||||
}(IPython)); | ||||