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