-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
CellAdapter.ts
executable file
路275 lines (258 loc) 路 9.54 KB
/
CellAdapter.ts
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
/*
* Copyright (c) 2022-2023 Datalayer Inc. All rights reserved.
*
* MIT License
*/
import { BoxPanel, Widget } from '@lumino/widgets';
import { find } from '@lumino/algorithm';
import { CommandRegistry } from '@lumino/commands';
import { SessionContext, ISessionContext, Toolbar, ToolbarButton } from '@jupyterlab/apputils';
import { CodeCellModel, CodeCell, Cell } from '@jupyterlab/cells';
import { ybinding, CodeMirrorMimeTypeService, EditorLanguageRegistry, CodeMirrorEditorFactory, EditorExtensionRegistry, EditorThemeRegistry } from '@jupyterlab/codemirror';
import { Completer, CompleterModel, CompletionHandler, ProviderReconciliator, KernelCompleterProvider } from '@jupyterlab/completer';
import { RenderMimeRegistry, standardRendererFactories as initialFactories } from '@jupyterlab/rendermime';
import { Session, ServerConnection, SessionManager, KernelManager, KernelSpecManager } from '@jupyterlab/services';
import { runIcon } from '@jupyterlab/ui-components';
import { createStandaloneCell, YCodeCell, IYText } from '@jupyter/ydoc';
import { requireLoader as loader } from "@jupyter-widgets/html-manager";
import { WIDGET_MIMETYPE, WidgetRenderer } from "@jupyter-widgets/html-manager/lib/output_renderers";
import IPyWidgetsManager from "../../jupyter/ipywidgets/IPyWidgetsManager";
import Kernel from '../../jupyter/kernel/Kernel';
import CellCommands from './CellCommands';
export class CellAdapter {
private _codeCell: CodeCell;
private _kernel: Kernel;
private _panel: BoxPanel;
private _sessionContext: SessionContext;
constructor(options: CellAdapter.ICellAdapterOptions) {
const { source, serverSettings, kernel } = options;
this._kernel = kernel;
const kernelManager = kernel?.kernelManager ?? new KernelManager({
serverSettings
});
const sessionManager = kernel?.sessionManager ?? new SessionManager({
serverSettings,
kernelManager
});
const specsManager = kernel?.kernelSpecManager ?? new KernelSpecManager({
serverSettings
});
const kernelPreference: ISessionContext.IKernelPreference = kernel ?
{
id: kernel.id,
shouldStart: false,
autoStartDefault: false,
shutdownOnDispose: false,
}
:
{
name: 'python',
shouldStart: true,
autoStartDefault: true,
shutdownOnDispose: true,
}
this._sessionContext = new SessionContext({
name: kernel?.path,
path: kernel?.path,
sessionManager,
specsManager,
type: 'python',
kernelPreference,
});
// These are fixes to have more control on the kernel launch.
(this._sessionContext as any)._initialize = async (): Promise<boolean> => {
const manager = (this._sessionContext as any).sessionManager as SessionManager;
await manager.ready;
await manager.refreshRunning();
const model = find(manager.running(), item => {
// return (item as any).path === (this._sessionContext as any)._path;
return item.kernel?.id === this._kernel.id;
});
if (model) {
try {
const session = manager.connectTo({
model,
kernelConnectionOptions: {
handleComms: true,
}
});
(this._sessionContext as any)._handleNewSession(session);
}
catch (err) {
void (this._sessionContext as any)._handleSessionError(err);
return Promise.reject(err);
}
}
return await (this._sessionContext as any)._startIfNecessary();
};
const themes = new EditorThemeRegistry();
for (const theme of EditorThemeRegistry.getDefaultThemes()) {
themes.addTheme(theme);
}
const editorExtensions = () => {
const registry = new EditorExtensionRegistry();
for (const extensionFactory of EditorExtensionRegistry.getDefaultExtensions({ themes })) {
registry.addExtension(extensionFactory);
}
registry.addExtension({
name: 'shared-model-binding',
factory: options => {
const sharedModel = options.model.sharedModel as IYText;
return EditorExtensionRegistry.createImmutableExtension(
ybinding({
ytext: sharedModel.ysource,
undoManager: sharedModel.undoManager ?? undefined,
})
);
}
});
return registry;
}
const languages = new EditorLanguageRegistry();
for (const language of EditorLanguageRegistry.getDefaultLanguages()) {
languages.addLanguage(language);
}
languages.addLanguage({
name: 'ipythongfm',
mime: 'text/x-ipythongfm',
load: async () => {
// TODO: add support for LaTeX.
const m = await import('@codemirror/lang-markdown');
return m.markdown({
codeLanguages: (info: string) => languages.findBest(info) as any
});
}
});
const mimeService = new CodeMirrorMimeTypeService(languages);
const commands = new CommandRegistry();
const useCapture = true;
document.addEventListener(
'keydown',
event => {
commands.processKeydownEvent(event);
},
useCapture
);
const rendermime = new RenderMimeRegistry({ initialFactories });
const iPyWidgetsClassicManager = new IPyWidgetsManager({ loader });
rendermime.addFactory(
{
safe: false,
mimeTypes: [WIDGET_MIMETYPE],
createRenderer: (options) => new WidgetRenderer(options, iPyWidgetsClassicManager),
},
0
);
const factoryService = new CodeMirrorEditorFactory({
extensions: editorExtensions(),
languages,
});
this._codeCell = new CodeCell({
rendermime,
model: new CodeCellModel({
sharedModel: createStandaloneCell({
cell_type: 'code',
source: source,
metadata: {
}
}) as YCodeCell
}),
contentFactory: new Cell.ContentFactory({
editorFactory: factoryService.newInlineEditor.bind(factoryService),
}),
});
this._codeCell.addClass('dla-Jupyter-Cell');
this._codeCell.initializeState();
this._sessionContext.kernelChanged.connect((_, arg: Session.ISessionConnection.IKernelChangedArgs) => {
const kernelConnection = arg.newValue;
console.log('Current Kernel Connection', kernelConnection);
if (kernelConnection && !kernelConnection.handleComms) {
console.warn('Kernel Connection does not handle Comms', kernelConnection.id);
(kernelConnection as any).handleComms = true;
console.log('Kernel Connection is updated to enforce Comms support', kernelConnection.handleComms);
}
iPyWidgetsClassicManager.registerWithKernel(kernelConnection);
});
this._sessionContext.kernelChanged.connect(() => {
void this._sessionContext.session?.kernel?.info.then(info => {
const lang = info.language_info;
const mimeType = mimeService.getMimeTypeByLanguage(lang);
this._codeCell.model.mimeType = mimeType;
});
});
const editor = this._codeCell.editor;
const model = new CompleterModel();
const completer = new Completer({ editor, model });
const timeout = 1000;
const provider = new KernelCompleterProvider();
const reconciliator = new ProviderReconciliator({
context: { widget: this._codeCell, editor, session: this._sessionContext.session },
providers: [provider],
timeout,
});
const handler = new CompletionHandler({ completer, reconciliator });
void this._sessionContext.ready.then(() => {
const provider = new KernelCompleterProvider();
handler.reconciliator = new ProviderReconciliator({
context: { widget: this._codeCell, editor, session: this._sessionContext.session },
providers: [provider],
timeout,
});
});
handler.editor = editor;
CellCommands(commands, this._codeCell!, this._sessionContext, handler);
completer.hide();
completer.addClass('jp-Completer-Cell');
Widget.attach(completer, document.body);
const toolbar = new Toolbar();
toolbar.addItem('spacer', Toolbar.createSpacerItem());
const runButton = new ToolbarButton({
icon: runIcon,
onClick: () => {
CodeCell.execute(this._codeCell, this._sessionContext);
},
tooltip: 'Run'
});
toolbar.addItem('run', runButton);
toolbar.addItem('interrupt', Toolbar.createInterruptButton(this._sessionContext));
toolbar.addItem('restart', Toolbar.createRestartButton(this._sessionContext));
// toolbar.addItem('name', Toolbar.createKernelNameItem(this._sessionContext));
toolbar.addItem('status', Toolbar.createKernelStatusItem(this._sessionContext));
this._codeCell.outputsScrolled = false;
this._codeCell.activate();
this._panel = new BoxPanel();
this._panel.direction = 'top-to-bottom';
this._panel.spacing = 0;
this._panel.addWidget(toolbar);
this._panel.addWidget(this._codeCell);
BoxPanel.setStretch(toolbar, 0);
BoxPanel.setStretch(this._codeCell, 1);
window.addEventListener('resize', () => {
this._panel.update();
});
this._panel.update();
}
get panel(): BoxPanel {
return this._panel;
}
get codeCell(): CodeCell {
return this._codeCell;
}
get sessionContext(): SessionContext {
return this._sessionContext;
}
get kernel(): Kernel {
return this._kernel;
}
execute = () => {
CodeCell.execute(this._codeCell, this._sessionContext);
}
}
export namespace CellAdapter {
export type ICellAdapterOptions = {
source: string;
serverSettings: ServerConnection.ISettings;
kernel: Kernel;
}
}
export default CellAdapter;