-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
kernelStatus.tsx
300 lines (252 loc) · 7.53 KB
/
kernelStatus.tsx
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
/**
* Default item to display the status of the kernel of the active notbeook or console.
*/
/**
* Part of Jupyterlab status bar defaults.
*/
import React from 'react';
import {
JupyterLabPlugin,
JupyterLab,
ApplicationShell
} from '@jupyterlab/application';
import { IClientSession, VDomRenderer, VDomModel } from '@jupyterlab/apputils';
import { IConsoleTracker, ConsolePanel } from '@jupyterlab/console';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { Kernel, Session } from '@jupyterlab/services';
import {
interactiveItem,
IStatusBar,
TextItem,
TextExt
} from '@jupyterlab/statusbar';
import { CommandRegistry } from '@phosphor/commands';
import { IDisposable } from '@phosphor/disposable';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
import { IStatusContext } from '../contexts';
// tslint:disable-next-line:variable-name
const KernelStatusComponent = (
props: KernelStatusComponent.IProps
): React.ReactElement<KernelStatusComponent.IProps> => {
return (
<TextItem
onClick={props.handleClick}
source={`${TextExt.titleCase(props.name)} | ${TextExt.titleCase(
props.status
)}`}
/>
);
};
namespace KernelStatusComponent {
export interface IProps {
handleClick: () => void;
name: string;
status: Kernel.Status;
}
}
class KernelStatus extends VDomRenderer<KernelStatus.Model>
implements IKernelStatus {
constructor(opts: KernelStatus.IOptions) {
super();
this._notebookTracker = opts.notebookTracker;
this._consoleTracker = opts.consoleTracker;
this._commands = opts.commands;
this._shell = opts.shell;
this._shell.currentChanged.connect(
this._onCurrentChanged,
this
);
this.model = new KernelStatus.Model(
this._getFocusedSession(this._shell.currentWidget)
);
this.addClass(interactiveItem);
}
readonly model: KernelStatus.Model;
render() {
if (this.model === null) {
return null;
} else {
return (
<KernelStatusComponent
status={this.model.status}
name={this.model.name}
handleClick={this._handleClick}
/>
);
}
}
dispose() {
super.dispose();
Signal.disconnectAll(this);
this._shell.currentChanged.disconnect(this._onCurrentChanged);
}
protected onUpdateRequest(msg: Message) {
this.model.session = this._getFocusedSession(this._shell.currentWidget);
super.onUpdateRequest(msg);
}
private _handleClick = () => {
// The kernel menu flavor of change kernel delegates
// based on the active widget, so use that.
this._commands.execute('kernelmenu:change');
};
private _onTitleChanged = () => {
const name = this._shell.currentWidget
? this._shell.currentWidget.title.label
: 'activity';
this.node.title = `Change active kernel for ${name}`;
};
private _getFocusedSession(val: Widget | null): IClientSession | null {
if (!val) {
return null;
} else {
if (this._notebookTracker.has(val)) {
return (val as NotebookPanel).session;
} else if (this._consoleTracker.has(val)) {
return (val as ConsolePanel).session;
} else {
return null;
}
}
}
private _onCurrentChanged(
shell: ApplicationShell,
change: ApplicationShell.IChangedArgs
): void {
if (this._current) {
this._current.title.changed.disconnect(this._onTitleChanged, this);
}
this._current = change.newValue;
this._current.title.changed.connect(
this._onTitleChanged,
this
);
const session = this._getFocusedSession(this._current);
this.model.session = session;
}
private _current: Widget | undefined;
private _notebookTracker: INotebookTracker;
private _consoleTracker: IConsoleTracker;
private _shell: ApplicationShell;
private _commands: CommandRegistry;
}
namespace KernelStatus {
export class Model extends VDomModel implements IKernelStatus.IModel {
constructor(session: IClientSession | null) {
super();
this.session = session;
}
get name() {
return this._kernelName;
}
get status() {
return this._kernelStatus;
}
get type() {
return this._session && this._session.type;
}
get session() {
return this._session;
}
set session(session: IClientSession | null) {
const oldSession = this._session;
if (oldSession !== null) {
oldSession.statusChanged.disconnect(this._onKernelStatusChanged);
oldSession.kernelChanged.disconnect(this._onKernelChanged);
}
const oldState = this._getAllState();
this._session = session;
if (this._session === null) {
this._kernelStatus = 'unknown';
this._kernelName = 'unknown';
} else {
this._kernelStatus = this._session.status;
this._kernelName = this._session.kernelDisplayName.toLowerCase();
this._session.statusChanged.connect(this._onKernelStatusChanged);
this._session.kernelChanged.connect(this._onKernelChanged);
}
this._triggerChange(oldState, this._getAllState());
}
private _onKernelStatusChanged = (
_session: IClientSession,
status: Kernel.Status
) => {
this._kernelStatus = status;
this.stateChanged.emit(void 0);
};
private _onKernelChanged = (
_session: IClientSession,
change: Session.IKernelChangedArgs
) => {
const oldState = this._getAllState();
const { newValue } = change;
if (newValue !== null) {
this._kernelStatus = newValue.status;
this._kernelName = newValue.model.name.toLowerCase();
} else {
this._kernelStatus = 'unknown';
this._kernelName = 'unknown';
}
this._triggerChange(oldState, this._getAllState());
};
private _getAllState(): [string, string] {
return [this._kernelName, this._kernelStatus];
}
private _triggerChange(
oldState: [string, string],
newState: [string, string]
) {
if (oldState[0] !== newState[0] || oldState[1] !== newState[1]) {
this.stateChanged.emit(void 0);
}
}
private _kernelName: string = 'unknown';
private _kernelStatus: Kernel.Status = 'unknown';
private _session: IClientSession | null = null;
}
export interface IOptions {
notebookTracker: INotebookTracker;
consoleTracker: IConsoleTracker;
shell: ApplicationShell;
commands: CommandRegistry;
}
}
export interface IKernelStatus extends IDisposable {
readonly model: IKernelStatus.IModel | null;
readonly modelChanged: ISignal<this, void>;
}
export namespace IKernelStatus {
export interface IModel {
readonly name: string;
readonly status: Kernel.Status;
readonly type: string | null;
readonly session: IClientSession | null;
}
}
export const kernelStatus: JupyterLabPlugin<void> = {
id: '@jupyterlab/statusbar:kernel-status',
autoStart: true,
requires: [IStatusBar, INotebookTracker, IConsoleTracker],
activate: (
app: JupyterLab,
statusBar: IStatusBar,
notebookTracker: INotebookTracker,
consoleTracker: IConsoleTracker
) => {
const item = new KernelStatus({
shell: app.shell,
notebookTracker,
consoleTracker,
commands: app.commands
});
statusBar.registerStatusItem('kernel-status-item', item, {
align: 'left',
rank: 1,
isActive: IStatusContext.delegateActive(app.shell, [
{ tracker: notebookTracker },
{ tracker: consoleTracker }
])
});
}
};