@jataware/beaker-client 2.0.0-b.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/dist/extension.d.ts +21 -0
- package/dist/extension.js +3 -0
- package/dist/history.d.ts +39 -0
- package/dist/history.js +16 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +10 -0
- package/dist/notebook.d.ts +192 -0
- package/dist/notebook.js +690 -0
- package/dist/render.d.ts +38 -0
- package/dist/render.js +84 -0
- package/dist/session.d.ts +166 -0
- package/dist/session.js +418 -0
- package/dist/util.d.ts +50 -0
- package/dist/util.js +112 -0
- package/package.json +39 -0
- package/src/extension.ts +34 -0
- package/src/history.ts +61 -0
- package/src/index.ts +12 -0
- package/src/notebook.ts +949 -0
- package/src/render.ts +121 -0
- package/src/session.ts +504 -0
- package/src/util.ts +173 -0
package/src/util.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import { JSONObject } from '@lumino/coreutils';
|
|
3
|
+
import { KernelFutureHandler, KernelShellFutureHandler } from '@jupyterlab/services/lib/kernel/future';
|
|
4
|
+
import * as nbformat from '@jupyterlab/nbformat';
|
|
5
|
+
import * as Kernel from '@jupyterlab/services/lib/kernel/kernel';
|
|
6
|
+
import * as messages from '@jupyterlab/services/lib/kernel/messages';
|
|
7
|
+
import { IBeakerShellMessage, BeakerCodeCell, IBeakerIOPubMessage, IBeakerCell, BeakerNotebook, BeakerNotebookContent, BeakerBaseCell } from './notebook';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// Lower case states to match the naming in the messages.
|
|
11
|
+
export enum KernelState {
|
|
12
|
+
unknown = 'unknown',
|
|
13
|
+
starting = 'starting',
|
|
14
|
+
idle = 'idle',
|
|
15
|
+
busy = 'busy',
|
|
16
|
+
terminating = 'terminating',
|
|
17
|
+
restarting = 'restarting',
|
|
18
|
+
autorestarting = 'autorestarting',
|
|
19
|
+
dead = 'dead'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const createMessageId = (msgType: string): string => {
|
|
24
|
+
const uuid = uuidv4().replaceAll('-', '').slice(0, 16);
|
|
25
|
+
return `beaker-${uuid}-${msgType}`;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// export type IBeakerFutureMessage = IBeakerShellMessage | messages.IShellMessage;
|
|
29
|
+
|
|
30
|
+
export interface IBeakerFuture<REQUEST extends IBeakerShellMessage = IBeakerShellMessage, REPLY extends IBeakerShellMessage = IBeakerShellMessage> extends Kernel.IShellFuture<REQUEST, REPLY> {
|
|
31
|
+
msgId?: string;
|
|
32
|
+
onResponse?: (msg: any) => void | PromiseLike<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IBeakerAvailableContexts {
|
|
36
|
+
[key: string]: {
|
|
37
|
+
languages: {
|
|
38
|
+
slug: string,
|
|
39
|
+
kernel: string,
|
|
40
|
+
},
|
|
41
|
+
defaultPayload: string,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface IActiveContextInfo {
|
|
46
|
+
slug: string;
|
|
47
|
+
class: string;
|
|
48
|
+
config: JSONObject;
|
|
49
|
+
language: {
|
|
50
|
+
slug: string;
|
|
51
|
+
subkernel: string;
|
|
52
|
+
}
|
|
53
|
+
kernelInfo?: any;
|
|
54
|
+
name?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
export class BeakerCellFuture extends KernelFutureHandler<IBeakerShellMessage, IBeakerShellMessage> {
|
|
59
|
+
/**
|
|
60
|
+
* Construct a new KernelFutureHandler.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
constructor(
|
|
64
|
+
msg: IBeakerShellMessage,
|
|
65
|
+
expectReply: boolean,
|
|
66
|
+
disposeOnDone: boolean,
|
|
67
|
+
kernel?: Kernel.IKernelConnection | null,
|
|
68
|
+
cell?: BeakerCodeCell,
|
|
69
|
+
) {
|
|
70
|
+
|
|
71
|
+
if (!kernel) {
|
|
72
|
+
throw Error("Unable to send message. Not connected to kernel.");
|
|
73
|
+
}
|
|
74
|
+
if (!cell) {
|
|
75
|
+
throw Error("Cannot execute in cell as cell is undefined or invalid.");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
const msgId = msg.header.msg_id;
|
|
80
|
+
// Ensure channel is set if created via alternate means
|
|
81
|
+
if (msg.channel === undefined) {
|
|
82
|
+
msg.channel = "shell";
|
|
83
|
+
}
|
|
84
|
+
// Clean up from kernel future list when complete.
|
|
85
|
+
const disposalCallback: (() => void) = () => {
|
|
86
|
+
// @ts-ignore -- Deregister future via private member for cleanup
|
|
87
|
+
kernel._futures.delete(msgId);
|
|
88
|
+
};
|
|
89
|
+
super(disposalCallback, msg, expectReply, disposeOnDone, kernel)
|
|
90
|
+
// @ts-ignore -- Register future via private member
|
|
91
|
+
kernel._futures?.set(msgId, this);
|
|
92
|
+
this.cell = cell;
|
|
93
|
+
this.onIOPub = (msg: IBeakerIOPubMessage) => BeakerCellFutures.handleIOPub(msg, this.cell);
|
|
94
|
+
this.onReply = (msg: IBeakerShellMessage) => BeakerCellFutures.handleReply(msg, this.cell);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
private cell: BeakerCodeCell;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export namespace BeakerCellFutures {
|
|
102
|
+
|
|
103
|
+
export const handleIOPub = (msg: IBeakerIOPubMessage, cell: IBeakerCell): void => {
|
|
104
|
+
const msg_type = msg.header.msg_type;
|
|
105
|
+
const content = msg.content;
|
|
106
|
+
if (msg_type === "status") {
|
|
107
|
+
cell.status = content.execution_state;
|
|
108
|
+
}
|
|
109
|
+
else if (msg_type === "execute_result") {
|
|
110
|
+
cell.busy = false;
|
|
111
|
+
if (content.execution_count) {
|
|
112
|
+
cell.execution_count = content.execution_count;
|
|
113
|
+
}
|
|
114
|
+
cell.outputs.push({
|
|
115
|
+
output_type: "execute_result",
|
|
116
|
+
...content
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else if (msg_type === "stream") {
|
|
120
|
+
cell.outputs.push({
|
|
121
|
+
output_type: "stream",
|
|
122
|
+
...content
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else if (msg_type === "display_data") {
|
|
126
|
+
cell.outputs.push({
|
|
127
|
+
output_type: "display_data",
|
|
128
|
+
...content
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const handleReply = (msg: IBeakerShellMessage, cell: IBeakerCell) => {
|
|
134
|
+
cell.busy = false;
|
|
135
|
+
if (msg.content.status === "ok") {
|
|
136
|
+
cell.last_execution = {...cell.last_execution, status: "ok"};
|
|
137
|
+
cell.execution_count = Number(msg.content.execution_count);
|
|
138
|
+
}
|
|
139
|
+
else if (msg.content.status === "error") {
|
|
140
|
+
cell.execution_count = Number(msg.content.execution_count);
|
|
141
|
+
const error_details = {
|
|
142
|
+
ename: msg.content.ename,
|
|
143
|
+
evalue: msg.content.evalue,
|
|
144
|
+
traceback: msg.content.traceback,
|
|
145
|
+
};
|
|
146
|
+
cell.last_execution = {
|
|
147
|
+
status: "error",
|
|
148
|
+
...error_details
|
|
149
|
+
};
|
|
150
|
+
cell.outputs.push({
|
|
151
|
+
output_type: "error",
|
|
152
|
+
...error_details
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else if (msg.content.status === "abort") {
|
|
156
|
+
cell.execution_count = msg.content.execution_count;
|
|
157
|
+
const error_details = {
|
|
158
|
+
ename: "Execution aborted",
|
|
159
|
+
evalue: "Execution aborted",
|
|
160
|
+
traceback: [],
|
|
161
|
+
};
|
|
162
|
+
cell.last_execution = {
|
|
163
|
+
status: "error",
|
|
164
|
+
...error_details,
|
|
165
|
+
};
|
|
166
|
+
cell.outputs.push({
|
|
167
|
+
output_type: "error",
|
|
168
|
+
content: error_details,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
}
|