##// END OF EJS Templates
log the error stack for a kernel javascript error message
Jason Grout -
Show More
@@ -1,584 +1,584 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Kernel
9 // Kernel
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule Kernel
15 * @submodule Kernel
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 var utils = IPython.utils;
21 var utils = IPython.utils;
22
22
23 // Initialization and connection.
23 // Initialization and connection.
24 /**
24 /**
25 * A Kernel Class to communicate with the Python kernel
25 * A Kernel Class to communicate with the Python kernel
26 * @Class Kernel
26 * @Class Kernel
27 */
27 */
28 var Kernel = function (base_url) {
28 var Kernel = function (base_url) {
29 this.kernel_id = null;
29 this.kernel_id = null;
30 this.shell_channel = null;
30 this.shell_channel = null;
31 this.iopub_channel = null;
31 this.iopub_channel = null;
32 this.stdin_channel = null;
32 this.stdin_channel = null;
33 this.base_url = base_url;
33 this.base_url = base_url;
34 this.running = false;
34 this.running = false;
35 this.username = "username";
35 this.username = "username";
36 this.session_id = utils.uuid();
36 this.session_id = utils.uuid();
37 this._msg_callbacks = {};
37 this._msg_callbacks = {};
38
38
39 if (typeof(WebSocket) !== 'undefined') {
39 if (typeof(WebSocket) !== 'undefined') {
40 this.WebSocket = WebSocket;
40 this.WebSocket = WebSocket;
41 } else if (typeof(MozWebSocket) !== 'undefined') {
41 } else if (typeof(MozWebSocket) !== 'undefined') {
42 this.WebSocket = MozWebSocket;
42 this.WebSocket = MozWebSocket;
43 } else {
43 } else {
44 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.');
44 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.');
45 }
45 }
46
46
47 this.bind_events();
47 this.bind_events();
48 this.init_iopub_handlers();
48 this.init_iopub_handlers();
49 this.comm_manager = new IPython.CommManager(this);
49 this.comm_manager = new IPython.CommManager(this);
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 var url = this.base_url + '?' + qs;
96 var url = this.base_url + '?' + qs;
97 $.post(url,
97 $.post(url,
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 var url = utils.url_join_encode(this.kernel_url, "restart");
116 var url = utils.url_join_encode(this.kernel_url, "restart");
117 $.post(url,
117 $.post(url,
118 $.proxy(this._kernel_started, this),
118 $.proxy(this._kernel_started, this),
119 'json'
119 'json'
120 );
120 );
121 }
121 }
122 };
122 };
123
123
124
124
125 Kernel.prototype._kernel_started = function (json) {
125 Kernel.prototype._kernel_started = function (json) {
126 console.log("Kernel started: ", json.id);
126 console.log("Kernel started: ", json.id);
127 this.running = true;
127 this.running = true;
128 this.kernel_id = json.id;
128 this.kernel_id = json.id;
129 var ws_url = json.ws_url;
129 var ws_url = json.ws_url;
130 if (ws_url.match(/wss?:\/\//) === null) {
130 if (ws_url.match(/wss?:\/\//) === null) {
131 // trailing 's' in https will become wss for secure web sockets
131 // trailing 's' in https will become wss for secure web sockets
132 var prot = location.protocol.replace('http', 'ws') + "//";
132 var prot = location.protocol.replace('http', 'ws') + "//";
133 ws_url = prot + location.host + ws_url;
133 ws_url = prot + location.host + ws_url;
134 }
134 }
135 this.ws_url = ws_url;
135 this.ws_url = ws_url;
136 this.kernel_url = utils.url_join_encode(this.base_url, this.kernel_id);
136 this.kernel_url = utils.url_join_encode(this.base_url, this.kernel_id);
137 this.start_channels();
137 this.start_channels();
138 };
138 };
139
139
140
140
141 Kernel.prototype._websocket_closed = function(ws_url, early) {
141 Kernel.prototype._websocket_closed = function(ws_url, early) {
142 this.stop_channels();
142 this.stop_channels();
143 $([IPython.events]).trigger('websocket_closed.Kernel',
143 $([IPython.events]).trigger('websocket_closed.Kernel',
144 {ws_url: ws_url, kernel: this, early: early}
144 {ws_url: ws_url, kernel: this, early: early}
145 );
145 );
146 };
146 };
147
147
148 /**
148 /**
149 * Start the `shell`and `iopub` channels.
149 * Start the `shell`and `iopub` channels.
150 * Will stop and restart them if they already exist.
150 * Will stop and restart them if they already exist.
151 *
151 *
152 * @method start_channels
152 * @method start_channels
153 */
153 */
154 Kernel.prototype.start_channels = function () {
154 Kernel.prototype.start_channels = function () {
155 var that = this;
155 var that = this;
156 this.stop_channels();
156 this.stop_channels();
157 var ws_url = this.ws_url + this.kernel_url;
157 var ws_url = this.ws_url + this.kernel_url;
158 console.log("Starting WebSockets:", ws_url);
158 console.log("Starting WebSockets:", ws_url);
159 this.shell_channel = new this.WebSocket(ws_url + "/shell");
159 this.shell_channel = new this.WebSocket(ws_url + "/shell");
160 this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
160 this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
161 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
161 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
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_url, true);
170 that._websocket_closed(ws_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_url, false);
179 that._websocket_closed(ws_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 info on an object
246 * Get info on an object
247 *
247 *
248 * @param objname {string}
248 * @param objname {string}
249 * @param callback {function}
249 * @param callback {function}
250 * @method object_info
250 * @method object_info
251 *
251 *
252 * When calling this method, pass a callback function that expects one argument.
252 * When calling this method, pass a callback function that expects one argument.
253 * The callback will be passed the complete `object_info_reply` message documented
253 * The callback will be passed the complete `object_info_reply` message documented
254 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
254 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
255 */
255 */
256 Kernel.prototype.object_info = function (objname, callback) {
256 Kernel.prototype.object_info = function (objname, callback) {
257 var callbacks;
257 var callbacks;
258 if (callback) {
258 if (callback) {
259 callbacks = { shell : { reply : callback } };
259 callbacks = { shell : { reply : callback } };
260 }
260 }
261
261
262 if (typeof(objname) !== null && objname !== null) {
262 if (typeof(objname) !== null && objname !== null) {
263 var content = {
263 var content = {
264 oname : objname.toString(),
264 oname : objname.toString(),
265 detail_level : 0,
265 detail_level : 0,
266 };
266 };
267 return this.send_shell_message("object_info_request", content, callbacks);
267 return this.send_shell_message("object_info_request", content, callbacks);
268 }
268 }
269 return;
269 return;
270 };
270 };
271
271
272 /**
272 /**
273 * Execute given code into kernel, and pass result to callback.
273 * Execute given code into kernel, and pass result to callback.
274 *
274 *
275 * @async
275 * @async
276 * @method execute
276 * @method execute
277 * @param {string} code
277 * @param {string} code
278 * @param [callbacks] {Object} With the following keys (all optional)
278 * @param [callbacks] {Object} With the following keys (all optional)
279 * @param callbacks.shell.reply {function}
279 * @param callbacks.shell.reply {function}
280 * @param callbacks.shell.payload.[payload_name] {function}
280 * @param callbacks.shell.payload.[payload_name] {function}
281 * @param callbacks.iopub.output {function}
281 * @param callbacks.iopub.output {function}
282 * @param callbacks.iopub.clear_output {function}
282 * @param callbacks.iopub.clear_output {function}
283 * @param callbacks.input {function}
283 * @param callbacks.input {function}
284 * @param {object} [options]
284 * @param {object} [options]
285 * @param [options.silent=false] {Boolean}
285 * @param [options.silent=false] {Boolean}
286 * @param [options.user_expressions=empty_dict] {Dict}
286 * @param [options.user_expressions=empty_dict] {Dict}
287 * @param [options.user_variables=empty_list] {List od Strings}
287 * @param [options.user_variables=empty_list] {List od Strings}
288 * @param [options.allow_stdin=false] {Boolean} true|false
288 * @param [options.allow_stdin=false] {Boolean} true|false
289 *
289 *
290 * @example
290 * @example
291 *
291 *
292 * The options object should contain the options for the execute call. Its default
292 * The options object should contain the options for the execute call. Its default
293 * values are:
293 * values are:
294 *
294 *
295 * options = {
295 * options = {
296 * silent : true,
296 * silent : true,
297 * user_variables : [],
297 * user_variables : [],
298 * user_expressions : {},
298 * user_expressions : {},
299 * allow_stdin : false
299 * allow_stdin : false
300 * }
300 * }
301 *
301 *
302 * When calling this method pass a callbacks structure of the form:
302 * When calling this method pass a callbacks structure of the form:
303 *
303 *
304 * callbacks = {
304 * callbacks = {
305 * shell : {
305 * shell : {
306 * reply : execute_reply_callback,
306 * reply : execute_reply_callback,
307 * payload : {
307 * payload : {
308 * set_next_input : set_next_input_callback,
308 * set_next_input : set_next_input_callback,
309 * }
309 * }
310 * },
310 * },
311 * iopub : {
311 * iopub : {
312 * output : output_callback,
312 * output : output_callback,
313 * clear_output : clear_output_callback,
313 * clear_output : clear_output_callback,
314 * },
314 * },
315 * input : raw_input_callback
315 * input : raw_input_callback
316 * }
316 * }
317 *
317 *
318 * Each callback will be passed the entire message as a single arugment.
318 * Each callback will be passed the entire message as a single arugment.
319 * Payload handlers will be passed the corresponding payload and the execute_reply message.
319 * Payload handlers will be passed the corresponding payload and the execute_reply message.
320 */
320 */
321 Kernel.prototype.execute = function (code, callbacks, options) {
321 Kernel.prototype.execute = function (code, callbacks, options) {
322
322
323 var content = {
323 var content = {
324 code : code,
324 code : code,
325 silent : true,
325 silent : true,
326 store_history : false,
326 store_history : false,
327 user_variables : [],
327 user_variables : [],
328 user_expressions : {},
328 user_expressions : {},
329 allow_stdin : false
329 allow_stdin : false
330 };
330 };
331 callbacks = callbacks || {};
331 callbacks = callbacks || {};
332 if (callbacks.input !== undefined) {
332 if (callbacks.input !== undefined) {
333 content.allow_stdin = true;
333 content.allow_stdin = true;
334 }
334 }
335 $.extend(true, content, options);
335 $.extend(true, content, options);
336 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
336 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
337 return this.send_shell_message("execute_request", content, callbacks);
337 return this.send_shell_message("execute_request", content, callbacks);
338 };
338 };
339
339
340 /**
340 /**
341 * When calling this method, pass a function to be called with the `complete_reply` message
341 * When calling this method, pass a function to be called with the `complete_reply` message
342 * as its only argument when it arrives.
342 * as its only argument when it arrives.
343 *
343 *
344 * `complete_reply` is documented
344 * `complete_reply` is documented
345 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
345 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
346 *
346 *
347 * @method complete
347 * @method complete
348 * @param line {integer}
348 * @param line {integer}
349 * @param cursor_pos {integer}
349 * @param cursor_pos {integer}
350 * @param callback {function}
350 * @param callback {function}
351 *
351 *
352 */
352 */
353 Kernel.prototype.complete = function (line, cursor_pos, callback) {
353 Kernel.prototype.complete = function (line, cursor_pos, callback) {
354 var callbacks;
354 var callbacks;
355 if (callback) {
355 if (callback) {
356 callbacks = { shell : { reply : callback } };
356 callbacks = { shell : { reply : callback } };
357 }
357 }
358 var content = {
358 var content = {
359 text : '',
359 text : '',
360 line : line,
360 line : line,
361 block : null,
361 block : null,
362 cursor_pos : cursor_pos
362 cursor_pos : cursor_pos
363 };
363 };
364 return this.send_shell_message("complete_request", content, callbacks);
364 return this.send_shell_message("complete_request", content, callbacks);
365 };
365 };
366
366
367
367
368 Kernel.prototype.interrupt = function () {
368 Kernel.prototype.interrupt = function () {
369 if (this.running) {
369 if (this.running) {
370 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
370 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
371 $.post(this.kernel_url + "/interrupt");
371 $.post(this.kernel_url + "/interrupt");
372 }
372 }
373 };
373 };
374
374
375
375
376 Kernel.prototype.kill = function () {
376 Kernel.prototype.kill = function () {
377 if (this.running) {
377 if (this.running) {
378 this.running = false;
378 this.running = false;
379 var settings = {
379 var settings = {
380 cache : false,
380 cache : false,
381 type : "DELETE"
381 type : "DELETE"
382 };
382 };
383 $.ajax(this.kernel_url, settings);
383 $.ajax(this.kernel_url, settings);
384 }
384 }
385 };
385 };
386
386
387 Kernel.prototype.send_input_reply = function (input) {
387 Kernel.prototype.send_input_reply = function (input) {
388 var content = {
388 var content = {
389 value : input,
389 value : input,
390 };
390 };
391 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
391 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
392 var msg = this._get_msg("input_reply", content);
392 var msg = this._get_msg("input_reply", content);
393 this.stdin_channel.send(JSON.stringify(msg));
393 this.stdin_channel.send(JSON.stringify(msg));
394 return msg.header.msg_id;
394 return msg.header.msg_id;
395 };
395 };
396
396
397
397
398 // Reply handlers
398 // Reply handlers
399
399
400 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
400 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
401 this._iopub_handlers[msg_type] = callback;
401 this._iopub_handlers[msg_type] = callback;
402 };
402 };
403
403
404 Kernel.prototype.get_iopub_handler = function (msg_type) {
404 Kernel.prototype.get_iopub_handler = function (msg_type) {
405 // get iopub handler for a specific message type
405 // get iopub handler for a specific message type
406 return this._iopub_handlers[msg_type];
406 return this._iopub_handlers[msg_type];
407 };
407 };
408
408
409
409
410 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
410 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
411 // get callbacks for a specific message
411 // get callbacks for a specific message
412 return this._msg_callbacks[msg_id];
412 return this._msg_callbacks[msg_id];
413 };
413 };
414
414
415
415
416 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
416 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
417 if (this._msg_callbacks[msg_id] !== undefined ) {
417 if (this._msg_callbacks[msg_id] !== undefined ) {
418 delete this._msg_callbacks[msg_id];
418 delete this._msg_callbacks[msg_id];
419 }
419 }
420 };
420 };
421
421
422 /* Set callbacks for a particular message.
422 /* Set callbacks for a particular message.
423 * Callbacks should be a struct of the following form:
423 * Callbacks should be a struct of the following form:
424 * shell : {
424 * shell : {
425 *
425 *
426 * }
426 * }
427
427
428 */
428 */
429 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
429 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
430 if (callbacks) {
430 if (callbacks) {
431 // shallow-copy mapping, because we will modify it at the top level
431 // shallow-copy mapping, because we will modify it at the top level
432 var cbcopy = this._msg_callbacks[msg_id] = {};
432 var cbcopy = this._msg_callbacks[msg_id] = {};
433 cbcopy.shell = callbacks.shell;
433 cbcopy.shell = callbacks.shell;
434 cbcopy.iopub = callbacks.iopub;
434 cbcopy.iopub = callbacks.iopub;
435 cbcopy.input = callbacks.input;
435 cbcopy.input = callbacks.input;
436 this._msg_callbacks[msg_id] = cbcopy;
436 this._msg_callbacks[msg_id] = cbcopy;
437 }
437 }
438 };
438 };
439
439
440
440
441 Kernel.prototype._handle_shell_reply = function (e) {
441 Kernel.prototype._handle_shell_reply = function (e) {
442 var reply = $.parseJSON(e.data);
442 var reply = $.parseJSON(e.data);
443 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
443 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
444 var content = reply.content;
444 var content = reply.content;
445 var metadata = reply.metadata;
445 var metadata = reply.metadata;
446 var parent_id = reply.parent_header.msg_id;
446 var parent_id = reply.parent_header.msg_id;
447 var callbacks = this.get_callbacks_for_msg(parent_id);
447 var callbacks = this.get_callbacks_for_msg(parent_id);
448 if (!callbacks || !callbacks.shell) {
448 if (!callbacks || !callbacks.shell) {
449 return;
449 return;
450 }
450 }
451 var shell_callbacks = callbacks.shell;
451 var shell_callbacks = callbacks.shell;
452
452
453 // clear callbacks on shell
453 // clear callbacks on shell
454 delete callbacks.shell;
454 delete callbacks.shell;
455 delete callbacks.input;
455 delete callbacks.input;
456 if (!callbacks.iopub) {
456 if (!callbacks.iopub) {
457 this.clear_callbacks_for_msg(parent_id);
457 this.clear_callbacks_for_msg(parent_id);
458 }
458 }
459
459
460 if (shell_callbacks.reply !== undefined) {
460 if (shell_callbacks.reply !== undefined) {
461 shell_callbacks.reply(reply);
461 shell_callbacks.reply(reply);
462 }
462 }
463 if (content.payload && shell_callbacks.payload) {
463 if (content.payload && shell_callbacks.payload) {
464 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
464 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
465 }
465 }
466 };
466 };
467
467
468
468
469 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
469 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
470 var l = payloads.length;
470 var l = payloads.length;
471 // Payloads are handled by triggering events because we don't want the Kernel
471 // Payloads are handled by triggering events because we don't want the Kernel
472 // to depend on the Notebook or Pager classes.
472 // to depend on the Notebook or Pager classes.
473 for (var i=0; i<l; i++) {
473 for (var i=0; i<l; i++) {
474 var payload = payloads[i];
474 var payload = payloads[i];
475 var callback = payload_callbacks[payload.source];
475 var callback = payload_callbacks[payload.source];
476 if (callback) {
476 if (callback) {
477 callback(payload, msg);
477 callback(payload, msg);
478 }
478 }
479 }
479 }
480 };
480 };
481
481
482 Kernel.prototype._handle_status_message = function (msg) {
482 Kernel.prototype._handle_status_message = function (msg) {
483 var execution_state = msg.content.execution_state;
483 var execution_state = msg.content.execution_state;
484 var parent_id = msg.parent_header.msg_id;
484 var parent_id = msg.parent_header.msg_id;
485
485
486 // dispatch status msg callbacks, if any
486 // dispatch status msg callbacks, if any
487 var callbacks = this.get_callbacks_for_msg(parent_id);
487 var callbacks = this.get_callbacks_for_msg(parent_id);
488 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
488 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
489 try {
489 try {
490 callbacks.iopub.status(msg);
490 callbacks.iopub.status(msg);
491 } catch (e) {
491 } catch (e) {
492 console.log("Exception in status msg handler", e);
492 console.log("Exception in status msg handler", e, e.stack);
493 }
493 }
494 }
494 }
495
495
496 if (execution_state === 'busy') {
496 if (execution_state === 'busy') {
497 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
497 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
498 } else if (execution_state === 'idle') {
498 } else if (execution_state === 'idle') {
499 // clear callbacks on idle, there can be no more
499 // clear callbacks on idle, there can be no more
500 if (callbacks !== undefined) {
500 if (callbacks !== undefined) {
501 delete callbacks.iopub;
501 delete callbacks.iopub;
502 delete callbacks.input;
502 delete callbacks.input;
503 if (!callbacks.shell) {
503 if (!callbacks.shell) {
504 this.clear_callbacks_for_msg(parent_id);
504 this.clear_callbacks_for_msg(parent_id);
505 }
505 }
506 }
506 }
507 // trigger status_idle event
507 // trigger status_idle event
508 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
508 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
509 } else if (execution_state === 'restarting') {
509 } else if (execution_state === 'restarting') {
510 // autorestarting is distinct from restarting,
510 // autorestarting is distinct from restarting,
511 // in that it means the kernel died and the server is restarting it.
511 // in that it means the kernel died and the server is restarting it.
512 // status_restarting sets the notification widget,
512 // status_restarting sets the notification widget,
513 // autorestart shows the more prominent dialog.
513 // autorestart shows the more prominent dialog.
514 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
514 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
515 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
515 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
516 } else if (execution_state === 'dead') {
516 } else if (execution_state === 'dead') {
517 this.stop_channels();
517 this.stop_channels();
518 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
518 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
519 }
519 }
520 };
520 };
521
521
522
522
523 // handle clear_output message
523 // handle clear_output message
524 Kernel.prototype._handle_clear_output = function (msg) {
524 Kernel.prototype._handle_clear_output = function (msg) {
525 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
525 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
526 if (!callbacks || !callbacks.iopub) {
526 if (!callbacks || !callbacks.iopub) {
527 return;
527 return;
528 }
528 }
529 var callback = callbacks.iopub.clear_output;
529 var callback = callbacks.iopub.clear_output;
530 if (callback) {
530 if (callback) {
531 callback(msg);
531 callback(msg);
532 }
532 }
533 };
533 };
534
534
535
535
536 // handle an output message (pyout, display_data, etc.)
536 // handle an output message (pyout, display_data, etc.)
537 Kernel.prototype._handle_output_message = function (msg) {
537 Kernel.prototype._handle_output_message = function (msg) {
538 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
538 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
539 if (!callbacks || !callbacks.iopub) {
539 if (!callbacks || !callbacks.iopub) {
540 return;
540 return;
541 }
541 }
542 var callback = callbacks.iopub.output;
542 var callback = callbacks.iopub.output;
543 if (callback) {
543 if (callback) {
544 callback(msg);
544 callback(msg);
545 }
545 }
546 };
546 };
547
547
548 // dispatch IOPub messages to respective handlers.
548 // dispatch IOPub messages to respective handlers.
549 // each message type should have a handler.
549 // each message type should have a handler.
550 Kernel.prototype._handle_iopub_message = function (e) {
550 Kernel.prototype._handle_iopub_message = function (e) {
551 var msg = $.parseJSON(e.data);
551 var msg = $.parseJSON(e.data);
552
552
553 var handler = this.get_iopub_handler(msg.header.msg_type);
553 var handler = this.get_iopub_handler(msg.header.msg_type);
554 if (handler !== undefined) {
554 if (handler !== undefined) {
555 handler(msg);
555 handler(msg);
556 }
556 }
557 };
557 };
558
558
559
559
560 Kernel.prototype._handle_input_request = function (e) {
560 Kernel.prototype._handle_input_request = function (e) {
561 var request = $.parseJSON(e.data);
561 var request = $.parseJSON(e.data);
562 var header = request.header;
562 var header = request.header;
563 var content = request.content;
563 var content = request.content;
564 var metadata = request.metadata;
564 var metadata = request.metadata;
565 var msg_type = header.msg_type;
565 var msg_type = header.msg_type;
566 if (msg_type !== 'input_request') {
566 if (msg_type !== 'input_request') {
567 console.log("Invalid input request!", request);
567 console.log("Invalid input request!", request);
568 return;
568 return;
569 }
569 }
570 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
570 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
571 if (callbacks) {
571 if (callbacks) {
572 if (callbacks.input) {
572 if (callbacks.input) {
573 callbacks.input(request);
573 callbacks.input(request);
574 }
574 }
575 }
575 }
576 };
576 };
577
577
578
578
579 IPython.Kernel = Kernel;
579 IPython.Kernel = Kernel;
580
580
581 return IPython;
581 return IPython;
582
582
583 }(IPython));
583 }(IPython));
584
584
General Comments 0
You need to be logged in to leave comments. Login now