Repository navigation
Expand file tree
/
Copy pathkernel.js
More file actions
1052 lines (966 loc) · 34.9 KB
/
Copy pathkernel.js
File metadata and controls
1052 lines (966 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'jquery',
'base/js/utils',
'./comm',
'./serialize',
'widgets/js/init'
], function(IPython, $, utils, comm, serialize, widgetmanager) {
"use strict";
/**
* A Kernel class to communicate with the Python kernel. This
* should generally not be constructed directly, but be created
* by. the `Session` object. Once created, this object should be
* used to communicate with the kernel.
*
* @class Kernel
* @param {string} kernel_service_url - the URL to access the kernel REST api
* @param {string} ws_url - the websockets URL
* @param {Notebook} notebook - notebook object
* @param {string} name - the kernel type (e.g. python3)
*/
var Kernel = function (kernel_service_url, ws_url, notebook, name) {
this.events = notebook.events;
this.id = null;
this.name = name;
this.channels = {
'shell': null,
'iopub': null,
'stdin': null
};
this.kernel_service_url = kernel_service_url;
this.kernel_url = null;
this.ws_url = ws_url || IPython.utils.get_body_data("wsUrl");
if (!this.ws_url) {
// trailing 's' in https will become wss for secure web sockets
this.ws_url = location.protocol.replace('http', 'ws') + "//" + location.host;
}
this.username = "username";
this.session_id = utils.uuid();
this._msg_callbacks = {};
this.info_reply = {}; // kernel_info_reply stored here after starting
if (typeof(WebSocket) !== 'undefined') {
this.WebSocket = WebSocket;
} else if (typeof(MozWebSocket) !== 'undefined') {
this.WebSocket = MozWebSocket;
} else {
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.');
}
this.bind_events();
this.init_iopub_handlers();
this.comm_manager = new comm.CommManager(this);
this.widget_manager = new widgetmanager.WidgetManager(this.comm_manager, notebook);
this.last_msg_id = null;
this.last_msg_callbacks = {};
this._autorestart_attempt = 0;
this._reconnect_attempt = 0;
this.reconnect_limit = 7;
};
/**
* @function _get_msg
*/
Kernel.prototype._get_msg = function (msg_type, content, metadata, buffers) {
var msg = {
header : {
msg_id : utils.uuid(),
username : this.username,
session : this.session_id,
msg_type : msg_type,
version : "5.0"
},
metadata : metadata || {},
content : content,
buffers : buffers || [],
parent_header : {}
};
return msg;
};
/**
* @function bind_events
*/
Kernel.prototype.bind_events = function () {
var that = this;
this.events.on('send_input_reply.Kernel', function(evt, data) {
that.send_input_reply(data);
});
var record_status = function (evt, info) {
console.log('Kernel: ' + evt.type + ' (' + info.kernel.id + ')');
};
this.events.on('kernel_created.Kernel', record_status);
this.events.on('kernel_reconnecting.Kernel', record_status);
this.events.on('kernel_connected.Kernel', record_status);
this.events.on('kernel_starting.Kernel', record_status);
this.events.on('kernel_restarting.Kernel', record_status);
this.events.on('kernel_autorestarting.Kernel', record_status);
this.events.on('kernel_interrupting.Kernel', record_status);
this.events.on('kernel_disconnected.Kernel', record_status);
// these are commented out because they are triggered a lot, but can
// be uncommented for debugging purposes
//this.events.on('kernel_idle.Kernel', record_status);
//this.events.on('kernel_busy.Kernel', record_status);
this.events.on('kernel_ready.Kernel', record_status);
this.events.on('kernel_killed.Kernel', record_status);
this.events.on('kernel_dead.Kernel', record_status);
this.events.on('kernel_ready.Kernel', function () {
that._autorestart_attempt = 0;
});
this.events.on('kernel_connected.Kernel', function () {
that._reconnect_attempt = 0;
});
};
/**
* Initialize the iopub handlers.
*
* @function init_iopub_handlers
*/
Kernel.prototype.init_iopub_handlers = function () {
var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
this._iopub_handlers = {};
this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
for (var i=0; i < output_msg_types.length; i++) {
this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this));
}
};
/**
* GET /api/kernels
*
* Get the list of running kernels.
*
* @function list
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.list = function (success, error) {
$.ajax(this.kernel_service_url, {
processData: false,
cache: false,
type: "GET",
dataType: "json",
success: success,
error: this._on_error(error)
});
};
/**
* POST /api/kernels
*
* Start a new kernel.
*
* In general this shouldn't be used -- the kernel should be
* started through the session API. If you use this function and
* are also using the session API then your session and kernel
* WILL be out of sync!
*
* @function start
* @param {params} [Object] - parameters to include in the query string
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.start = function (params, success, error) {
var url = this.kernel_service_url;
var qs = $.param(params || {}); // query string for sage math stuff
if (qs !== "") {
url = url + "?" + qs;
}
var that = this;
var on_success = function (data, status, xhr) {
that.events.trigger('kernel_created.Kernel', {kernel: that});
that._kernel_created(data);
if (success) {
success(data, status, xhr);
}
};
$.ajax(url, {
processData: false,
cache: false,
type: "POST",
data: JSON.stringify({name: this.name}),
dataType: "json",
success: this._on_success(on_success),
error: this._on_error(error)
});
return url;
};
/**
* GET /api/kernels/[:kernel_id]
*
* Get information about the kernel.
*
* @function get_info
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.get_info = function (success, error) {
$.ajax(this.kernel_url, {
processData: false,
cache: false,
type: "GET",
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
};
/**
* DELETE /api/kernels/[:kernel_id]
*
* Shutdown the kernel.
*
* If you are also using sessions, then this function shoul NOT be
* used. Instead, use Session.delete. Otherwise, the session and
* kernel WILL be out of sync.
*
* @function kill
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.kill = function (success, error) {
this.events.trigger('kernel_killed.Kernel', {kernel: this});
this._kernel_dead();
$.ajax(this.kernel_url, {
processData: false,
cache: false,
type: "DELETE",
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
};
/**
* POST /api/kernels/[:kernel_id]/interrupt
*
* Interrupt the kernel.
*
* @function interrupt
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.interrupt = function (success, error) {
this.events.trigger('kernel_interrupting.Kernel', {kernel: this});
var that = this;
var on_success = function (data, status, xhr) {
// get kernel info so we know what state the kernel is in
that.kernel_info();
if (success) {
success(data, status, xhr);
}
};
var url = utils.url_join_encode(this.kernel_url, 'interrupt');
$.ajax(url, {
processData: false,
cache: false,
type: "POST",
dataType: "json",
success: this._on_success(on_success),
error: this._on_error(error)
});
};
/**
* POST /api/kernels/[:kernel_id]/restart
*
* Restart the kernel.
*
* @function interrupt
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Kernel.prototype.restart = function (success, error) {
this.events.trigger('kernel_restarting.Kernel', {kernel: this});
this.stop_channels();
var that = this;
var on_success = function (data, status, xhr) {
that.events.trigger('kernel_created.Kernel', {kernel: that});
that._kernel_created(data);
if (success) {
success(data, status, xhr);
}
};
var on_error = function (xhr, status, err) {
that.events.trigger('kernel_dead.Kernel', {kernel: that});
that._kernel_dead();
if (error) {
error(xhr, status, err);
}
};
var url = utils.url_join_encode(this.kernel_url, 'restart');
$.ajax(url, {
processData: false,
cache: false,
type: "POST",
dataType: "json",
success: this._on_success(on_success),
error: this._on_error(on_error)
});
};
/**
* Reconnect to a disconnected kernel. This is not actually a
* standard HTTP request, but useful function nonetheless for
* reconnecting to the kernel if the connection is somehow lost.
*
* @function reconnect
*/
Kernel.prototype.reconnect = function () {
if (this.is_connected()) {
return;
}
this._reconnect_attempt = this._reconnect_attempt + 1;
this.events.trigger('kernel_reconnecting.Kernel', {
kernel: this,
attempt: this._reconnect_attempt,
});
this.start_channels();
};
/**
* Handle a successful AJAX request by updating the kernel id and
* name from the response, and then optionally calling a provided
* callback.
*
* @function _on_success
* @param {function} success - callback
*/
Kernel.prototype._on_success = function (success) {
var that = this;
return function (data, status, xhr) {
if (data) {
that.id = data.id;
that.name = data.name;
}
that.kernel_url = utils.url_join_encode(that.kernel_service_url, that.id);
if (success) {
success(data, status, xhr);
}
};
};
/**
* Handle a failed AJAX request by logging the error message, and
* then optionally calling a provided callback.
*
* @function _on_error
* @param {function} error - callback
*/
Kernel.prototype._on_error = function (error) {
return function (xhr, status, err) {
utils.log_ajax_error(xhr, status, err);
if (error) {
error(xhr, status, err);
}
};
};
/**
* Perform necessary tasks once the kernel has been started,
* including actually connecting to the kernel.
*
* @function _kernel_created
* @param {Object} data - information about the kernel including id
*/
Kernel.prototype._kernel_created = function (data) {
this.id = data.id;
this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id);
this.start_channels();
};
/**
* Perform necessary tasks once the connection to the kernel has
* been established. This includes requesting information about
* the kernel.
*
* @function _kernel_connected
*/
Kernel.prototype._kernel_connected = function () {
this.events.trigger('kernel_connected.Kernel', {kernel: this});
this.events.trigger('kernel_starting.Kernel', {kernel: this});
// get kernel info so we know what state the kernel is in
var that = this;
this.kernel_info(function (reply) {
that.info_reply = reply.content;
that.events.trigger('kernel_ready.Kernel', {kernel: that});
});
};
/**
* Perform necessary tasks after the kernel has died. This closing
* communication channels to the kernel if they are still somehow
* open.
*
* @function _kernel_dead
*/
Kernel.prototype._kernel_dead = function () {
this.stop_channels();
};
/**
* Start the `shell`and `iopub` channels.
* Will stop and restart them if they already exist.
*
* @function start_channels
*/
Kernel.prototype.start_channels = function () {
var that = this;
this.stop_channels();
var ws_host_url = this.ws_url + this.kernel_url;
console.log("Starting WebSockets:", ws_host_url);
var channel_url = function(channel) {
return [
that.ws_url,
utils.url_join_encode(that.kernel_url, channel),
"?session_id=" + that.session_id
].join('');
};
this.channels.shell = new this.WebSocket(channel_url("shell"));
this.channels.stdin = new this.WebSocket(channel_url("stdin"));
this.channels.iopub = new this.WebSocket(channel_url("iopub"));
var already_called_onclose = false; // only alert once
var ws_closed_early = function(evt){
if (already_called_onclose){
return;
}
already_called_onclose = true;
if ( ! evt.wasClean ){
// If the websocket was closed early, that could mean
// that the kernel is actually dead. Try getting
// information about the kernel from the API call --
// if that fails, then assume the kernel is dead,
// otherwise just follow the typical websocket closed
// protocol.
that.get_info(function () {
that._ws_closed(ws_host_url, false);
}, function () {
that.events.trigger('kernel_dead.Kernel', {kernel: that});
that._kernel_dead();
});
}
};
var ws_closed_late = function(evt){
if (already_called_onclose){
return;
}
already_called_onclose = true;
if ( ! evt.wasClean ){
that._ws_closed(ws_host_url, false);
}
};
var ws_error = function(evt){
if (already_called_onclose){
return;
}
already_called_onclose = true;
that._ws_closed(ws_host_url, true);
};
for (var c in this.channels) {
this.channels[c].onopen = $.proxy(this._ws_opened, this);
this.channels[c].onclose = ws_closed_early;
this.channels[c].onerror = ws_error;
}
// switch from early-close to late-close message after 1s
setTimeout(function() {
for (var c in that.channels) {
if (that.channels[c] !== null) {
that.channels[c].onclose = ws_closed_late;
}
}
}, 1000);
this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this);
this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this);
this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this);
};
/**
* Handle a websocket entering the open state,
* signaling that the kernel is connected when all channels are open.
*
* @function _ws_opened
*/
Kernel.prototype._ws_opened = function (evt) {
if (this.is_connected()) {
// all events ready, trigger started event.
this._kernel_connected();
}
};
/**
* Handle a websocket entering the closed state. This closes the
* other communication channels if they are open. If the websocket
* was not closed due to an error, try to reconnect to the kernel.
*
* @function _ws_closed
* @param {string} ws_url - the websocket url
* @param {bool} error - whether the connection was closed due to an error
*/
Kernel.prototype._ws_closed = function(ws_url, error) {
this.stop_channels();
this.events.trigger('kernel_disconnected.Kernel', {kernel: this});
if (error) {
console.log('WebSocket connection failed: ', ws_url);
this.events.trigger('kernel_connection_failed.Kernel', {kernel: this, ws_url: ws_url, attempt: this._reconnect_attempt});
}
this._schedule_reconnect();
};
Kernel.prototype._schedule_reconnect = function () {
// function to call when kernel connection is lost
// schedules reconnect, or fires 'connection_dead' if reconnect limit is hit
if (this._reconnect_attempt < this.reconnect_limit) {
var timeout = Math.pow(2, this._reconnect_attempt);
console.log("Connection lost, reconnecting in " + timeout + " seconds.");
setTimeout($.proxy(this.reconnect, this), 1e3 * timeout);
} else {
this.events.trigger('kernel_connection_dead.Kernel', {
kernel: this,
reconnect_attempt: this._reconnect_attempt,
});
console.log("Failed to reconnect, giving up.");
}
};
/**
* Close the websocket channels. After successful close, the value
* in `this.channels[channel_name]` will be null.
*
* @function stop_channels
*/
Kernel.prototype.stop_channels = function () {
var that = this;
var close = function (c) {
return function () {
if (that.channels[c] && that.channels[c].readyState === WebSocket.CLOSED) {
that.channels[c] = null;
}
};
};
for (var c in this.channels) {
if ( this.channels[c] !== null ) {
if (this.channels[c].readyState === WebSocket.OPEN) {
this.channels[c].onclose = close(c);
this.channels[c].close();
} else {
close(c)();
}
}
}
};
/**
* Check whether there is a connection to the kernel. This
* function only returns true if all channel objects have been
* created and have a state of WebSocket.OPEN.
*
* @function is_connected
* @returns {bool} - whether there is a connection
*/
Kernel.prototype.is_connected = function () {
for (var c in this.channels) {
// if any channel is not ready, then we're not connected
if (this.channels[c] === null) {
return false;
}
if (this.channels[c].readyState !== WebSocket.OPEN) {
return false;
}
}
return true;
};
/**
* Check whether the connection to the kernel has been completely
* severed. This function only returns true if all channel objects
* are null.
*
* @function is_fully_disconnected
* @returns {bool} - whether the kernel is fully disconnected
*/
Kernel.prototype.is_fully_disconnected = function () {
for (var c in this.channels) {
if (this.channels[c] === null) {
return true;
}
}
return false;
};
/**
* Send a message on the Kernel's shell channel
*
* @function send_shell_message
*/
Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata, buffers) {
if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
var msg = this._get_msg(msg_type, content, metadata, buffers);
this.channels.shell.send(serialize.serialize(msg));
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
return msg.header.msg_id;
};
/**
* Get kernel info
*
* @function kernel_info
* @param callback {function}
*
* When calling this method, pass a callback function that expects one argument.
* The callback will be passed the complete `kernel_info_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
*/
Kernel.prototype.kernel_info = function (callback) {
var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
return this.send_shell_message("kernel_info_request", {}, callbacks);
};
/**
* Get info on an object
*
* When calling this method, pass a callback function that expects one argument.
* The callback will be passed the complete `inspect_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
*
* @function inspect
* @param code {string}
* @param cursor_pos {integer}
* @param callback {function}
*/
Kernel.prototype.inspect = function (code, cursor_pos, callback) {
var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
var content = {
code : code,
cursor_pos : cursor_pos,
detail_level : 0
};
return this.send_shell_message("inspect_request", content, callbacks);
};
/**
* Execute given code into kernel, and pass result to callback.
*
* @async
* @function execute
* @param {string} code
* @param [callbacks] {Object} With the following keys (all optional)
* @param callbacks.shell.reply {function}
* @param callbacks.shell.payload.[payload_name] {function}
* @param callbacks.iopub.output {function}
* @param callbacks.iopub.clear_output {function}
* @param callbacks.input {function}
* @param {object} [options]
* @param [options.silent=false] {Boolean}
* @param [options.user_expressions=empty_dict] {Dict}
* @param [options.allow_stdin=false] {Boolean} true|false
*
* @example
*
* The options object should contain the options for the execute
* call. Its default values are:
*
* options = {
* silent : true,
* user_expressions : {},
* allow_stdin : false
* }
*
* When calling this method pass a callbacks structure of the
* form:
*
* callbacks = {
* shell : {
* reply : execute_reply_callback,
* payload : {
* set_next_input : set_next_input_callback,
* }
* },
* iopub : {
* output : output_callback,
* clear_output : clear_output_callback,
* },
* input : raw_input_callback
* }
*
* Each callback will be passed the entire message as a single
* arugment. Payload handlers will be passed the corresponding
* payload and the execute_reply message.
*/
Kernel.prototype.execute = function (code, callbacks, options) {
var content = {
code : code,
silent : true,
store_history : false,
user_expressions : {},
allow_stdin : false
};
callbacks = callbacks || {};
if (callbacks.input !== undefined) {
content.allow_stdin = true;
}
$.extend(true, content, options);
this.events.trigger('execution_request.Kernel', {kernel: this, content: content});
return this.send_shell_message("execute_request", content, callbacks);
};
/**
* When calling this method, pass a function to be called with the
* `complete_reply` message as its only argument when it arrives.
*
* `complete_reply` is documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
*
* @function complete
* @param code {string}
* @param cursor_pos {integer}
* @param callback {function}
*/
Kernel.prototype.complete = function (code, cursor_pos, callback) {
var callbacks;
if (callback) {
callbacks = { shell : { reply : callback } };
}
var content = {
code : code,
cursor_pos : cursor_pos
};
return this.send_shell_message("complete_request", content, callbacks);
};
/**
* @function send_input_reply
*/
Kernel.prototype.send_input_reply = function (input) {
if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
var content = {
value : input
};
this.events.trigger('input_reply.Kernel', {kernel: this, content: content});
var msg = this._get_msg("input_reply", content);
this.channels.stdin.send(serialize.serialize(msg));
return msg.header.msg_id;
};
/**
* @function register_iopub_handler
*/
Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
this._iopub_handlers[msg_type] = callback;
};
/**
* Get the iopub handler for a specific message type.
*
* @function get_iopub_handler
*/
Kernel.prototype.get_iopub_handler = function (msg_type) {
return this._iopub_handlers[msg_type];
};
/**
* Get callbacks for a specific message.
*
* @function get_callbacks_for_msg
*/
Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
if (msg_id == this.last_msg_id) {
return this.last_msg_callbacks;
} else {
return this._msg_callbacks[msg_id];
}
};
/**
* Clear callbacks for a specific message.
*
* @function clear_callbacks_for_msg
*/
Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
if (this._msg_callbacks[msg_id] !== undefined ) {
delete this._msg_callbacks[msg_id];
}
};
/**
* @function _finish_shell
*/
Kernel.prototype._finish_shell = function (msg_id) {
var callbacks = this._msg_callbacks[msg_id];
if (callbacks !== undefined) {
callbacks.shell_done = true;
if (callbacks.iopub_done) {
this.clear_callbacks_for_msg(msg_id);
}
}
};
/**
* @function _finish_iopub
*/
Kernel.prototype._finish_iopub = function (msg_id) {
var callbacks = this._msg_callbacks[msg_id];
if (callbacks !== undefined) {
callbacks.iopub_done = true;
if (callbacks.shell_done) {
this.clear_callbacks_for_msg(msg_id);
}
}
};
/**
* Set callbacks for a particular message.
* Callbacks should be a struct of the following form:
* shell : {
*
* }
*
* @function set_callbacks_for_msg
*/
Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
this.last_msg_id = msg_id;
if (callbacks) {
// shallow-copy mapping, because we will modify it at the top level
var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
cbcopy.shell = callbacks.shell;
cbcopy.iopub = callbacks.iopub;
cbcopy.input = callbacks.input;
cbcopy.shell_done = (!callbacks.shell);
cbcopy.iopub_done = (!callbacks.iopub);
} else {
this.last_msg_callbacks = {};
}
};
/**
* @function _handle_shell_reply
*/
Kernel.prototype._handle_shell_reply = function (e) {
serialize.deserialize(e.data, $.proxy(this._finish_shell_reply, this));
};
Kernel.prototype._finish_shell_reply = function (reply) {
this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
var content = reply.content;
var metadata = reply.metadata;
var parent_id = reply.parent_header.msg_id;
var callbacks = this.get_callbacks_for_msg(parent_id);
if (!callbacks || !callbacks.shell) {
return;
}
var shell_callbacks = callbacks.shell;
// signal that shell callbacks are done
this._finish_shell(parent_id);
if (shell_callbacks.reply !== undefined) {
shell_callbacks.reply(reply);
}
if (content.payload && shell_callbacks.payload) {
this._handle_payloads(content.payload, shell_callbacks.payload, reply);
}
};
/**
* @function _handle_payloads
*/
Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
var l = payloads.length;
// Payloads are handled by triggering events because we don't want the Kernel
// to depend on the Notebook or Pager classes.
for (var i=0; i<l; i++) {
var payload = payloads[i];
var callback = payload_callbacks[payload.source];
if (callback) {
callback(payload, msg);
}
}
};
/**
* @function _handle_status_message
*/
Kernel.prototype._handle_status_message = function (msg) {
var execution_state = msg.content.execution_state;
var parent_id = msg.parent_header.msg_id;
// dispatch status msg callbacks, if any
var callbacks = this.get_callbacks_for_msg(parent_id);
if (callbacks && callbacks.iopub && callbacks.iopub.status) {
try {
callbacks.iopub.status(msg);
} catch (e) {
console.log("Exception in status msg handler", e, e.stack);
}
}
if (execution_state === 'busy') {
this.events.trigger('kernel_busy.Kernel', {kernel: this});
} else if (execution_state === 'idle') {
// signal that iopub callbacks are (probably) done
// async output may still arrive,
// but only for the most recent request
this._finish_iopub(parent_id);
// trigger status_idle event
this.events.trigger('kernel_idle.Kernel', {kernel: this});
} else if (execution_state === 'starting') {
this.events.trigger('kernel_starting.Kernel', {kernel: this});
var that = this;
this.kernel_info(function (reply) {
that.info_reply = reply.content;
that.events.trigger('kernel_ready.Kernel', {kernel: that});
});
} else if (execution_state === 'restarting') {
// autorestarting is distinct from restarting,
// in that it means the kernel died and the server is restarting it.
// kernel_restarting sets the notification widget,
// autorestart shows the more prominent dialog.
this._autorestart_attempt = this._autorestart_attempt + 1;
this.events.trigger('kernel_restarting.Kernel', {kernel: this});
this.events.trigger('kernel_autorestarting.Kernel', {kernel: this, attempt: this._autorestart_attempt});
} else if (execution_state === 'dead') {
this.events.trigger('kernel_dead.Kernel', {kernel: this});
this._kernel_dead();
}
};
/**
* Handle clear_output message
*
* @function _handle_clear_output
*/
Kernel.prototype._handle_clear_output = function (msg) {
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
if (!callbacks || !callbacks.iopub) {
return;
}
var callback = callbacks.iopub.clear_output;
if (callback) {
callback(msg);
}
};
/**
* handle an output message (execute_result, display_data, etc.)
*
* @function _handle_output_message
*/
Kernel.prototype._handle_output_message = function (msg) {
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
if (!callbacks || !callbacks.iopub) {
return;
}
var callback = callbacks.iopub.output;
if (callback) {