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