-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
index.ts
346 lines (306 loc) · 9.4 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
343
344
345
346
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ILabShell,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { IClientSession, ICommandPalette } from '@jupyterlab/apputils';
import { Cell, CodeCell } from '@jupyterlab/cells';
import {
CodeConsole,
ConsolePanel,
IConsoleTracker
} from '@jupyterlab/console';
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { FileEditor, IEditorTracker } from '@jupyterlab/fileeditor';
import {
INotebookTracker,
Notebook,
NotebookPanel
} from '@jupyterlab/notebook';
import {
IStatusBar,
KernelStatus,
LineCol,
MemoryUsage,
RunningSessions,
StatusBar
} from '@jupyterlab/statusbar';
import { ISettingRegistry } from '@jupyterlab/coreutils';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { Title, Widget } from '@phosphor/widgets';
export const STATUSBAR_PLUGIN_ID = '@jupyterlab/statusbar-extension:plugin';
/**
* Initialization data for the statusbar extension.
*/
const statusBar: JupyterFrontEndPlugin<IStatusBar> = {
id: STATUSBAR_PLUGIN_ID,
provides: IStatusBar,
autoStart: true,
activate: (
app: JupyterFrontEnd,
labShell: ILabShell | null,
settingRegistry: ISettingRegistry | null,
mainMenu: IMainMenu | null,
palette: ICommandPalette | null
) => {
const statusBar = new StatusBar();
statusBar.id = 'jp-main-statusbar';
app.shell.add(statusBar, 'bottom');
// If available, connect to the shell's layout modified signal.
if (labShell) {
labShell.layoutModified.connect(() => {
statusBar.update();
});
}
const category: string = 'Main Area';
const command: string = 'statusbar:toggle';
app.commands.addCommand(command, {
label: 'Show Status Bar',
execute: (args: any) => {
statusBar.setHidden(statusBar.isVisible);
if (settingRegistry) {
void settingRegistry.set(
STATUSBAR_PLUGIN_ID,
'visible',
statusBar.isVisible
);
}
},
isToggled: () => statusBar.isVisible
});
if (palette) {
palette.addItem({ command, category });
}
if (mainMenu) {
mainMenu.viewMenu.addGroup([{ command }], 1);
}
if (settingRegistry) {
const updateSettings = (settings: ISettingRegistry.ISettings): void => {
const visible = settings.get('visible').composite as boolean;
statusBar.setHidden(!visible);
};
Promise.all([settingRegistry.load(STATUSBAR_PLUGIN_ID), app.restored])
.then(([settings]) => {
updateSettings(settings);
settings.changed.connect(settings => {
updateSettings(settings);
});
})
.catch((reason: Error) => {
console.error(reason.message);
});
}
return statusBar;
},
optional: [ILabShell, ISettingRegistry, IMainMenu, ICommandPalette]
};
/**
* A plugin that provides a kernel status item to the status bar.
*/
export const kernelStatus: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/statusbar-extension:kernel-status',
autoStart: true,
requires: [IStatusBar, INotebookTracker, IConsoleTracker, ILabShell],
activate: (
app: JupyterFrontEnd,
statusBar: IStatusBar,
notebookTracker: INotebookTracker,
consoleTracker: IConsoleTracker,
labShell: ILabShell
) => {
// When the status item is clicked, launch the kernel
// selection dialog for the current session.
let currentSession: IClientSession | null = null;
const changeKernel = async () => {
if (!currentSession) {
return;
}
await currentSession.selectKernel();
};
// Create the status item.
const item = new KernelStatus({
onClick: changeKernel
});
// When the title of the active widget changes, update the label
// of the hover text.
const onTitleChanged = (title: Title<Widget>) => {
item.model!.activityName = title.label;
};
// Keep the session object on the status item up-to-date.
labShell.currentChanged.connect((_, change) => {
const { oldValue, newValue } = change;
// Clean up after the old value if it exists,
// listen for changes to the title of the activity
if (oldValue) {
oldValue.title.changed.disconnect(onTitleChanged);
}
if (newValue) {
newValue.title.changed.connect(onTitleChanged);
}
// Grab the session off of the current widget, if it exists.
if (newValue && consoleTracker.has(newValue)) {
currentSession = (newValue as ConsolePanel).session;
} else if (newValue && notebookTracker.has(newValue)) {
currentSession = (newValue as NotebookPanel).session;
} else {
currentSession = null;
}
item.model!.session = currentSession;
});
statusBar.registerStatusItem(
'@jupyterlab/statusbar-extension:kernel-status',
{
item,
align: 'left',
rank: 1,
isActive: () => {
const current = labShell.currentWidget;
return (
current &&
(notebookTracker.has(current) || consoleTracker.has(current))
);
}
}
);
}
};
/**
* A plugin providing a line/column status item to the application.
*/
export const lineColItem: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/statusbar-extension:line-col-status',
autoStart: true,
requires: [
IStatusBar,
INotebookTracker,
IEditorTracker,
IConsoleTracker,
ILabShell
],
activate: (
_: JupyterFrontEnd,
statusBar: IStatusBar,
notebookTracker: INotebookTracker,
editorTracker: IEditorTracker,
consoleTracker: IConsoleTracker,
labShell: ILabShell
) => {
const item = new LineCol();
const onActiveCellChanged = (notebook: Notebook, cell: Cell) => {
item.model!.editor = cell && cell.editor;
};
const onPromptCreated = (console: CodeConsole, prompt: CodeCell) => {
item.model!.editor = prompt && prompt.editor;
};
labShell.currentChanged.connect((_, change) => {
const { oldValue, newValue } = change;
// Check if we need to disconnect the console listener
// or the notebook active cell listener
if (oldValue && consoleTracker.has(oldValue)) {
(oldValue as ConsolePanel).console.promptCellCreated.disconnect(
onPromptCreated
);
} else if (oldValue && notebookTracker.has(oldValue)) {
(oldValue as NotebookPanel).content.activeCellChanged.disconnect(
onActiveCellChanged
);
}
// Wire up the new editor to the model if it exists
if (newValue && consoleTracker.has(newValue)) {
(newValue as ConsolePanel).console.promptCellCreated.connect(
onPromptCreated
);
const prompt = (newValue as ConsolePanel).console.promptCell;
item.model!.editor = prompt && prompt.editor;
} else if (newValue && notebookTracker.has(newValue)) {
(newValue as NotebookPanel).content.activeCellChanged.connect(
onActiveCellChanged
);
const cell = (newValue as NotebookPanel).content.activeCell;
item.model!.editor = cell && cell.editor;
} else if (newValue && editorTracker.has(newValue)) {
item.model!.editor = (newValue as IDocumentWidget<
FileEditor
>).content.editor;
} else {
item.model!.editor = null;
}
});
// Add the status item to the status bar.
statusBar.registerStatusItem(
'@jupyterlab/statusbar-extension:line-col-status',
{
item,
align: 'right',
rank: 2,
isActive: () => {
const current = labShell.currentWidget;
return (
current &&
(notebookTracker.has(current) ||
editorTracker.has(current) ||
consoleTracker.has(current))
);
}
}
);
}
};
/**
* A plugin providing memory usage statistics to the application.
*
* #### Notes
* This plugin will not work unless the memory usage server extension
* is installed.
*/
export const memoryUsageItem: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/statusbar-extension:memory-usage-status',
autoStart: true,
requires: [IStatusBar],
activate: (app: JupyterFrontEnd, statusBar: IStatusBar) => {
let item = new MemoryUsage();
statusBar.registerStatusItem(
'@jupyterlab/statusbar-extension:memory-usage-status',
{
item,
align: 'left',
rank: 2,
isActive: () => item.model!.metricsAvailable,
activeStateChanged: item.model!.stateChanged
}
);
}
};
/*
* A plugin providing running terminals and sessions information
* to the status bar.
*/
export const runningSessionsItem: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/statusbar-extension:running-sessions-status',
autoStart: true,
requires: [IStatusBar],
activate: (app: JupyterFrontEnd, statusBar: IStatusBar) => {
const item = new RunningSessions({
onClick: () => app.shell.activateById('jp-running-sessions'),
serviceManager: app.serviceManager
});
statusBar.registerStatusItem(
'@jupyterlab/statusbar-extension:running-sessions-status',
{
item,
align: 'left',
rank: 0
}
);
}
};
const plugins: JupyterFrontEndPlugin<any>[] = [
statusBar,
lineColItem,
kernelStatus,
runningSessionsItem,
memoryUsageItem
];
export default plugins;