forked from andrewray/iocamljs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel.js
517 lines (459 loc) · 17.2 KB
/
kernel.js
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
//----------------------------------------------------------------------------
// Copyright (C) 2008-2011 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
// HACK HACK HACK HACK
//============================================================================
// Kernel
//============================================================================
/**
* @module IPython
* @namespace IPython
* @submodule Kernel
*/
iocaml = {};
var IPython = (function (IPython) {
var utils = IPython.utils;
// Initialization and connection.
/**
* A Kernel Class to communicate with the Python kernel
* @Class Kernel
*/
var Kernel = function (base_url) {
this.kernel_id = null;
this.shell_channel = null;
this.iopub_channel = null;
this.stdin_channel = null;
this.base_url = base_url;
this.running = false;
this.username = "username";
this.session_id = utils.uuid();
this._msg_callbacks = {};
this.execution_count = 1;
this.execute_context = null;
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.');
};
};
Kernel.prototype._get_msg = function (msg_type, content) {
var msg = {
header : {
msg_id : utils.uuid(),
username : this.username,
session : this.session_id,
msg_type : msg_type
},
metadata : {},
content : content,
parent_header : {}
};
return msg;
};
/**
* Start the Python kernel
* @method start
*/
Kernel.prototype.start = function (notebook_id) {
};
/**
* Restart the python kernel.
*
* Emit a 'status_restarting.Kernel' event with
* the current object as parameter
*
* @method restart
*/
Kernel.prototype.restart = function () {
};
Kernel.prototype._kernel_started = function (json) {
};
Kernel.prototype._websocket_closed = function(ws_url, early) {
this.stop_channels();
$([IPython.events]).trigger('websocket_closed.Kernel',
{ws_url: ws_url, kernel: this, early: early}
);
};
/**
* Start the `shell`and `iopub` channels.
* Will stop and restart them if they already exist.
*
* @method start_channels
*/
Kernel.prototype.start_channels = function () {
};
/**
* Start the `shell`and `iopub` channels.
* @method stop_channels
*/
Kernel.prototype.stop_channels = function () {
};
// Main public methods.
/**
* Get info on object asynchronoulsy
*
* @async
* @param objname {string}
* @param callback {dict}
* @method object_info_request
*
* @example
*
* When calling this method pass a callbacks structure of the form:
*
* callbacks = {
* 'object_info_reply': object_info_reply_callback
* }
*
* The `object_info_reply_callback` will be passed the content object of the
*
* `object_into_reply` message documented in
* [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
*/
Kernel.prototype.object_info_request = function (objname, callbacks) {
if(typeof(objname)!=null && objname!=null)
{
var content = {
oname : objname.toString(),
};
var msg = this._get_msg("object_info_request", content);
this.shell_channel.send(JSON.stringify(msg));
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
return msg.header.msg_id;
}
return;
}
/**
* Execute given code into kernel, and pass result to callback.
*
* TODO: document input_request in callbacks
*
* @async
* @method execute
* @param {string} code
* @param [callbacks] {Object} With the optional following keys
* @param callbacks.'execute_reply' {function}
* @param callbacks.'output' {function}
* @param callbacks.'clear_output' {function}
* @param callbacks.'set_next_input' {function}
* @param {object} [options]
* @param [options.silent=false] {Boolean}
* @param [options.user_expressions=empty_dict] {Dict}
* @param [options.user_variables=empty_list] {List od Strings}
* @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_variables : [],
* user_expressions : {},
* allow_stdin : false
* }
*
* When calling this method pass a callbacks structure of the form:
*
* callbacks = {
* 'execute_reply': execute_reply_callback,
* 'output': output_callback,
* 'clear_output': clear_output_callback,
* 'set_next_input': set_next_input_callback
* }
*
* The `execute_reply_callback` will be passed the content and metadata
* objects of the `execute_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute)
*
* The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr')
* of the output and the content and metadata objects of the PUB/SUB channel that contains the
* output:
*
* http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket
*
* The `clear_output_callback` will be passed a content object that contains
* stdout, stderr and other fields that are booleans, as well as the metadata object.
*
* The `set_next_input_callback` will be passed the text that should become the next
* input cell.
*/
Kernel.prototype.send_stdout_message = function(message,where) {
if (this.execute_context !== null && this.execute_context !== undefined) {
var msg = this._get_msg("stream", {name: where, data: message});
msg.parent_header = this.execute_context;
this._handle_iopub_reply({data : JSON.stringify(msg)});
}
}
Kernel.prototype.send_pyout = function(execution_count,message) {
if (this.execute_context !== null && this.execute_context !== undefined) {
var msg = this._get_msg("pyout",
{
execution_count: execution_count,
data: { "text/html" : message },
metadata: {}
});
msg.parent_header = this.execute_context;
this._handle_iopub_reply({data : JSON.stringify(msg)});
}
}
Kernel.prototype.send_mime = function(mime_type,data) {
if (this.execute_context !== null && this.execute_context !== undefined) {
var d = {};
d[mime_type] = data;
var msg = this._get_msg("display_data",
{
source : 'iocamljs',
data : d,
metadata : {}
});
msg.parent_header = this.execute_context;
this._handle_iopub_reply({data : JSON.stringify(msg)});
}
}
Kernel.prototype.send_clear = function(wait,stdout,stderr,other) {
if (this.execute_context !== null && this.execute_context !== undefined) {
var msg = this._get_msg("clear_output",
{
wait: wait,
stdout: stdout,
stderr: stderr,
other: other
});
msg.parent_header = this.execute_context;
this._handle_iopub_reply({data : JSON.stringify(msg)});
}
}
Kernel.prototype.execute = function (code, callbacks, options) {
var that = this;
var content = {
code : code,
silent : true,
user_variables : [],
user_expressions : {},
allow_stdin : false
};
callbacks = callbacks || {};
if (callbacks.input_request !== undefined) {
content.allow_stdin = true;
}
$.extend(true, content, options)
$([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
var request = this._get_msg("execute_request", content);
this.set_callbacks_for_msg(request.header.msg_id, callbacks);
this.execute_context = request.header;
var r = null;
var success = true;
var save_console_log = console.log;
console.log = function() {
var data = ""
for (var i = 0; i < arguments.length; i++) {
if (i) data += " ";
data += arguments[i];
}
data += "\n";
var msg = that._get_msg("stream", {name: "stdout", data: data});
msg.parent_header = request.header;
that._handle_iopub_reply({data : JSON.stringify(msg)});
}
try {
var res = iocaml.execute(this.execution_count, code);
r = res.message;
success = res.compilerStatus;
} catch(err) {
r = err; // presumably a javscript exception
success = false;
}
console.log = save_console_log;
var reply = this._get_msg("execute_reply", {
status : "ok",
execution_count: this.execution_count
});
reply.parent_header = request.header;
/*var result = null;
if (r !== null && r !== undefined) {
if (success) {
result = this._get_msg("pyout", {
execution_count: this.execution_count,
data : {
'text/plain' : "" + r
},
metadata : {}
});
} else if (!success){
result = this._get_msg("pyout", {
execution_count: this.execution_count,
data : {
'text/html' : '<b><pre style="color:red">' + r + '</pre></b>'
},
metadata : {}
});
}
result.parent_header = request.header;
this._handle_iopub_reply({data : JSON.stringify(result)});
}*/
var idle = this._get_msg("status", {status: "idle"})
idle.parent_header = request.header;
this.execution_count = this.execution_count + 1;
this._handle_iopub_reply({data : JSON.stringify(idle)});
this._handle_shell_reply({data : JSON.stringify(reply)});
};
/**
* When calling this method pass a callbacks structure of the form:
*
* callbacks = {
* 'complete_reply': complete_reply_callback
* }
*
* The `complete_reply_callback` will be passed the content object of the
* `complete_reply` message documented
* [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
*
* @method complete
* @param line {integer}
* @param cursor_pos {integer}
* @param {dict} callbacks
* @param callbacks.complete_reply {function} `complete_reply_callback`
*
*/
Kernel.prototype.complete = function (line, cursor_pos, callbacks) {
callbacks = callbacks || {};
var content = {
text : '',
line : line,
cursor_pos : cursor_pos
};
var msg = this._get_msg("complete_request", content);
this.shell_channel.send(JSON.stringify(msg));
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
return msg.header.msg_id;
};
Kernel.prototype.interrupt = function () {
if (this.running) {
$([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
$.post(this.kernel_url + "/interrupt");
};
};
Kernel.prototype.kill = function () {
if (this.running) {
this.running = false;
var settings = {
cache : false,
type : "DELETE"
};
$.ajax(this.kernel_url, settings);
};
};
Kernel.prototype.send_input_reply = function (input) {
var content = {
value : input,
};
$([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
var msg = this._get_msg("input_reply", content);
this.stdin_channel.send(JSON.stringify(msg));
return msg.header.msg_id;
};
// Reply handlers
Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
var callbacks = this._msg_callbacks[msg_id];
return callbacks;
};
Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
this._msg_callbacks[msg_id] = callbacks || {};
}
Kernel.prototype._handle_shell_reply = function (e) {
var reply = $.parseJSON(e.data);
$([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
var header = reply.header;
var content = reply.content;
var metadata = reply.metadata;
var msg_type = header.msg_type;
var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
if (callbacks !== undefined) {
var cb = callbacks[msg_type];
if (cb !== undefined) {
cb(content, metadata);
}
};
if (content.payload !== undefined) {
var payload = content.payload || [];
this._handle_payload(callbacks, payload);
}
};
Kernel.prototype._handle_payload = function (callbacks, payload) {
var l = payload.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++) {
if (payload[i].source === 'IPython.kernel.zmq.page.page') {
var data = {'text':payload[i].text}
$([IPython.events]).trigger('open_with_text.Pager', data);
} else if (payload[i].source === 'IPython.kernel.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
if (callbacks.set_next_input !== undefined) {
callbacks.set_next_input(payload[i].text)
}
}
};
};
Kernel.prototype._handle_iopub_reply = function (e) {
var reply = $.parseJSON(e.data);
var content = reply.content;
var msg_type = reply.header.msg_type;
var metadata = reply.metadata;
var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
if (msg_type !== 'status' && callbacks === undefined) {
// Message not from one of this notebook's cells and there are no
// callbacks to handle it.
return;
}
var output_types = ['stream','display_data','pyout','pyerr'];
if (output_types.indexOf(msg_type) >= 0) {
var cb = callbacks['output'];
if (cb !== undefined) {
cb(msg_type, content, metadata);
}
} else if (msg_type === 'status') {
if (content.execution_state === 'busy') {
$([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
} else if (content.execution_state === 'idle') {
$([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
} else if (content.execution_state === 'restarting') {
$([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
} else if (content.execution_state === 'dead') {
this.stop_channels();
$([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
};
} else if (msg_type === 'clear_output') {
var cb = callbacks['clear_output'];
if (cb !== undefined) {
cb(content, metadata);
}
};
};
Kernel.prototype._handle_input_request = function (e) {
var request = $.parseJSON(e.data);
var header = request.header;
var content = request.content;
var metadata = request.metadata;
var msg_type = header.msg_type;
if (msg_type !== 'input_request') {
console.log("Invalid input request!", request);
return;
}
var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
if (callbacks !== undefined) {
var cb = callbacks[msg_type];
if (cb !== undefined) {
cb(content, metadata);
}
};
};
IPython.Kernel = Kernel;
return IPython;
}(IPython));