##// END OF EJS Templates
Backport PR #5804: remove an inappropriate `!`...
Thomas Kluyver -
Show More
@@ -1,622 +1,622 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 //============================================================================
4 //============================================================================
5 // Kernel
5 // Kernel
6 //============================================================================
6 //============================================================================
7
7
8 /**
8 /**
9 * @module IPython
9 * @module IPython
10 * @namespace IPython
10 * @namespace IPython
11 * @submodule Kernel
11 * @submodule Kernel
12 */
12 */
13
13
14 var IPython = (function (IPython) {
14 var IPython = (function (IPython) {
15 "use strict";
15 "use strict";
16
16
17 var utils = IPython.utils;
17 var utils = IPython.utils;
18
18
19 // Initialization and connection.
19 // Initialization and connection.
20 /**
20 /**
21 * A Kernel Class to communicate with the Python kernel
21 * A Kernel Class to communicate with the Python kernel
22 * @Class Kernel
22 * @Class Kernel
23 */
23 */
24 var Kernel = function (kernel_service_url) {
24 var Kernel = function (kernel_service_url) {
25 this.kernel_id = null;
25 this.kernel_id = null;
26 this.shell_channel = null;
26 this.shell_channel = null;
27 this.iopub_channel = null;
27 this.iopub_channel = null;
28 this.stdin_channel = null;
28 this.stdin_channel = null;
29 this.kernel_service_url = kernel_service_url;
29 this.kernel_service_url = kernel_service_url;
30 this.running = false;
30 this.running = false;
31 this.username = "username";
31 this.username = "username";
32 this.session_id = utils.uuid();
32 this.session_id = utils.uuid();
33 this._msg_callbacks = {};
33 this._msg_callbacks = {};
34
34
35 if (typeof(WebSocket) !== 'undefined') {
35 if (typeof(WebSocket) !== 'undefined') {
36 this.WebSocket = WebSocket;
36 this.WebSocket = WebSocket;
37 } else if (typeof(MozWebSocket) !== 'undefined') {
37 } else if (typeof(MozWebSocket) !== 'undefined') {
38 this.WebSocket = MozWebSocket;
38 this.WebSocket = MozWebSocket;
39 } else {
39 } else {
40 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.');
40 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.');
41 }
41 }
42
42
43 this.bind_events();
43 this.bind_events();
44 this.init_iopub_handlers();
44 this.init_iopub_handlers();
45 this.comm_manager = new IPython.CommManager(this);
45 this.comm_manager = new IPython.CommManager(this);
46 this.widget_manager = new IPython.WidgetManager(this.comm_manager);
46 this.widget_manager = new IPython.WidgetManager(this.comm_manager);
47
47
48 this.last_msg_id = null;
48 this.last_msg_id = null;
49 this.last_msg_callbacks = {};
49 this.last_msg_callbacks = {};
50 };
50 };
51
51
52
52
53 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
53 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
54 var msg = {
54 var msg = {
55 header : {
55 header : {
56 msg_id : utils.uuid(),
56 msg_id : utils.uuid(),
57 username : this.username,
57 username : this.username,
58 session : this.session_id,
58 session : this.session_id,
59 msg_type : msg_type
59 msg_type : msg_type
60 },
60 },
61 metadata : metadata || {},
61 metadata : metadata || {},
62 content : content,
62 content : content,
63 parent_header : {}
63 parent_header : {}
64 };
64 };
65 return msg;
65 return msg;
66 };
66 };
67
67
68 Kernel.prototype.bind_events = function () {
68 Kernel.prototype.bind_events = function () {
69 var that = this;
69 var that = this;
70 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
70 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
71 that.send_input_reply(data);
71 that.send_input_reply(data);
72 });
72 });
73 };
73 };
74
74
75 // Initialize the iopub handlers
75 // Initialize the iopub handlers
76
76
77 Kernel.prototype.init_iopub_handlers = function () {
77 Kernel.prototype.init_iopub_handlers = function () {
78 var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
78 var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
79 this._iopub_handlers = {};
79 this._iopub_handlers = {};
80 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
80 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
81 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
81 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
82
82
83 for (var i=0; i < output_types.length; i++) {
83 for (var i=0; i < output_types.length; i++) {
84 this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
84 this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
85 }
85 }
86 };
86 };
87
87
88 /**
88 /**
89 * Start the Python kernel
89 * Start the Python kernel
90 * @method start
90 * @method start
91 */
91 */
92 Kernel.prototype.start = function (params) {
92 Kernel.prototype.start = function (params) {
93 params = params || {};
93 params = params || {};
94 if (!this.running) {
94 if (!this.running) {
95 var qs = $.param(params);
95 var qs = $.param(params);
96 $.post(utils.url_join_encode(this.kernel_service_url) + '?' + qs,
96 $.post(utils.url_join_encode(this.kernel_service_url) + '?' + qs,
97 $.proxy(this._kernel_started, this),
97 $.proxy(this._kernel_started, this),
98 'json'
98 'json'
99 );
99 );
100 }
100 }
101 };
101 };
102
102
103 /**
103 /**
104 * Restart the python kernel.
104 * Restart the python kernel.
105 *
105 *
106 * Emit a 'status_restarting.Kernel' event with
106 * Emit a 'status_restarting.Kernel' event with
107 * the current object as parameter
107 * the current object as parameter
108 *
108 *
109 * @method restart
109 * @method restart
110 */
110 */
111 Kernel.prototype.restart = function () {
111 Kernel.prototype.restart = function () {
112 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
112 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
113 if (this.running) {
113 if (this.running) {
114 this.stop_channels();
114 this.stop_channels();
115 $.post(utils.url_join_encode(this.kernel_url, "restart"),
115 $.post(utils.url_join_encode(this.kernel_url, "restart"),
116 $.proxy(this._kernel_started, this),
116 $.proxy(this._kernel_started, this),
117 'json'
117 'json'
118 );
118 );
119 }
119 }
120 };
120 };
121
121
122
122
123 Kernel.prototype._kernel_started = function (json) {
123 Kernel.prototype._kernel_started = function (json) {
124 console.log("Kernel started: ", json.id);
124 console.log("Kernel started: ", json.id);
125 this.running = true;
125 this.running = true;
126 this.kernel_id = json.id;
126 this.kernel_id = json.id;
127 // trailing 's' in https will become wss for secure web sockets
127 // trailing 's' in https will become wss for secure web sockets
128 this.ws_host = location.protocol.replace('http', 'ws') + "//" + location.host;
128 this.ws_host = location.protocol.replace('http', 'ws') + "//" + location.host;
129 this.kernel_url = utils.url_path_join(this.kernel_service_url, this.kernel_id);
129 this.kernel_url = utils.url_path_join(this.kernel_service_url, this.kernel_id);
130 this.start_channels();
130 this.start_channels();
131 };
131 };
132
132
133
133
134 Kernel.prototype._websocket_closed = function(ws_url, early) {
134 Kernel.prototype._websocket_closed = function(ws_url, early) {
135 this.stop_channels();
135 this.stop_channels();
136 $([IPython.events]).trigger('websocket_closed.Kernel',
136 $([IPython.events]).trigger('websocket_closed.Kernel',
137 {ws_url: ws_url, kernel: this, early: early}
137 {ws_url: ws_url, kernel: this, early: early}
138 );
138 );
139 };
139 };
140
140
141 /**
141 /**
142 * Start the `shell`and `iopub` channels.
142 * Start the `shell`and `iopub` channels.
143 * Will stop and restart them if they already exist.
143 * Will stop and restart them if they already exist.
144 *
144 *
145 * @method start_channels
145 * @method start_channels
146 */
146 */
147 Kernel.prototype.start_channels = function () {
147 Kernel.prototype.start_channels = function () {
148 var that = this;
148 var that = this;
149 this.stop_channels();
149 this.stop_channels();
150 var ws_host_url = this.ws_host + this.kernel_url;
150 var ws_host_url = this.ws_host + this.kernel_url;
151 console.log("Starting WebSockets:", ws_host_url);
151 console.log("Starting WebSockets:", ws_host_url);
152 this.shell_channel = new this.WebSocket(
152 this.shell_channel = new this.WebSocket(
153 this.ws_host + utils.url_join_encode(this.kernel_url, "shell")
153 this.ws_host + utils.url_join_encode(this.kernel_url, "shell")
154 );
154 );
155 this.stdin_channel = new this.WebSocket(
155 this.stdin_channel = new this.WebSocket(
156 this.ws_host + utils.url_join_encode(this.kernel_url, "stdin")
156 this.ws_host + utils.url_join_encode(this.kernel_url, "stdin")
157 );
157 );
158 this.iopub_channel = new this.WebSocket(
158 this.iopub_channel = new this.WebSocket(
159 this.ws_host + utils.url_join_encode(this.kernel_url, "iopub")
159 this.ws_host + utils.url_join_encode(this.kernel_url, "iopub")
160 );
160 );
161
161
162 var already_called_onclose = false; // only alert once
162 var already_called_onclose = false; // only alert once
163 var ws_closed_early = function(evt){
163 var ws_closed_early = function(evt){
164 if (already_called_onclose){
164 if (already_called_onclose){
165 return;
165 return;
166 }
166 }
167 already_called_onclose = true;
167 already_called_onclose = true;
168 if ( ! evt.wasClean ){
168 if ( ! evt.wasClean ){
169 that._websocket_closed(ws_host_url, true);
169 that._websocket_closed(ws_host_url, true);
170 }
170 }
171 };
171 };
172 var ws_closed_late = function(evt){
172 var ws_closed_late = function(evt){
173 if (already_called_onclose){
173 if (already_called_onclose){
174 return;
174 return;
175 }
175 }
176 already_called_onclose = true;
176 already_called_onclose = true;
177 if ( ! evt.wasClean ){
177 if ( ! evt.wasClean ){
178 that._websocket_closed(ws_host_url, false);
178 that._websocket_closed(ws_host_url, false);
179 }
179 }
180 };
180 };
181 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
181 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
182 for (var i=0; i < channels.length; i++) {
182 for (var i=0; i < channels.length; i++) {
183 channels[i].onopen = $.proxy(this._ws_opened, this);
183 channels[i].onopen = $.proxy(this._ws_opened, this);
184 channels[i].onclose = ws_closed_early;
184 channels[i].onclose = ws_closed_early;
185 }
185 }
186 // switch from early-close to late-close message after 1s
186 // switch from early-close to late-close message after 1s
187 setTimeout(function() {
187 setTimeout(function() {
188 for (var i=0; i < channels.length; i++) {
188 for (var i=0; i < channels.length; i++) {
189 if (channels[i] !== null) {
189 if (channels[i] !== null) {
190 channels[i].onclose = ws_closed_late;
190 channels[i].onclose = ws_closed_late;
191 }
191 }
192 }
192 }
193 }, 1000);
193 }, 1000);
194 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
194 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
195 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
195 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
196 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
196 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
197 };
197 };
198
198
199 /**
199 /**
200 * Handle a websocket entering the open state
200 * Handle a websocket entering the open state
201 * sends session and cookie authentication info as first message.
201 * sends session and cookie authentication info as first message.
202 * Once all sockets are open, signal the Kernel.status_started event.
202 * Once all sockets are open, signal the Kernel.status_started event.
203 * @method _ws_opened
203 * @method _ws_opened
204 */
204 */
205 Kernel.prototype._ws_opened = function (evt) {
205 Kernel.prototype._ws_opened = function (evt) {
206 // send the session id so the Session object Python-side
206 // send the session id so the Session object Python-side
207 // has the same identity
207 // has the same identity
208 evt.target.send(this.session_id + ':' + document.cookie);
208 evt.target.send(this.session_id + ':' + document.cookie);
209
209
210 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
210 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
211 for (var i=0; i < channels.length; i++) {
211 for (var i=0; i < channels.length; i++) {
212 // if any channel is not ready, don't trigger event.
212 // if any channel is not ready, don't trigger event.
213 if ( !channels[i].readyState ) return;
213 if ( !channels[i].readyState ) return;
214 }
214 }
215 // all events ready, trigger started event.
215 // all events ready, trigger started event.
216 $([IPython.events]).trigger('status_started.Kernel', {kernel: this});
216 $([IPython.events]).trigger('status_started.Kernel', {kernel: this});
217 };
217 };
218
218
219 /**
219 /**
220 * Stop the websocket channels.
220 * Stop the websocket channels.
221 * @method stop_channels
221 * @method stop_channels
222 */
222 */
223 Kernel.prototype.stop_channels = function () {
223 Kernel.prototype.stop_channels = function () {
224 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
224 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
225 for (var i=0; i < channels.length; i++) {
225 for (var i=0; i < channels.length; i++) {
226 if ( channels[i] !== null ) {
226 if ( channels[i] !== null ) {
227 channels[i].onclose = null;
227 channels[i].onclose = null;
228 channels[i].close();
228 channels[i].close();
229 }
229 }
230 }
230 }
231 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
231 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
232 };
232 };
233
233
234 // Main public methods.
234 // Main public methods.
235
235
236 // send a message on the Kernel's shell channel
236 // send a message on the Kernel's shell channel
237 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
237 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
238 var msg = this._get_msg(msg_type, content, metadata);
238 var msg = this._get_msg(msg_type, content, metadata);
239 this.shell_channel.send(JSON.stringify(msg));
239 this.shell_channel.send(JSON.stringify(msg));
240 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
240 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
241 return msg.header.msg_id;
241 return msg.header.msg_id;
242 };
242 };
243
243
244 /**
244 /**
245 * Get kernel info
245 * Get kernel info
246 *
246 *
247 * @param callback {function}
247 * @param callback {function}
248 * @method object_info
248 * @method object_info
249 *
249 *
250 * When calling this method, pass a callback function that expects one argument.
250 * When calling this method, pass a callback function that expects one argument.
251 * The callback will be passed the complete `kernel_info_reply` message documented
251 * The callback will be passed the complete `kernel_info_reply` message documented
252 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
252 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
253 */
253 */
254 Kernel.prototype.kernel_info = function (callback) {
254 Kernel.prototype.kernel_info = function (callback) {
255 var callbacks;
255 var callbacks;
256 if (callback) {
256 if (callback) {
257 callbacks = { shell : { reply : callback } };
257 callbacks = { shell : { reply : callback } };
258 }
258 }
259 return this.send_shell_message("kernel_info_request", {}, callbacks);
259 return this.send_shell_message("kernel_info_request", {}, callbacks);
260 };
260 };
261
261
262 /**
262 /**
263 * Get info on an object
263 * Get info on an object
264 *
264 *
265 * @param objname {string}
265 * @param objname {string}
266 * @param callback {function}
266 * @param callback {function}
267 * @method object_info
267 * @method object_info
268 *
268 *
269 * When calling this method, pass a callback function that expects one argument.
269 * When calling this method, pass a callback function that expects one argument.
270 * The callback will be passed the complete `object_info_reply` message documented
270 * The callback will be passed the complete `object_info_reply` message documented
271 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
271 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
272 */
272 */
273 Kernel.prototype.object_info = function (objname, callback) {
273 Kernel.prototype.object_info = function (objname, callback) {
274 var callbacks;
274 var callbacks;
275 if (callback) {
275 if (callback) {
276 callbacks = { shell : { reply : callback } };
276 callbacks = { shell : { reply : callback } };
277 }
277 }
278
278
279 if (typeof(objname) !== null && objname !== null) {
279 if (typeof(objname) !== null && objname !== null) {
280 var content = {
280 var content = {
281 oname : objname.toString(),
281 oname : objname.toString(),
282 detail_level : 0,
282 detail_level : 0,
283 };
283 };
284 return this.send_shell_message("object_info_request", content, callbacks);
284 return this.send_shell_message("object_info_request", content, callbacks);
285 }
285 }
286 return;
286 return;
287 };
287 };
288
288
289 /**
289 /**
290 * Execute given code into kernel, and pass result to callback.
290 * Execute given code into kernel, and pass result to callback.
291 *
291 *
292 * @async
292 * @async
293 * @method execute
293 * @method execute
294 * @param {string} code
294 * @param {string} code
295 * @param [callbacks] {Object} With the following keys (all optional)
295 * @param [callbacks] {Object} With the following keys (all optional)
296 * @param callbacks.shell.reply {function}
296 * @param callbacks.shell.reply {function}
297 * @param callbacks.shell.payload.[payload_name] {function}
297 * @param callbacks.shell.payload.[payload_name] {function}
298 * @param callbacks.iopub.output {function}
298 * @param callbacks.iopub.output {function}
299 * @param callbacks.iopub.clear_output {function}
299 * @param callbacks.iopub.clear_output {function}
300 * @param callbacks.input {function}
300 * @param callbacks.input {function}
301 * @param {object} [options]
301 * @param {object} [options]
302 * @param [options.silent=false] {Boolean}
302 * @param [options.silent=false] {Boolean}
303 * @param [options.user_expressions=empty_dict] {Dict}
303 * @param [options.user_expressions=empty_dict] {Dict}
304 * @param [options.user_variables=empty_list] {List od Strings}
304 * @param [options.user_variables=empty_list] {List od Strings}
305 * @param [options.allow_stdin=false] {Boolean} true|false
305 * @param [options.allow_stdin=false] {Boolean} true|false
306 *
306 *
307 * @example
307 * @example
308 *
308 *
309 * The options object should contain the options for the execute call. Its default
309 * The options object should contain the options for the execute call. Its default
310 * values are:
310 * values are:
311 *
311 *
312 * options = {
312 * options = {
313 * silent : true,
313 * silent : true,
314 * user_variables : [],
314 * user_variables : [],
315 * user_expressions : {},
315 * user_expressions : {},
316 * allow_stdin : false
316 * allow_stdin : false
317 * }
317 * }
318 *
318 *
319 * When calling this method pass a callbacks structure of the form:
319 * When calling this method pass a callbacks structure of the form:
320 *
320 *
321 * callbacks = {
321 * callbacks = {
322 * shell : {
322 * shell : {
323 * reply : execute_reply_callback,
323 * reply : execute_reply_callback,
324 * payload : {
324 * payload : {
325 * set_next_input : set_next_input_callback,
325 * set_next_input : set_next_input_callback,
326 * }
326 * }
327 * },
327 * },
328 * iopub : {
328 * iopub : {
329 * output : output_callback,
329 * output : output_callback,
330 * clear_output : clear_output_callback,
330 * clear_output : clear_output_callback,
331 * },
331 * },
332 * input : raw_input_callback
332 * input : raw_input_callback
333 * }
333 * }
334 *
334 *
335 * Each callback will be passed the entire message as a single arugment.
335 * Each callback will be passed the entire message as a single arugment.
336 * Payload handlers will be passed the corresponding payload and the execute_reply message.
336 * Payload handlers will be passed the corresponding payload and the execute_reply message.
337 */
337 */
338 Kernel.prototype.execute = function (code, callbacks, options) {
338 Kernel.prototype.execute = function (code, callbacks, options) {
339
339
340 var content = {
340 var content = {
341 code : code,
341 code : code,
342 silent : true,
342 silent : true,
343 store_history : false,
343 store_history : false,
344 user_variables : [],
344 user_variables : [],
345 user_expressions : {},
345 user_expressions : {},
346 allow_stdin : false
346 allow_stdin : false
347 };
347 };
348 callbacks = callbacks || {};
348 callbacks = callbacks || {};
349 if (callbacks.input !== undefined) {
349 if (callbacks.input !== undefined) {
350 content.allow_stdin = true;
350 content.allow_stdin = true;
351 }
351 }
352 $.extend(true, content, options);
352 $.extend(true, content, options);
353 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
353 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
354 return this.send_shell_message("execute_request", content, callbacks);
354 return this.send_shell_message("execute_request", content, callbacks);
355 };
355 };
356
356
357 /**
357 /**
358 * When calling this method, pass a function to be called with the `complete_reply` message
358 * When calling this method, pass a function to be called with the `complete_reply` message
359 * as its only argument when it arrives.
359 * as its only argument when it arrives.
360 *
360 *
361 * `complete_reply` is documented
361 * `complete_reply` is documented
362 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
362 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
363 *
363 *
364 * @method complete
364 * @method complete
365 * @param line {integer}
365 * @param line {integer}
366 * @param cursor_pos {integer}
366 * @param cursor_pos {integer}
367 * @param callback {function}
367 * @param callback {function}
368 *
368 *
369 */
369 */
370 Kernel.prototype.complete = function (line, cursor_pos, callback) {
370 Kernel.prototype.complete = function (line, cursor_pos, callback) {
371 var callbacks;
371 var callbacks;
372 if (callback) {
372 if (callback) {
373 callbacks = { shell : { reply : callback } };
373 callbacks = { shell : { reply : callback } };
374 }
374 }
375 var content = {
375 var content = {
376 text : '',
376 text : '',
377 line : line,
377 line : line,
378 block : null,
378 block : null,
379 cursor_pos : cursor_pos
379 cursor_pos : cursor_pos
380 };
380 };
381 return this.send_shell_message("complete_request", content, callbacks);
381 return this.send_shell_message("complete_request", content, callbacks);
382 };
382 };
383
383
384
384
385 Kernel.prototype.interrupt = function () {
385 Kernel.prototype.interrupt = function () {
386 if (this.running) {
386 if (this.running) {
387 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
387 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
388 $.post(utils.url_join_encode(this.kernel_url, "interrupt"));
388 $.post(utils.url_join_encode(this.kernel_url, "interrupt"));
389 }
389 }
390 };
390 };
391
391
392
392
393 Kernel.prototype.kill = function () {
393 Kernel.prototype.kill = function () {
394 if (this.running) {
394 if (this.running) {
395 this.running = false;
395 this.running = false;
396 var settings = {
396 var settings = {
397 cache : false,
397 cache : false,
398 type : "DELETE"
398 type : "DELETE"
399 };
399 };
400 $.ajax(utils.url_join_encode(this.kernel_url), settings);
400 $.ajax(utils.url_join_encode(this.kernel_url), settings);
401 }
401 }
402 };
402 };
403
403
404 Kernel.prototype.send_input_reply = function (input) {
404 Kernel.prototype.send_input_reply = function (input) {
405 var content = {
405 var content = {
406 value : input,
406 value : input,
407 };
407 };
408 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
408 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
409 var msg = this._get_msg("input_reply", content);
409 var msg = this._get_msg("input_reply", content);
410 this.stdin_channel.send(JSON.stringify(msg));
410 this.stdin_channel.send(JSON.stringify(msg));
411 return msg.header.msg_id;
411 return msg.header.msg_id;
412 };
412 };
413
413
414
414
415 // Reply handlers
415 // Reply handlers
416
416
417 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
417 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
418 this._iopub_handlers[msg_type] = callback;
418 this._iopub_handlers[msg_type] = callback;
419 };
419 };
420
420
421 Kernel.prototype.get_iopub_handler = function (msg_type) {
421 Kernel.prototype.get_iopub_handler = function (msg_type) {
422 // get iopub handler for a specific message type
422 // get iopub handler for a specific message type
423 return this._iopub_handlers[msg_type];
423 return this._iopub_handlers[msg_type];
424 };
424 };
425
425
426
426
427 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
427 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
428 // get callbacks for a specific message
428 // get callbacks for a specific message
429 if (msg_id == this.last_msg_id) {
429 if (msg_id == this.last_msg_id) {
430 return this.last_msg_callbacks;
430 return this.last_msg_callbacks;
431 } else {
431 } else {
432 return this._msg_callbacks[msg_id];
432 return this._msg_callbacks[msg_id];
433 }
433 }
434 };
434 };
435
435
436
436
437 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
437 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
438 if (this._msg_callbacks[msg_id] !== undefined ) {
438 if (this._msg_callbacks[msg_id] !== undefined ) {
439 delete this._msg_callbacks[msg_id];
439 delete this._msg_callbacks[msg_id];
440 }
440 }
441 };
441 };
442
442
443 Kernel.prototype._finish_shell = function (msg_id) {
443 Kernel.prototype._finish_shell = function (msg_id) {
444 var callbacks = this._msg_callbacks[msg_id];
444 var callbacks = this._msg_callbacks[msg_id];
445 if (callbacks !== undefined) {
445 if (callbacks !== undefined) {
446 callbacks.shell_done = true;
446 callbacks.shell_done = true;
447 if (callbacks.iopub_done) {
447 if (callbacks.iopub_done) {
448 this.clear_callbacks_for_msg(msg_id);
448 this.clear_callbacks_for_msg(msg_id);
449 }
449 }
450 }
450 }
451 };
451 };
452
452
453 Kernel.prototype._finish_iopub = function (msg_id) {
453 Kernel.prototype._finish_iopub = function (msg_id) {
454 var callbacks = this._msg_callbacks[msg_id];
454 var callbacks = this._msg_callbacks[msg_id];
455 if (callbacks !== undefined) {
455 if (callbacks !== undefined) {
456 callbacks.iopub_done = true;
456 callbacks.iopub_done = true;
457 if (!callbacks.shell_done) {
457 if (callbacks.shell_done) {
458 this.clear_callbacks_for_msg(msg_id);
458 this.clear_callbacks_for_msg(msg_id);
459 }
459 }
460 }
460 }
461 };
461 };
462
462
463 /* Set callbacks for a particular message.
463 /* Set callbacks for a particular message.
464 * Callbacks should be a struct of the following form:
464 * Callbacks should be a struct of the following form:
465 * shell : {
465 * shell : {
466 *
466 *
467 * }
467 * }
468
468
469 */
469 */
470 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
470 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
471 this.last_msg_id = msg_id;
471 this.last_msg_id = msg_id;
472 if (callbacks) {
472 if (callbacks) {
473 // shallow-copy mapping, because we will modify it at the top level
473 // shallow-copy mapping, because we will modify it at the top level
474 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
474 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
475 cbcopy.shell = callbacks.shell;
475 cbcopy.shell = callbacks.shell;
476 cbcopy.iopub = callbacks.iopub;
476 cbcopy.iopub = callbacks.iopub;
477 cbcopy.input = callbacks.input;
477 cbcopy.input = callbacks.input;
478 cbcopy.shell_done = (!callbacks.shell);
478 cbcopy.shell_done = (!callbacks.shell);
479 cbcopy.iopub_done = (!callbacks.iopub);
479 cbcopy.iopub_done = (!callbacks.iopub);
480 } else {
480 } else {
481 this.last_msg_callbacks = {};
481 this.last_msg_callbacks = {};
482 }
482 }
483 };
483 };
484
484
485
485
486 Kernel.prototype._handle_shell_reply = function (e) {
486 Kernel.prototype._handle_shell_reply = function (e) {
487 var reply = $.parseJSON(e.data);
487 var reply = $.parseJSON(e.data);
488 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
488 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
489 var content = reply.content;
489 var content = reply.content;
490 var metadata = reply.metadata;
490 var metadata = reply.metadata;
491 var parent_id = reply.parent_header.msg_id;
491 var parent_id = reply.parent_header.msg_id;
492 var callbacks = this.get_callbacks_for_msg(parent_id);
492 var callbacks = this.get_callbacks_for_msg(parent_id);
493 if (!callbacks || !callbacks.shell) {
493 if (!callbacks || !callbacks.shell) {
494 return;
494 return;
495 }
495 }
496 var shell_callbacks = callbacks.shell;
496 var shell_callbacks = callbacks.shell;
497
497
498 // signal that shell callbacks are done
498 // signal that shell callbacks are done
499 this._finish_shell(parent_id);
499 this._finish_shell(parent_id);
500
500
501 if (shell_callbacks.reply !== undefined) {
501 if (shell_callbacks.reply !== undefined) {
502 shell_callbacks.reply(reply);
502 shell_callbacks.reply(reply);
503 }
503 }
504 if (content.payload && shell_callbacks.payload) {
504 if (content.payload && shell_callbacks.payload) {
505 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
505 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
506 }
506 }
507 };
507 };
508
508
509
509
510 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
510 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
511 var l = payloads.length;
511 var l = payloads.length;
512 // Payloads are handled by triggering events because we don't want the Kernel
512 // Payloads are handled by triggering events because we don't want the Kernel
513 // to depend on the Notebook or Pager classes.
513 // to depend on the Notebook or Pager classes.
514 for (var i=0; i<l; i++) {
514 for (var i=0; i<l; i++) {
515 var payload = payloads[i];
515 var payload = payloads[i];
516 var callback = payload_callbacks[payload.source];
516 var callback = payload_callbacks[payload.source];
517 if (callback) {
517 if (callback) {
518 callback(payload, msg);
518 callback(payload, msg);
519 }
519 }
520 }
520 }
521 };
521 };
522
522
523 Kernel.prototype._handle_status_message = function (msg) {
523 Kernel.prototype._handle_status_message = function (msg) {
524 var execution_state = msg.content.execution_state;
524 var execution_state = msg.content.execution_state;
525 var parent_id = msg.parent_header.msg_id;
525 var parent_id = msg.parent_header.msg_id;
526
526
527 // dispatch status msg callbacks, if any
527 // dispatch status msg callbacks, if any
528 var callbacks = this.get_callbacks_for_msg(parent_id);
528 var callbacks = this.get_callbacks_for_msg(parent_id);
529 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
529 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
530 try {
530 try {
531 callbacks.iopub.status(msg);
531 callbacks.iopub.status(msg);
532 } catch (e) {
532 } catch (e) {
533 console.log("Exception in status msg handler", e, e.stack);
533 console.log("Exception in status msg handler", e, e.stack);
534 }
534 }
535 }
535 }
536
536
537 if (execution_state === 'busy') {
537 if (execution_state === 'busy') {
538 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
538 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
539 } else if (execution_state === 'idle') {
539 } else if (execution_state === 'idle') {
540 // signal that iopub callbacks are (probably) done
540 // signal that iopub callbacks are (probably) done
541 // async output may still arrive,
541 // async output may still arrive,
542 // but only for the most recent request
542 // but only for the most recent request
543 this._finish_iopub(parent_id);
543 this._finish_iopub(parent_id);
544
544
545 // trigger status_idle event
545 // trigger status_idle event
546 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
546 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
547 } else if (execution_state === 'restarting') {
547 } else if (execution_state === 'restarting') {
548 // autorestarting is distinct from restarting,
548 // autorestarting is distinct from restarting,
549 // in that it means the kernel died and the server is restarting it.
549 // in that it means the kernel died and the server is restarting it.
550 // status_restarting sets the notification widget,
550 // status_restarting sets the notification widget,
551 // autorestart shows the more prominent dialog.
551 // autorestart shows the more prominent dialog.
552 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
552 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
553 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
553 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
554 } else if (execution_state === 'dead') {
554 } else if (execution_state === 'dead') {
555 this.stop_channels();
555 this.stop_channels();
556 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
556 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
557 }
557 }
558 };
558 };
559
559
560
560
561 // handle clear_output message
561 // handle clear_output message
562 Kernel.prototype._handle_clear_output = function (msg) {
562 Kernel.prototype._handle_clear_output = function (msg) {
563 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
563 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
564 if (!callbacks || !callbacks.iopub) {
564 if (!callbacks || !callbacks.iopub) {
565 return;
565 return;
566 }
566 }
567 var callback = callbacks.iopub.clear_output;
567 var callback = callbacks.iopub.clear_output;
568 if (callback) {
568 if (callback) {
569 callback(msg);
569 callback(msg);
570 }
570 }
571 };
571 };
572
572
573
573
574 // handle an output message (pyout, display_data, etc.)
574 // handle an output message (pyout, display_data, etc.)
575 Kernel.prototype._handle_output_message = function (msg) {
575 Kernel.prototype._handle_output_message = function (msg) {
576 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
576 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
577 if (!callbacks || !callbacks.iopub) {
577 if (!callbacks || !callbacks.iopub) {
578 return;
578 return;
579 }
579 }
580 var callback = callbacks.iopub.output;
580 var callback = callbacks.iopub.output;
581 if (callback) {
581 if (callback) {
582 callback(msg);
582 callback(msg);
583 }
583 }
584 };
584 };
585
585
586 // dispatch IOPub messages to respective handlers.
586 // dispatch IOPub messages to respective handlers.
587 // each message type should have a handler.
587 // each message type should have a handler.
588 Kernel.prototype._handle_iopub_message = function (e) {
588 Kernel.prototype._handle_iopub_message = function (e) {
589 var msg = $.parseJSON(e.data);
589 var msg = $.parseJSON(e.data);
590
590
591 var handler = this.get_iopub_handler(msg.header.msg_type);
591 var handler = this.get_iopub_handler(msg.header.msg_type);
592 if (handler !== undefined) {
592 if (handler !== undefined) {
593 handler(msg);
593 handler(msg);
594 }
594 }
595 };
595 };
596
596
597
597
598 Kernel.prototype._handle_input_request = function (e) {
598 Kernel.prototype._handle_input_request = function (e) {
599 var request = $.parseJSON(e.data);
599 var request = $.parseJSON(e.data);
600 var header = request.header;
600 var header = request.header;
601 var content = request.content;
601 var content = request.content;
602 var metadata = request.metadata;
602 var metadata = request.metadata;
603 var msg_type = header.msg_type;
603 var msg_type = header.msg_type;
604 if (msg_type !== 'input_request') {
604 if (msg_type !== 'input_request') {
605 console.log("Invalid input request!", request);
605 console.log("Invalid input request!", request);
606 return;
606 return;
607 }
607 }
608 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
608 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
609 if (callbacks) {
609 if (callbacks) {
610 if (callbacks.input) {
610 if (callbacks.input) {
611 callbacks.input(request);
611 callbacks.input(request);
612 }
612 }
613 }
613 }
614 };
614 };
615
615
616
616
617 IPython.Kernel = Kernel;
617 IPython.Kernel = Kernel;
618
618
619 return IPython;
619 return IPython;
620
620
621 }(IPython));
621 }(IPython));
622
622
General Comments 0
You need to be logged in to leave comments. Login now