-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
kernelstatuses.tsx
296 lines (269 loc) · 8.1 KB
/
kernelstatuses.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Session } from '@jupyterlab/services';
import { TextItem } from '@jupyterlab/statusbar';
import {
ITranslator,
nullTranslator,
TranslationBundle
} from '@jupyterlab/translation';
import { VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
import { JSONArray, JSONExt } from '@lumino/coreutils';
import React, { KeyboardEvent } from 'react';
import { ISessionContext } from './sessioncontext';
/**
* Helper function to translate kernel statuses mapping by using
* input translator.
*
* @param translator - Language translator.
* @return The translated kernel status mapping.
*/
export function translateKernelStatuses(
translator?: ITranslator
): Record<ISessionContext.KernelDisplayStatus, string> {
translator = translator || nullTranslator;
const trans = translator.load('jupyterlab');
const translated: Record<ISessionContext.KernelDisplayStatus, string> = {
unknown: trans.__('Unknown'),
starting: trans.__('Starting'),
idle: trans.__('Idle'),
busy: trans.__('Busy'),
terminating: trans.__('Terminating'),
restarting: trans.__('Restarting'),
autorestarting: trans.__('Autorestarting'),
dead: trans.__('Dead'),
connected: trans.__('Connected'),
connecting: trans.__('Connecting'),
disconnected: trans.__('Disconnected'),
initializing: trans.__('Initializing'),
'': ''
};
return translated;
}
/**
* A pure functional component for rendering kernel status.
*/
function KernelStatusComponent(
props: KernelStatusComponent.IProps
): React.ReactElement<KernelStatusComponent.IProps> {
const translator = props.translator || nullTranslator;
const trans = translator.load('jupyterlab');
let statusText = '';
if (props.status) {
statusText = ` | ${props.status}`;
}
return (
<TextItem
onClick={props.handleClick}
onKeyDown={props.handleKeyDown}
source={`${props.kernelName}${statusText}`}
title={trans.__('Change kernel for %1', props.activityName)}
tabIndex={0}
/>
);
}
/**
* A namespace for KernelStatusComponent statics.
*/
namespace KernelStatusComponent {
/**
* Props for the kernel status component.
*/
export interface IProps {
/**
* A click handler for the kernel status component. By default
* we have it bring up the kernel change dialog.
*/
handleClick: () => void;
/**
* A key down handler for the kernel status component. By default
* we have it bring up the kernel change dialog.
*/
handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
/**
* The name the kernel.
*/
kernelName: string;
/**
* The name of the activity using the kernel.
*/
activityName: string;
/**
* The status of the kernel.
*/
status?: string;
/**
* The application language translator.
*/
translator?: ITranslator;
}
}
/**
* A VDomRenderer widget for displaying the status of a kernel.
*/
export class KernelStatus extends VDomRenderer<KernelStatus.Model> {
/**
* Construct the kernel status widget.
*/
constructor(opts: KernelStatus.IOptions, translator?: ITranslator) {
super(new KernelStatus.Model(translator));
this.translator = translator || nullTranslator;
this._handleClick = opts.onClick;
this._handleKeyDown = opts.onKeyDown;
this.addClass('jp-mod-highlighted');
}
/**
* Render the kernel status item.
*/
render(): JSX.Element | null {
if (this.model === null) {
return null;
} else {
return (
<KernelStatusComponent
status={this.model.status}
kernelName={this.model.kernelName}
activityName={this.model.activityName}
handleClick={this._handleClick}
handleKeyDown={this._handleKeyDown}
translator={this.translator}
/>
);
}
}
translator: ITranslator;
private _handleClick: () => void;
private _handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
}
/**
* A namespace for KernelStatus statics.
*/
export namespace KernelStatus {
/**
* A VDomModel for the kernel status indicator.
*/
export class Model extends VDomModel {
constructor(translator?: ITranslator) {
super();
translator = translator ?? nullTranslator;
this._trans = translator.load('jupyterlab');
this._statusNames = translateKernelStatuses(translator);
}
/**
* The name of the kernel.
*/
get kernelName(): string {
return this._kernelName;
}
/**
* The current status of the kernel.
*/
get status(): string | undefined {
return this._kernelStatus
? this._statusNames[this._kernelStatus]
: undefined;
}
/**
* A display name for the activity.
*/
get activityName(): string {
return this._activityName;
}
set activityName(val: string) {
const oldVal = this._activityName;
if (oldVal === val) {
return;
}
this._activityName = val;
this.stateChanged.emit();
}
/**
* The current client session associated with the kernel status indicator.
*/
get sessionContext(): ISessionContext | null {
return this._sessionContext;
}
set sessionContext(sessionContext: ISessionContext | null) {
this._sessionContext?.statusChanged.disconnect(
this._onKernelStatusChanged,
this
);
this._sessionContext?.connectionStatusChanged.disconnect(
this._onKernelStatusChanged,
this
);
this._sessionContext?.kernelChanged.disconnect(
this._onKernelChanged,
this
);
const oldState = this._getAllState();
this._sessionContext = sessionContext;
this._kernelStatus = sessionContext?.kernelDisplayStatus;
this._kernelName =
sessionContext?.kernelDisplayName ?? this._trans.__('No Kernel');
sessionContext?.statusChanged.connect(this._onKernelStatusChanged, this);
sessionContext?.connectionStatusChanged.connect(
this._onKernelStatusChanged,
this
);
sessionContext?.kernelChanged.connect(this._onKernelChanged, this);
this._triggerChange(oldState, this._getAllState());
}
/**
* React to changes to the kernel status.
*/
private _onKernelStatusChanged() {
this._kernelStatus = this._sessionContext?.kernelDisplayStatus;
this.stateChanged.emit(void 0);
}
/**
* React to changes in the kernel.
*/
private _onKernelChanged(
_sessionContext: ISessionContext,
change: Session.ISessionConnection.IKernelChangedArgs
) {
const oldState = this._getAllState();
// sync setting of status and display name
this._kernelStatus = this._sessionContext?.kernelDisplayStatus;
this._kernelName = _sessionContext.kernelDisplayName;
this._triggerChange(oldState, this._getAllState());
}
private _getAllState(): Private.State {
return [this._kernelName, this._kernelStatus, this._activityName];
}
private _triggerChange(oldState: Private.State, newState: Private.State) {
if (JSONExt.deepEqual(oldState as JSONArray, newState as JSONArray)) {
this.stateChanged.emit(void 0);
}
}
protected translation: ITranslator;
private _trans: TranslationBundle;
private _activityName: string = '';
private _kernelName: string = '';
private _kernelStatus: ISessionContext.KernelDisplayStatus | undefined = '';
private _sessionContext: ISessionContext | null = null;
private readonly _statusNames: Record<
ISessionContext.KernelDisplayStatus,
string
>;
}
/**
* Options for creating a KernelStatus object.
*/
export interface IOptions {
/**
* A click handler for the item. By default
* we launch a kernel selection dialog.
*/
onClick: () => void;
/**
* A key press handler for the item. By default
* we launch a kernel selection dialog.
*/
onKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
}
}
namespace Private {
export type State = [string, string | undefined, string];
}