-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathindex.ts
342 lines (300 loc) · 10.9 KB
/
index.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
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
import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';
import {
ILabShell,
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { IConsoleTracker } from '@jupyterlab/console';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { listIcon } from '@jupyterlab/ui-components';
import { DummyHandler, VariableInspectionHandler } from './handler';
import { Languages } from './inspectorscripts';
import { KernelConnector } from './kernelconnector';
import { VariableInspectorManager } from './manager';
import { VariableInspectorPanel } from './variableinspector';
import { IVariableInspector, IVariableInspectorManager } from './tokens';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { addJupyterLabThemeChangeListener } from '@jupyter/web-components';
import { PromiseDelegate } from '@lumino/coreutils';
addJupyterLabThemeChangeListener();
namespace CommandIDs {
export const open = 'variableinspector:open';
}
/**
* A service providing variable introspection.
*/
const variableinspector: JupyterFrontEndPlugin<IVariableInspectorManager> = {
id: '@lckr/jupyterlab_variableinspector:plugin',
requires: [ICommandPalette, ILayoutRestorer, ILabShell, ISettingRegistry],
provides: IVariableInspectorManager,
autoStart: true,
activate: async (
app: JupyterFrontEnd,
palette: ICommandPalette,
restorer: ILayoutRestorer,
labShell: ILabShell,
settingRegistry: ISettingRegistry
): Promise<IVariableInspectorManager> => {
const manager = new VariableInspectorManager();
const category = 'Variable Inspector';
const command = CommandIDs.open;
const label = 'Open Variable Inspector';
const namespace = 'variableinspector';
const tracker = new WidgetTracker<VariableInspectorPanel>({ namespace });
const settings = new PromiseDelegate<ISettingRegistry.ISettings>();
Promise.all([settingRegistry.load(variableinspector.id), app.restored])
.then(returnArray => {
return settings.resolve(returnArray[0]);
})
.catch(error => {
console.error('Failed to load settings for the Git Extension', error);
settings.reject(error);
});
/**
* Create and track a new inspector.
*/
function newPanel(): VariableInspectorPanel {
const panel = new VariableInspectorPanel();
panel.settingsPromise = settings;
panel.id = 'jp-variableinspector';
panel.title.label = 'Variable Inspector';
panel.title.icon = listIcon;
panel.title.closable = true;
panel.disposed.connect(() => {
if (manager.panel === panel) {
manager.panel = null;
}
});
//Track the inspector panel
tracker.add(panel);
return panel;
}
// Enable state restoration
restorer.restore(tracker, {
command,
args: () => ({}),
name: () => 'variableinspector'
});
// Add command to palette
app.commands.addCommand(command, {
label,
execute: () => {
if (!manager.panel || manager.panel.isDisposed) {
manager.panel = newPanel();
}
if (!manager.panel.isAttached) {
labShell.add(manager.panel, 'main');
}
if (manager.source) {
manager.source.performInspection();
}
labShell.activateById(manager.panel.id);
}
});
palette.addItem({ command, category });
console.log(
'JupyterLab extension @lckr/jupyterlab_variableinspector is activated!'
);
return manager;
}
};
/**
* An extension that registers consoles for variable inspection.
*/
const consoles: JupyterFrontEndPlugin<void> = {
id: '@lckr/jupyterlab-variableinspector:consoles',
requires: [IVariableInspectorManager, IConsoleTracker, ILabShell],
autoStart: true,
activate: (
app: JupyterFrontEnd,
manager: IVariableInspectorManager,
consoles: IConsoleTracker,
labShell: ILabShell
): void => {
const handlers: {
[id: string]: Promise<IVariableInspector.IInspectable>;
} = {};
/**
* Subscribes to the creation of new consoles. If a new notebook is created, build a new handler for the consoles.
* Adds a promise for a instanced handler to the 'handlers' collection.
*/
consoles.widgetAdded.connect((sender, consolePanel) => {
if (manager.hasHandler(consolePanel.sessionContext.path)) {
handlers[consolePanel.id] = new Promise((resolve, reject) => {
resolve(manager.getHandler(consolePanel.sessionContext.path));
});
} else {
handlers[consolePanel.id] = new Promise((resolve, reject) => {
const session = consolePanel.sessionContext;
// Create connector and init w script if it exists for kernel type.
const connector = new KernelConnector({ session });
const scripts: Promise<Languages.LanguageModel> =
connector.ready.then(() => {
return connector.kernelLanguage.then(lang => {
return Languages.getScript(lang);
});
});
scripts.then((result: Languages.LanguageModel) => {
const initScript = result.initScript;
const queryCommand = result.queryCommand;
const matrixQueryCommand = result.matrixQueryCommand;
const widgetQueryCommand = result.widgetQueryCommand;
const deleteCommand = result.deleteCommand;
const options: VariableInspectionHandler.IOptions = {
queryCommand: queryCommand,
matrixQueryCommand: matrixQueryCommand,
widgetQueryCommand,
deleteCommand: deleteCommand,
connector: connector,
initScript: initScript,
id: session.path //Using the sessions path as an identifier for now.
};
const handler = new VariableInspectionHandler(options);
manager.addHandler(handler);
consolePanel.disposed.connect(() => {
delete handlers[consolePanel.id];
handler.dispose();
});
handler.ready.then(() => {
resolve(handler);
});
});
//Otherwise log error message.
scripts.catch((result: string) => {
console.log(result);
const handler = new DummyHandler(connector);
consolePanel.disposed.connect(() => {
delete handlers[consolePanel.id];
handler.dispose();
});
resolve(handler);
});
});
}
setSource(labShell);
});
const setSource = (sender: ILabShell, args?: ILabShell.IChangedArgs) => {
const widget = args?.newValue ?? sender.currentWidget;
if (!widget || !consoles.has(widget)) {
return;
}
const future = handlers[widget.id];
future.then((source: IVariableInspector.IInspectable) => {
if (source) {
manager.source = source;
manager.source.performInspection();
}
});
};
/**
* If focus window changes, checks whether new focus widget is a console.
* In that case, retrieves the handler associated to the console after it has been
* initialized and updates the manager with it.
*/
setSource(labShell);
labShell.currentChanged.connect(setSource);
app.contextMenu.addItem({
command: CommandIDs.open,
selector: '.jp-CodeConsole'
});
}
};
/**
* An extension that registers notebooks for variable inspection.
*/
const notebooks: JupyterFrontEndPlugin<void> = {
id: '@lckr/jupyterlab-variableinspector:notebooks',
requires: [IVariableInspectorManager, INotebookTracker, ILabShell],
autoStart: true,
activate: (
app: JupyterFrontEnd,
manager: IVariableInspectorManager,
notebooks: INotebookTracker,
labShell: ILabShell
): void => {
const handlers: { [id: string]: Promise<VariableInspectionHandler> } = {};
/**
* Subscribes to the creation of new notebooks. If a new notebook is created, build a new handler for the notebook.
* Adds a promise for a instanced handler to the 'handlers' collection.
*/
notebooks.widgetAdded.connect((sender, nbPanel: NotebookPanel) => {
//A promise that resolves after the initialization of the handler is done.
handlers[nbPanel.id] = new Promise((resolve, reject) => {
const session = nbPanel.sessionContext;
const connector = new KernelConnector({ session });
const rendermime = nbPanel.content.rendermime;
const scripts: Promise<Languages.LanguageModel> = connector.ready.then(
async () => {
const lang = await connector.kernelLanguage;
return Languages.getScript(lang);
}
);
scripts.then((result: Languages.LanguageModel) => {
const initScript = result.initScript;
const queryCommand = result.queryCommand;
const matrixQueryCommand = result.matrixQueryCommand;
const widgetQueryCommand = result.widgetQueryCommand;
const deleteCommand = result.deleteCommand;
const options: VariableInspectionHandler.IOptions = {
queryCommand: queryCommand,
matrixQueryCommand: matrixQueryCommand,
widgetQueryCommand,
deleteCommand: deleteCommand,
connector: connector,
rendermime,
initScript: initScript,
id: session.path //Using the sessions path as an identifier for now.
};
const handler = new VariableInspectionHandler(options);
manager.addHandler(handler);
nbPanel.disposed.connect(() => {
delete handlers[nbPanel.id];
handler.dispose();
});
handler.ready.then(() => {
resolve(handler);
});
});
//Otherwise log error message.
scripts.catch((result: string) => {
reject(result);
});
});
setSource(labShell);
});
const setSource = (sender: ILabShell, args?: ILabShell.IChangedArgs) => {
const widget = args?.newValue ?? sender.currentWidget;
if (!widget || !notebooks.has(widget) || widget.isDisposed) {
return;
}
const future = handlers[widget.id];
future?.then((source: VariableInspectionHandler) => {
if (source) {
manager.source = source;
manager.source.performInspection();
}
});
};
/**
* If focus window changes, checks whether new focus widget is a notebook.
* In that case, retrieves the handler associated to the notebook after it has been
* initialized and updates the manager with it.
*/
setSource(labShell);
labShell.currentChanged.connect(setSource);
app.contextMenu.addItem({
command: CommandIDs.open,
selector: '.jp-Notebook'
});
}
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [
variableinspector,
consoles,
notebooks
];
export default plugins;