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