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