forked from Joeyonng/react-jupyter-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleMessenger.js
94 lines (87 loc) · 2.49 KB
/
ExampleMessenger.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
import { KernelMessenger } from './lib/index';
function delay(time) {
return new Promise((res, rej) => setTimeout(res, time));
}
export default class ExampleMessenger extends KernelMessenger {
timeout = null;
get kernelInfo() {
return Promise.resolve({
implementation: 'Example',
implementation_version: 'N/A',
language_info: {
codemirror_mode: {
name: 'N/A',
version: -1,
},
file_extension: 'N/A',
mimetype: 'N/A',
name: 'N/A',
nbconvert_exporter: 'N/A',
pygments_lexer: 'N/A',
version: '1.0',
},
protocol_version: 'N/A',
status: 'ok',
banner: 'Example Messenger: An example of the messaging framework.',
help_links: [],
});
}
connectToKernel() {
this.updConnState(null);
delay(2000).then(() => this.updConnState(true));
}
runCode(code, callback) {
return new Promise((res, rej) => {
if (!this.connected) {
rej('Kernel Execute Error: Not ready!');
} else {
const sendResponse = () => {
// Signal our "kernel" is busy
callback({
header: { msg_type: 'status' },
content: { execution_state: 'busy' },
});
// Send the execution counter
callback({
header: { msg_type: 'execute_input' },
content: { execution_count: '?' },
});
const msDelay = 2000;
return delay(msDelay).then((_) => {
// Stream results
callback({
header: { msg_type: 'error' },
content: {
output_type: 'error',
traceback: ['Code entered was:\n'],
},
});
callback({
header: { msg_type: 'stream' },
content: {
name: 'stdout',
output_type: 'stream',
text: code,
},
});
// Signal our "kernel" is idle
callback({
header: { msg_type: 'status' },
content: { execution_state: 'idle' },
});
res();
});
};
if (this.timeout !== null) {
this.timeout.then(sendResponse);
} else {
this.timeout = Promise.resolve().then((_) => sendResponse());
}
}
});
}
signalKernel(signal) {
// Return whether the execution worked or not
return Promise.reject('Kernel Signal Error: Not available');
}
}