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