-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
index.tsx
380 lines (332 loc) · 9.4 KB
/
index.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import * as React from 'react';
import { IIterator, toArray } from '@phosphor/algorithm';
import { ISignal, Signal } from '@phosphor/signaling';
import { ReactWidget, UseSignal } from '@jupyterlab/apputils';
import {
Dialog,
showDialog,
ToolbarButtonComponent
} from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import { ServiceManager, Session, TerminalSession } from '@jupyterlab/services';
import '../style/index.css';
/**
* The class name added to a running widget.
*/
const RUNNING_CLASS = 'jp-RunningSessions';
/**
* The class name added to a running widget header.
*/
const HEADER_CLASS = 'jp-RunningSessions-header';
/**
* The class name added to the running terminal sessions section.
*/
const SECTION_CLASS = 'jp-RunningSessions-section';
/**
* The class name added to the running sessions section header.
*/
const SECTION_HEADER_CLASS = 'jp-RunningSessions-sectionHeader';
/**
* The class name added to a section container.
*/
const CONTAINER_CLASS = 'jp-RunningSessions-sectionContainer';
/**
* The class name added to the running kernel sessions section list.
*/
const LIST_CLASS = 'jp-RunningSessions-sectionList';
/**
* The class name added to the running sessions items.
*/
const ITEM_CLASS = 'jp-RunningSessions-item';
/**
* The class name added to a running session item icon.
*/
const ITEM_ICON_CLASS = 'jp-RunningSessions-itemIcon';
/**
* The class name added to a running session item label.
*/
const ITEM_LABEL_CLASS = 'jp-RunningSessions-itemLabel';
/**
* The class name added to a running session item shutdown button.
*/
const SHUTDOWN_BUTTON_CLASS = 'jp-RunningSessions-itemShutdown';
/**
* The class name added to a notebook icon.
*/
const NOTEBOOK_ICON_CLASS = 'jp-mod-notebook';
/**
* The class name added to a console icon.
*/
const CONSOLE_ICON_CLASS = 'jp-mod-console';
/**
* The class name added to a file icon.
*/
const FILE_ICON_CLASS = 'jp-mod-file';
/**
* The class name added to a terminal icon.
*/
const TERMINAL_ICON_CLASS = 'jp-mod-terminal';
/**
* Properties for a session list displaying items of generic type `M`.
*/
type SessionProps<M> = {
/**
* A signal that tracks when the `open` is clicked on a session item.
*/
openRequested: Signal<RunningSessions, M>;
/**
* The session manager.
*/
manager: {
/**
* The function called when the shutdown all button is pressed.
*/
shutdownAll(): void;
/**
* A signal that should emit a new list of items whenever they are changed.
*/
runningChanged: ISignal<any, M[]>;
/**
* Returns a list the running models.
*/
running(): IIterator<M>;
};
/**
* The function called when the shutdown button is pressed on an item.
*/
shutdown: (model: M) => void;
/**
* The filter that is applied to the items from `runningChanged`.
*/
filterRunning?: (model: M) => boolean;
/**
* The name displayed to the user.
*/
name: string;
/**
* Returns the icon class for an item.
*/
iconClass: (model: M) => string;
/**
* Returns the label for an item.
*/
label: (model: M) => string;
/**
* Called to determine the `title` attribute for each item, which is revealed
* on hover.
*/
labelTitle?: (model: M) => string;
/**
* Flag that sets whether it sessions should be displayed.
*/
available: boolean;
};
function Item<M>(props: SessionProps<M> & { model: M }) {
const { model } = props;
return (
<li className={ITEM_CLASS}>
<span className={`${ITEM_ICON_CLASS} ${props.iconClass(model)}`} />
<span
className={ITEM_LABEL_CLASS}
title={props.labelTitle ? props.labelTitle(model) : ''}
onClick={() => props.openRequested.emit(model)}
>
{props.label(model)}
</span>
<button
className={`${SHUTDOWN_BUTTON_CLASS} jp-mod-styled`}
onClick={() => props.shutdown(model)}
>
SHUT DOWN
</button>
</li>
);
}
function ListView<M>(props: { models: M[] } & SessionProps<M>) {
const { models, ...rest } = props;
return (
<ul className={LIST_CLASS}>
{models.map((m, i) => (
<Item key={i} model={m} {...rest} />
))}
</ul>
);
}
function List<M>(props: SessionProps<M>) {
const initialModels = toArray(props.manager.running());
const filterRunning = props.filterRunning || (_ => true);
function render(models: Array<M>) {
return <ListView models={models.filter(filterRunning)} {...props} />;
}
if (!props.available) {
return render(initialModels);
}
return (
<UseSignal
signal={props.manager.runningChanged}
initialArgs={initialModels}
>
{(sender: any, args: Array<M>) => render(args)}
</UseSignal>
);
}
/**
* The Section component contains the shared look and feel for an interactive
* list of kernels and sessions.
*
* It is specialized for each based on it's props.
*/
function Section<M>(props: SessionProps<M>) {
function onShutdown() {
void showDialog({
title: `Shut Down All ${props.name} Sessions?`,
buttons: [
Dialog.cancelButton(),
Dialog.warnButton({ label: 'SHUT DOWN ALL' })
]
}).then(result => {
if (result.button.accept) {
props.manager.shutdownAll();
}
});
}
return (
<div className={SECTION_CLASS}>
{props.available && (
<>
<header className={SECTION_HEADER_CLASS}>
<h2>{props.name} Sessions</h2>
<ToolbarButtonComponent
tooltip={`Shut Down All ${props.name} Sessions…`}
iconClassName="jp-CloseIcon"
onClick={onShutdown}
/>
</header>
<div className={CONTAINER_CLASS}>
<List {...props} />
</div>
</>
)}
</div>
);
}
interface IRunningSessionsProps {
manager: ServiceManager.IManager;
sessionOpenRequested: Signal<RunningSessions, Session.IModel>;
terminalOpenRequested: Signal<RunningSessions, TerminalSession.IModel>;
}
function RunningSessionsComponent({
manager,
sessionOpenRequested,
terminalOpenRequested
}: IRunningSessionsProps) {
const terminalsAvailable = manager.terminals.isAvailable();
return (
<>
<div className={HEADER_CLASS}>
<ToolbarButtonComponent
tooltip="Refresh List"
iconClassName="jp-RefreshIcon"
onClick={() => {
if (terminalsAvailable) {
void manager.terminals.refreshRunning();
}
void manager.sessions.refreshRunning();
}}
/>
</div>
<Section
openRequested={terminalOpenRequested}
manager={manager.terminals}
name="Terminal"
iconClass={() => `${ITEM_ICON_CLASS} ${TERMINAL_ICON_CLASS}`}
label={m => `terminals/${m.name}`}
available={terminalsAvailable}
shutdown={m => manager.terminals.shutdown(m.name)}
/>
<Section
openRequested={sessionOpenRequested}
manager={manager.sessions}
filterRunning={m =>
!!((m.name || PathExt.basename(m.path)).indexOf('.') !== -1 || m.name)
}
name="Kernel"
iconClass={m => {
if ((m.name || PathExt.basename(m.path)).indexOf('.ipynb') !== -1) {
return NOTEBOOK_ICON_CLASS;
} else if (m.type.toLowerCase() === 'console') {
return CONSOLE_ICON_CLASS;
}
return FILE_ICON_CLASS;
}}
label={m => m.name || PathExt.basename(m.path)}
available={true}
labelTitle={m => {
let kernelName = m.kernel.name;
if (manager.specs) {
const spec = manager.specs.kernelspecs[kernelName];
kernelName = spec ? spec.display_name : 'unknown';
}
return `Path: ${m.path}\nKernel: ${kernelName}`;
}}
shutdown={m => manager.sessions.shutdown(m.id)}
/>
</>
);
}
/**
* A class that exposes the running terminal and kernel sessions.
*/
export class RunningSessions extends ReactWidget {
/**
* Construct a new running widget.
*/
constructor(options: RunningSessions.IOptions) {
super();
this.options = options;
// this can't be in the react element, because then it would be too nested
this.addClass(RUNNING_CLASS);
}
protected render() {
return (
<RunningSessionsComponent
manager={this.options.manager}
sessionOpenRequested={this._sessionOpenRequested}
terminalOpenRequested={this._terminalOpenRequested}
/>
);
}
/**
* A signal emitted when a kernel session open is requested.
*/
get sessionOpenRequested(): ISignal<this, Session.IModel> {
return this._sessionOpenRequested;
}
/**
* A signal emitted when a terminal session open is requested.
*/
get terminalOpenRequested(): ISignal<this, TerminalSession.IModel> {
return this._terminalOpenRequested;
}
private _sessionOpenRequested = new Signal<this, Session.IModel>(this);
private _terminalOpenRequested = new Signal<this, TerminalSession.IModel>(
this
);
private options: RunningSessions.IOptions;
}
/**
* The namespace for the `RunningSessions` class statics.
*/
export namespace RunningSessions {
/**
* An options object for creating a running sessions widget.
*/
export interface IOptions {
/**
* A service manager instance.
*/
manager: ServiceManager.IManager;
}
}