##// END OF EJS Templates
Add docstrings to kernel.js
Jessica B. Hamrick -
Show More
@@ -10,10 +10,18 define([
10 10 ], function(IPython, $, utils, comm, widgetmanager) {
11 11 "use strict";
12 12
13 // Initialization and connection.
14 13 /**
15 * A Kernel Class to communicate with the Python kernel
16 * @Class Kernel
14 * A Kernel class to communicate with the Python kernel. This
15 * should generally not be constructed directly, but be created
16 * by. the `Session` object. Once created, this object should be
17 * used to communicate with the kernel.
18 *
19 * @class Kernel
20 * @param {string} kernel_service_url - the URL to access the kernel REST api
21 * @param {string} ws_url - the websockets URL
22 * @param {Notebook} notebook - notebook object
23 * @param {string} id - the kernel id
24 * @param {string} name - the kernel type (e.g. python3)
17 25 */
18 26 var Kernel = function (kernel_service_url, ws_url, notebook, id, name) {
19 27 this.events = notebook.events;
@@ -56,7 +64,9 define([
56 64 this.last_msg_callbacks = {};
57 65 };
58 66
59
67 /**
68 * @function _get_msg
69 */
60 70 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
61 71 var msg = {
62 72 header : {
@@ -72,16 +82,22 define([
72 82 };
73 83 return msg;
74 84 };
75
85
86 /**
87 * @function bind_events
88 */
76 89 Kernel.prototype.bind_events = function () {
77 90 var that = this;
78 91 this.events.on('send_input_reply.Kernel', function(evt, data) {
79 92 that.send_input_reply(data);
80 93 });
81 94 };
82
83 // Initialize the iopub handlers
84
95
96 /**
97 * Initialize the iopub handlers.
98 *
99 * @function init_iopub_handlers
100 */
85 101 Kernel.prototype.init_iopub_handlers = function () {
86 102 var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
87 103 this._iopub_handlers = {};
@@ -95,6 +111,12 define([
95 111
96 112 /**
97 113 * GET /api/kernels
114 *
115 * Get the list of running kernels.
116 *
117 * @function list
118 * @param {function} [success] - function executed on ajax success
119 * @param {function} [error] - functon executed on ajax error
98 120 */
99 121 Kernel.prototype.list = function (success, error) {
100 122 $.ajax(this.kernel_service_url, {
@@ -110,10 +132,17 define([
110 132 /**
111 133 * POST /api/kernels
112 134 *
135 * Start a new kernel.
136 *
113 137 * In general this shouldn't be used -- the kernel should be
114 138 * started through the session API. If you use this function and
115 139 * are also using the session API then your session and kernel
116 140 * WILL be out of sync!
141 *
142 * @function start
143 * @param {params} [Object] - parameters to include in the query string
144 * @param {function} [success] - function executed on ajax success
145 * @param {function} [error] - functon executed on ajax error
117 146 */
118 147 Kernel.prototype.start = function (params, success, error) {
119 148 var url = this.kernel_service_url;
@@ -126,7 +155,7 define([
126 155 var on_success = function (data, status, xhr) {
127 156 that.id = data.id;
128 157 that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id);
129 that._kernel_started(data);
158 that._kernel_started();
130 159 if (success) {
131 160 success(data, status, xhr);
132 161 }
@@ -147,6 +176,12 define([
147 176
148 177 /**
149 178 * GET /api/kernels/[:kernel_id]
179 *
180 * Get information about the kernel.
181 *
182 * @function get_info
183 * @param {function} [success] - function executed on ajax success
184 * @param {function} [error] - functon executed on ajax error
150 185 */
151 186 Kernel.prototype.get_info = function (success, error) {
152 187 $.ajax(this.kernel_url, {
@@ -161,6 +196,16 define([
161 196
162 197 /**
163 198 * DELETE /api/kernels/[:kernel_id]
199 *
200 * Shutdown the kernel.
201 *
202 * If you are also using sessions, then this function shoul NOT be
203 * used. Instead, use Session.delete. Otherwise, the session and
204 * kernel WILL be out of sync.
205 *
206 * @function kill
207 * @param {function} [success] - function executed on ajax success
208 * @param {function} [error] - functon executed on ajax error
164 209 */
165 210 Kernel.prototype.kill = function (success, error) {
166 211 this._kernel_dead();
@@ -176,6 +221,12 define([
176 221
177 222 /**
178 223 * POST /api/kernels/[:kernel_id]/interrupt
224 *
225 * Interrupt the kernel.
226 *
227 * @function interrupt
228 * @param {function} [success] - function executed on ajax success
229 * @param {function} [error] - functon executed on ajax error
179 230 */
180 231 Kernel.prototype.interrupt = function (success, error) {
181 232 this.events.trigger('status_interrupting.Kernel', {kernel: this});
@@ -192,6 +243,12 define([
192 243
193 244 /**
194 245 * POST /api/kernels/[:kernel_id]/restart
246 *
247 * Restart the kernel.
248 *
249 * @function interrupt
250 * @param {function} [success] - function executed on ajax success
251 * @param {function} [error] - functon executed on ajax error
195 252 */
196 253 Kernel.prototype.restart = function (success, error) {
197 254 this.events.trigger('status_restarting.Kernel', {kernel: this});
@@ -199,7 +256,7 define([
199 256
200 257 var that = this;
201 258 var on_success = function (data, status, xhr) {
202 that._kernel_started(data, status, xhr);
259 that._kernel_started();
203 260 if (success) {
204 261 success(data, status, xhr);
205 262 }
@@ -217,8 +274,11 define([
217 274 };
218 275
219 276 /**
220 * Not actually a HTTP request, but useful function nonetheless
221 * for reconnecting to the kernel if the connection is somehow lost
277 * Reconnect to a disconnected kernel. This is not actually a
278 * standard HTTP request, but useful function nonetheless for
279 * reconnecting to the kernel if the connection is somehow lost.
280 *
281 * @function reconnect
222 282 */
223 283 Kernel.prototype.reconnect = function () {
224 284 this.events.trigger('status_reconnecting.Kernel');
@@ -228,6 +288,14 define([
228 288 }, 5000);
229 289 };
230 290
291 /**
292 * Handle a successful AJAX request by updating the kernel id and
293 * name from the response, and then optionally calling a provided
294 * callback.
295 *
296 * @function _on_success
297 * @param {function} success - callback
298 */
231 299 Kernel.prototype._on_success = function (success) {
232 300 var that = this;
233 301 return function (data, status, xhr) {
@@ -242,6 +310,13 define([
242 310 };
243 311 };
244 312
313 /**
314 * Handle a failed AJAX request by logging the error message, and
315 * then optionally calling a provided callback.
316 *
317 * @function _on_error
318 * @param {function} error - callback
319 */
245 320 Kernel.prototype._on_error = function (error) {
246 321 return function (xhr, status, err) {
247 322 utils.log_ajax_error(xhr, status, err);
@@ -251,12 +326,27 define([
251 326 };
252 327 };
253 328
254 Kernel.prototype._kernel_started = function (json) {
255 console.log("Kernel started: ", json.id);
329 /**
330 * Perform necessary tasks once the kernel has been started. This
331 * includes triggering the 'status_started.Kernel' event and
332 * then actually connecting to the kernel.
333 *
334 * @function _kernel_started
335 */
336 Kernel.prototype._kernel_started = function () {
337 console.log("Kernel started: ", this.id);
256 338 this.events.trigger('status_started.Kernel', {kernel: this});
257 339 this.start_channels();
258 340 };
259 341
342 /**
343 * Perform necessary tasks once the connection to the kernel has
344 * been established. This includes triggering the
345 * 'status_connected.Kernel' event and then requesting information
346 * about the kernel.
347 *
348 * @function _kernel_connected
349 */
260 350 Kernel.prototype._kernel_connected = function () {
261 351 var that = this;
262 352 console.log('Connected to kernel: ', this.id);
@@ -266,18 +356,25 define([
266 356 });
267 357 };
268 358
359 /**
360 * Perform necessary tasks after the kernel has died. This
361 * includes triggering both 'status_dead.Kernel' and
362 * 'no_kernel.Kernel', and then closing communication channels to
363 * the kernel if they are still somehow open.
364 *
365 * @function _kernel_dead
366 */
269 367 Kernel.prototype._kernel_dead = function () {
270 368 this.events.trigger('status_dead.Kernel');
271 369 this.events.trigger('no_kernel.Kernel');
272 370 this.stop_channels();
273 371 };
274 372
275
276 373 /**
277 374 * Start the `shell`and `iopub` channels.
278 375 * Will stop and restart them if they already exist.
279 376 *
280 * @method start_channels
377 * @function start_channels
281 378 */
282 379 Kernel.prototype.start_channels = function () {
283 380 var that = this;
@@ -340,10 +437,10 define([
340 437 };
341 438
342 439 /**
343 * Handle a websocket entering the open state
344 * sends session and cookie authentication info as first message.
345 * Once all sockets are open, signal the Kernel.status_started event.
346 * @method _ws_opened
440 * Handle a websocket entering the open state sends session and
441 * cookie authentication info as first message.
442 *
443 * @function _ws_opened
347 444 */
348 445 Kernel.prototype._ws_opened = function (evt) {
349 446 // send the session id so the Session object Python-side
@@ -355,7 +452,18 define([
355 452 this._kernel_connected();
356 453 }
357 454 };
358
455
456 /**
457 * Handle a websocket entering the closed state. This closes the
458 * other communication channels if they are open, and triggers the
459 * 'status_disconnected.Kernel' event. If the websocket was closed
460 * early, then also trigger 'early_disconnect.Kernel'. Otherwise,
461 * try to reconnect to the kernel.
462 *
463 * @function _ws_closed
464 * @param {string} ws_url - the websocket url
465 * @param {bool} early - whether the connection was closed early or not
466 */
359 467 Kernel.prototype._ws_closed = function(ws_url, early) {
360 468 this.stop_channels();
361 469 this.events.trigger('status_disconnected.Kernel');
@@ -368,8 +476,10 define([
368 476 };
369 477
370 478 /**
371 * Stop the websocket channels.
372 * @method stop_channels
479 * Close the websocket channels. After successful close, the value
480 * in `this.channels[channel_name]` will be null.
481 *
482 * @function stop_channels
373 483 */
374 484 Kernel.prototype.stop_channels = function () {
375 485 var that = this;
@@ -388,8 +498,14 define([
388 498 }
389 499 };
390 500
391 // Main public methods.
392
501 /**
502 * Check whether there is a connection to the kernel. This
503 * function only returns true if all channel objects have been
504 * created and have a state of WebSocket.OPEN.
505 *
506 * @function is_connected
507 * @returns {bool} - whether there is a connection
508 */
393 509 Kernel.prototype.is_connected = function () {
394 510 for (var c in this.channels) {
395 511 // if any channel is not ready, then we're not connected
@@ -403,6 +519,14 define([
403 519 return true;
404 520 };
405 521
522 /**
523 * Check whether the connection to the kernel has been completely
524 * severed. This function only returns true if all channel objects
525 * are null.
526 *
527 * @function is_fully_disconnected
528 * @returns {bool} - whether the kernel is fully disconnected
529 */
406 530 Kernel.prototype.is_fully_disconnected = function () {
407 531 for (var c in this.channels) {
408 532 if (this.channels[c] === null) {
@@ -412,7 +536,11 define([
412 536 return false;
413 537 };
414 538
415 // send a message on the Kernel's shell channel
539 /**
540 * Send a message on the Kernel's shell channel
541 *
542 * @function send_shell_message
543 */
416 544 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
417 545 if (!this.is_connected()) {
418 546 throw new Error("kernel is not connected");
@@ -426,8 +554,8 define([
426 554 /**
427 555 * Get kernel info
428 556 *
557 * @function kernel_info
429 558 * @param callback {function}
430 * @method kernel_info
431 559 *
432 560 * When calling this method, pass a callback function that expects one argument.
433 561 * The callback will be passed the complete `kernel_info_reply` message documented
@@ -444,14 +572,14 define([
444 572 /**
445 573 * Get info on an object
446 574 *
447 * @param code {string}
448 * @param cursor_pos {integer}
449 * @param callback {function}
450 * @method inspect
451 *
452 575 * When calling this method, pass a callback function that expects one argument.
453 576 * The callback will be passed the complete `inspect_reply` message documented
454 577 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
578 *
579 * @function inspect
580 * @param code {string}
581 * @param cursor_pos {integer}
582 * @param callback {function}
455 583 */
456 584 Kernel.prototype.inspect = function (code, cursor_pos, callback) {
457 585 var callbacks;
@@ -471,7 +599,7 define([
471 599 * Execute given code into kernel, and pass result to callback.
472 600 *
473 601 * @async
474 * @method execute
602 * @function execute
475 603 * @param {string} code
476 604 * @param [callbacks] {Object} With the following keys (all optional)
477 605 * @param callbacks.shell.reply {function}
@@ -486,8 +614,8 define([
486 614 *
487 615 * @example
488 616 *
489 * The options object should contain the options for the execute call. Its default
490 * values are:
617 * The options object should contain the options for the execute
618 * call. Its default values are:
491 619 *
492 620 * options = {
493 621 * silent : true,
@@ -495,7 +623,8 define([
495 623 * allow_stdin : false
496 624 * }
497 625 *
498 * When calling this method pass a callbacks structure of the form:
626 * When calling this method pass a callbacks structure of the
627 * form:
499 628 *
500 629 * callbacks = {
501 630 * shell : {
@@ -511,8 +640,9 define([
511 640 * input : raw_input_callback
512 641 * }
513 642 *
514 * Each callback will be passed the entire message as a single arugment.
515 * Payload handlers will be passed the corresponding payload and the execute_reply message.
643 * Each callback will be passed the entire message as a single
644 * arugment. Payload handlers will be passed the corresponding
645 * payload and the execute_reply message.
516 646 */
517 647 Kernel.prototype.execute = function (code, callbacks, options) {
518 648 var content = {
@@ -532,17 +662,16 define([
532 662 };
533 663
534 664 /**
535 * When calling this method, pass a function to be called with the `complete_reply` message
536 * as its only argument when it arrives.
665 * When calling this method, pass a function to be called with the
666 * `complete_reply` message as its only argument when it arrives.
537 667 *
538 668 * `complete_reply` is documented
539 669 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
540 670 *
541 * @method complete
671 * @function complete
542 672 * @param code {string}
543 673 * @param cursor_pos {integer}
544 674 * @param callback {function}
545 *
546 675 */
547 676 Kernel.prototype.complete = function (code, cursor_pos, callback) {
548 677 var callbacks;
@@ -556,6 +685,9 define([
556 685 return this.send_shell_message("complete_request", content, callbacks);
557 686 };
558 687
688 /**
689 * @function send_input_reply
690 */
559 691 Kernel.prototype.send_input_reply = function (input) {
560 692 if (!this.is_connected()) {
561 693 throw new Error("kernel is not connected");
@@ -569,21 +701,28 define([
569 701 return msg.header.msg_id;
570 702 };
571 703
572
573 // Reply handlers
574
704 /**
705 * @function register_iopub_handler
706 */
575 707 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
576 708 this._iopub_handlers[msg_type] = callback;
577 709 };
578 710
711 /**
712 * Get the iopub handler for a specific message type.
713 *
714 * @function get_iopub_handler
715 */
579 716 Kernel.prototype.get_iopub_handler = function (msg_type) {
580 // get iopub handler for a specific message type
581 717 return this._iopub_handlers[msg_type];
582 718 };
583 719
584
720 /**
721 * Get callbacks for a specific message.
722 *
723 * @function get_callbacks_for_msg
724 */
585 725 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
586 // get callbacks for a specific message
587 726 if (msg_id == this.last_msg_id) {
588 727 return this.last_msg_callbacks;
589 728 } else {
@@ -591,13 +730,20 define([
591 730 }
592 731 };
593 732
594
733 /**
734 * Clear callbacks for a specific message.
735 *
736 * @function clear_callbacks_for_msg
737 */
595 738 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
596 739 if (this._msg_callbacks[msg_id] !== undefined ) {
597 740 delete this._msg_callbacks[msg_id];
598 741 }
599 742 };
600 743
744 /**
745 * @function _finish_shell
746 */
601 747 Kernel.prototype._finish_shell = function (msg_id) {
602 748 var callbacks = this._msg_callbacks[msg_id];
603 749 if (callbacks !== undefined) {
@@ -608,6 +754,9 define([
608 754 }
609 755 };
610 756
757 /**
758 * @function _finish_iopub
759 */
611 760 Kernel.prototype._finish_iopub = function (msg_id) {
612 761 var callbacks = this._msg_callbacks[msg_id];
613 762 if (callbacks !== undefined) {
@@ -618,12 +767,14 define([
618 767 }
619 768 };
620 769
621 /* Set callbacks for a particular message.
770 /**
771 * Set callbacks for a particular message.
622 772 * Callbacks should be a struct of the following form:
623 773 * shell : {
624 774 *
625 775 * }
626
776 *
777 * @function set_callbacks_for_msg
627 778 */
628 779 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
629 780 this.last_msg_id = msg_id;
@@ -640,7 +791,9 define([
640 791 }
641 792 };
642 793
643
794 /**
795 * @function _handle_shell_reply
796 */
644 797 Kernel.prototype._handle_shell_reply = function (e) {
645 798 var reply = $.parseJSON(e.data);
646 799 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
@@ -664,7 +817,9 define([
664 817 }
665 818 };
666 819
667
820 /**
821 * @function _handle_payloads
822 */
668 823 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
669 824 var l = payloads.length;
670 825 // Payloads are handled by triggering events because we don't want the Kernel
@@ -678,6 +833,9 define([
678 833 }
679 834 };
680 835
836 /**
837 * @function _handle_status_message
838 */
681 839 Kernel.prototype._handle_status_message = function (msg) {
682 840 var execution_state = msg.content.execution_state;
683 841 var parent_id = msg.parent_header.msg_id;
@@ -717,8 +875,11 define([
717 875 }
718 876 };
719 877
720
721 // handle clear_output message
878 /**
879 * Handle clear_output message
880 *
881 * @function _handle_clear_output
882 */
722 883 Kernel.prototype._handle_clear_output = function (msg) {
723 884 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
724 885 if (!callbacks || !callbacks.iopub) {
@@ -730,8 +891,11 define([
730 891 }
731 892 };
732 893
733
734 // handle an output message (execute_result, display_data, etc.)
894 /**
895 * handle an output message (execute_result, display_data, etc.)
896 *
897 * @function _handle_output_message
898 */
735 899 Kernel.prototype._handle_output_message = function (msg) {
736 900 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
737 901 if (!callbacks || !callbacks.iopub) {
@@ -743,8 +907,12 define([
743 907 }
744 908 };
745 909
746 // dispatch IOPub messages to respective handlers.
747 // each message type should have a handler.
910 /**
911 * Dispatch IOPub messages to respective handlers. Each message
912 * type should have a handler.
913 *
914 * @function _handle_iopub_message
915 */
748 916 Kernel.prototype._handle_iopub_message = function (e) {
749 917 var msg = $.parseJSON(e.data);
750 918
@@ -754,7 +922,9 define([
754 922 }
755 923 };
756 924
757
925 /**
926 * @function _handle_input_request
927 */
758 928 Kernel.prototype._handle_input_request = function (e) {
759 929 var request = $.parseJSON(e.data);
760 930 var header = request.header;
@@ -88,7 +88,7 define([
88 88 that.kernel = new kernel.Kernel(
89 89 kernel_service_url, that.ws_url, that.notebook,
90 90 that.kernel_model.id, that.kernel_model.name);
91 that.kernel._kernel_started(data.kernel);
91 that.kernel._kernel_started();
92 92 if (success) {
93 93 success(data, status, xhr);
94 94 }
General Comments 0
You need to be logged in to leave comments. Login now