@jupyterlab/apputils 0.19.1-alpha.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +34 -0
- package/lib/clientsession.d.ts +443 -443
- package/lib/clientsession.js +883 -883
- package/lib/clipboard.d.ts +33 -33
- package/lib/clipboard.js +88 -88
- package/lib/commandlinker.d.ts +121 -121
- package/lib/commandlinker.js +175 -175
- package/lib/commandpalette.d.ts +33 -33
- package/lib/commandpalette.js +12 -12
- package/lib/dialog.d.ts +357 -357
- package/lib/dialog.js +540 -540
- package/lib/domutils.d.ts +13 -13
- package/lib/domutils.js +28 -28
- package/lib/hoverbox.d.ts +82 -82
- package/lib/hoverbox.js +101 -101
- package/lib/iframe.d.ts +14 -14
- package/lib/iframe.js +45 -45
- package/lib/index.d.ts +19 -19
- package/lib/index.js +26 -26
- package/lib/instancetracker.d.ts +278 -278
- package/lib/instancetracker.js +318 -318
- package/lib/mainareawidget.d.ts +108 -108
- package/lib/mainareawidget.js +184 -184
- package/lib/sanitizer.d.ts +44 -44
- package/lib/sanitizer.js +913 -913
- package/lib/spinner.d.ts +15 -15
- package/lib/spinner.js +30 -30
- package/lib/splash.d.ts +19 -19
- package/lib/splash.js +10 -10
- package/lib/styling.d.ts +27 -27
- package/lib/styling.js +92 -92
- package/lib/thememanager.d.ts +141 -141
- package/lib/thememanager.js +238 -238
- package/lib/toolbar.d.ts +178 -178
- package/lib/toolbar.js +515 -515
- package/lib/vdom.d.ts +90 -90
- package/lib/vdom.js +130 -130
- package/lib/windowresolver.d.ts +37 -37
- package/lib/windowresolver.js +173 -173
- package/package.json +9 -6
package/lib/clientsession.js
CHANGED
|
@@ -1,883 +1,883 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright (c) Jupyter Development Team.
|
|
3
|
-
// Distributed under the terms of the Modified BSD License.
|
|
4
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
13
|
-
if (mod && mod.__esModule) return mod;
|
|
14
|
-
var result = {};
|
|
15
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
16
|
-
result["default"] = mod;
|
|
17
|
-
return result;
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
const coreutils_1 = require("@jupyterlab/coreutils");
|
|
21
|
-
const coreutils_2 = require("@phosphor/coreutils");
|
|
22
|
-
const algorithm_1 = require("@phosphor/algorithm");
|
|
23
|
-
const coreutils_3 = require("@phosphor/coreutils");
|
|
24
|
-
const signaling_1 = require("@phosphor/signaling");
|
|
25
|
-
const widgets_1 = require("@phosphor/widgets");
|
|
26
|
-
const React = __importStar(require("react"));
|
|
27
|
-
const dialog_1 = require("./dialog");
|
|
28
|
-
/**
|
|
29
|
-
* The default implementation of client session object.
|
|
30
|
-
*/
|
|
31
|
-
class ClientSession {
|
|
32
|
-
/**
|
|
33
|
-
* Construct a new client session.
|
|
34
|
-
*/
|
|
35
|
-
constructor(options) {
|
|
36
|
-
this._path = '';
|
|
37
|
-
this._name = '';
|
|
38
|
-
this._type = '';
|
|
39
|
-
this._prevKernelName = '';
|
|
40
|
-
this._isDisposed = false;
|
|
41
|
-
this._session = null;
|
|
42
|
-
this._ready = new coreutils_3.PromiseDelegate();
|
|
43
|
-
this._initializing = false;
|
|
44
|
-
this._isReady = false;
|
|
45
|
-
this._terminated = new signaling_1.Signal(this);
|
|
46
|
-
this._kernelChanged = new signaling_1.Signal(this);
|
|
47
|
-
this._statusChanged = new signaling_1.Signal(this);
|
|
48
|
-
this._iopubMessage = new signaling_1.Signal(this);
|
|
49
|
-
this._unhandledMessage = new signaling_1.Signal(this);
|
|
50
|
-
this._propertyChanged = new signaling_1.Signal(this);
|
|
51
|
-
this._dialog = null;
|
|
52
|
-
this._busyDisposable = null;
|
|
53
|
-
this.manager = options.manager;
|
|
54
|
-
this._path = options.path || coreutils_2.UUID.uuid4();
|
|
55
|
-
this._type = options.type || '';
|
|
56
|
-
this._name = options.name || '';
|
|
57
|
-
this._setBusy = options.setBusy;
|
|
58
|
-
this._kernelPreference = options.kernelPreference || {};
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* A signal emitted when the session is shut down.
|
|
62
|
-
*/
|
|
63
|
-
get terminated() {
|
|
64
|
-
return this._terminated;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* A signal emitted when the kernel changes.
|
|
68
|
-
*/
|
|
69
|
-
get kernelChanged() {
|
|
70
|
-
return this._kernelChanged;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* A signal emitted when the status changes.
|
|
74
|
-
*/
|
|
75
|
-
get statusChanged() {
|
|
76
|
-
return this._statusChanged;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* A signal emitted for iopub kernel messages.
|
|
80
|
-
*/
|
|
81
|
-
get iopubMessage() {
|
|
82
|
-
return this._iopubMessage;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* A signal emitted for an unhandled kernel message.
|
|
86
|
-
*/
|
|
87
|
-
get unhandledMessage() {
|
|
88
|
-
return this._unhandledMessage;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* A signal emitted when a session property changes.
|
|
92
|
-
*/
|
|
93
|
-
get propertyChanged() {
|
|
94
|
-
return this._propertyChanged;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* The current kernel of the session.
|
|
98
|
-
*/
|
|
99
|
-
get kernel() {
|
|
100
|
-
return this._session ? this._session.kernel : null;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* The current path of the session.
|
|
104
|
-
*/
|
|
105
|
-
get path() {
|
|
106
|
-
return this._path;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* The current name of the session.
|
|
110
|
-
*/
|
|
111
|
-
get name() {
|
|
112
|
-
return this._name;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* The type of the client session.
|
|
116
|
-
*/
|
|
117
|
-
get type() {
|
|
118
|
-
return this._type;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* The kernel preference of the session.
|
|
122
|
-
*/
|
|
123
|
-
get kernelPreference() {
|
|
124
|
-
return this._kernelPreference;
|
|
125
|
-
}
|
|
126
|
-
set kernelPreference(value) {
|
|
127
|
-
this._kernelPreference = value;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* The current status of the session.
|
|
131
|
-
*/
|
|
132
|
-
get status() {
|
|
133
|
-
if (!this.isReady) {
|
|
134
|
-
return 'starting';
|
|
135
|
-
}
|
|
136
|
-
return this._session ? this._session.status : 'dead';
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Whether the session is ready.
|
|
140
|
-
*/
|
|
141
|
-
get isReady() {
|
|
142
|
-
return this._isReady;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* A promise that is fulfilled when the session is ready.
|
|
146
|
-
*/
|
|
147
|
-
get ready() {
|
|
148
|
-
return this._ready.promise;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* The display name of the current kernel.
|
|
152
|
-
*/
|
|
153
|
-
get kernelDisplayName() {
|
|
154
|
-
let kernel = this.kernel;
|
|
155
|
-
if (!kernel) {
|
|
156
|
-
return 'No Kernel!';
|
|
157
|
-
}
|
|
158
|
-
let specs = this.manager.specs;
|
|
159
|
-
if (!specs) {
|
|
160
|
-
return 'Unknown!';
|
|
161
|
-
}
|
|
162
|
-
let spec = specs.kernelspecs[kernel.name];
|
|
163
|
-
return spec ? spec.display_name : kernel.name;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Test whether the context is disposed.
|
|
167
|
-
*/
|
|
168
|
-
get isDisposed() {
|
|
169
|
-
return this._isDisposed;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Dispose of the resources held by the context.
|
|
173
|
-
*/
|
|
174
|
-
dispose() {
|
|
175
|
-
if (this._isDisposed) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
this._isDisposed = true;
|
|
179
|
-
if (this._session) {
|
|
180
|
-
this._session = null;
|
|
181
|
-
}
|
|
182
|
-
if (this._dialog) {
|
|
183
|
-
this._dialog.dispose();
|
|
184
|
-
}
|
|
185
|
-
signaling_1.Signal.clearData(this);
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Change the current kernel associated with the document.
|
|
189
|
-
*/
|
|
190
|
-
changeKernel(options) {
|
|
191
|
-
return this.initialize().then(() => {
|
|
192
|
-
if (this.isDisposed) {
|
|
193
|
-
return Promise.reject('Disposed');
|
|
194
|
-
}
|
|
195
|
-
return this._changeKernel(options);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Select a kernel for the session.
|
|
200
|
-
*/
|
|
201
|
-
selectKernel() {
|
|
202
|
-
return this.initialize().then(() => {
|
|
203
|
-
if (this.isDisposed) {
|
|
204
|
-
return Promise.reject('Disposed');
|
|
205
|
-
}
|
|
206
|
-
return this._selectKernel(true);
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Kill the kernel and shutdown the session.
|
|
211
|
-
*
|
|
212
|
-
* @returns A promise that resolves when the session is shut down.
|
|
213
|
-
*/
|
|
214
|
-
shutdown() {
|
|
215
|
-
const session = this._session;
|
|
216
|
-
if (this.isDisposed || !session) {
|
|
217
|
-
return Promise.resolve();
|
|
218
|
-
}
|
|
219
|
-
this._session = null;
|
|
220
|
-
return session.shutdown();
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Restart the session.
|
|
224
|
-
*
|
|
225
|
-
* @returns A promise that resolves with whether the kernel has restarted.
|
|
226
|
-
*
|
|
227
|
-
* #### Notes
|
|
228
|
-
* If there is a running kernel, present a dialog.
|
|
229
|
-
* If there is no kernel, we start a kernel with the last run
|
|
230
|
-
* kernel name and resolves with `true`.
|
|
231
|
-
*/
|
|
232
|
-
restart() {
|
|
233
|
-
return this.initialize().then(() => {
|
|
234
|
-
if (this.isDisposed) {
|
|
235
|
-
return Promise.reject('session already disposed');
|
|
236
|
-
}
|
|
237
|
-
let kernel = this.kernel;
|
|
238
|
-
if (!kernel) {
|
|
239
|
-
if (this._prevKernelName) {
|
|
240
|
-
return this.changeKernel({ name: this._prevKernelName }).then(() => true);
|
|
241
|
-
}
|
|
242
|
-
// Bail if there is no previous kernel to start.
|
|
243
|
-
return Promise.reject('No kernel to restart');
|
|
244
|
-
}
|
|
245
|
-
return ClientSession.restartKernel(kernel);
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Change the session path.
|
|
250
|
-
*
|
|
251
|
-
* @param path - The new session path.
|
|
252
|
-
*
|
|
253
|
-
* @returns A promise that resolves when the session has renamed.
|
|
254
|
-
*
|
|
255
|
-
* #### Notes
|
|
256
|
-
* This uses the Jupyter REST API, and the response is validated.
|
|
257
|
-
* The promise is fulfilled on a valid response and rejected otherwise.
|
|
258
|
-
*/
|
|
259
|
-
setPath(path) {
|
|
260
|
-
if (this.isDisposed || this._path === path) {
|
|
261
|
-
return Promise.resolve();
|
|
262
|
-
}
|
|
263
|
-
this._path = path;
|
|
264
|
-
if (this._session) {
|
|
265
|
-
return this._session.setPath(path);
|
|
266
|
-
}
|
|
267
|
-
this._propertyChanged.emit('path');
|
|
268
|
-
return Promise.resolve();
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Change the session name.
|
|
272
|
-
*/
|
|
273
|
-
setName(name) {
|
|
274
|
-
if (this.isDisposed || this._name === name) {
|
|
275
|
-
return Promise.resolve();
|
|
276
|
-
}
|
|
277
|
-
this._name = name;
|
|
278
|
-
if (this._session) {
|
|
279
|
-
return this._session.setName(name);
|
|
280
|
-
}
|
|
281
|
-
this._propertyChanged.emit('name');
|
|
282
|
-
return Promise.resolve();
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Change the session type.
|
|
286
|
-
*/
|
|
287
|
-
setType(type) {
|
|
288
|
-
if (this.isDisposed || this._type === type) {
|
|
289
|
-
return Promise.resolve();
|
|
290
|
-
}
|
|
291
|
-
this._type = type;
|
|
292
|
-
if (this._session) {
|
|
293
|
-
return this._session.setType(name);
|
|
294
|
-
}
|
|
295
|
-
this._propertyChanged.emit('type');
|
|
296
|
-
return Promise.resolve();
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Initialize the session.
|
|
300
|
-
*
|
|
301
|
-
* #### Notes
|
|
302
|
-
* If a server session exists on the current path, we will connect to it.
|
|
303
|
-
* If preferences include disabling `canStart` or `shouldStart`, no
|
|
304
|
-
* server session will be started.
|
|
305
|
-
* If a kernel id is given, we attempt to start a session with that id.
|
|
306
|
-
* If a default kernel is available, we connect to it.
|
|
307
|
-
* Otherwise we ask the user to select a kernel.
|
|
308
|
-
*/
|
|
309
|
-
initialize() {
|
|
310
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
-
if (this._initializing || this._isReady) {
|
|
312
|
-
return this._ready.promise;
|
|
313
|
-
}
|
|
314
|
-
this._initializing = true;
|
|
315
|
-
let manager = this.manager;
|
|
316
|
-
yield manager.ready;
|
|
317
|
-
let model = algorithm_1.find(manager.running(), item => {
|
|
318
|
-
return item.path === this._path;
|
|
319
|
-
});
|
|
320
|
-
if (model) {
|
|
321
|
-
try {
|
|
322
|
-
let session = manager.connectTo(model);
|
|
323
|
-
this._handleNewSession(session);
|
|
324
|
-
}
|
|
325
|
-
catch (err) {
|
|
326
|
-
this._handleSessionError(err);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
yield this._startIfNecessary();
|
|
330
|
-
this._isReady = true;
|
|
331
|
-
this._ready.resolve(undefined);
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Start the session if necessary.
|
|
336
|
-
*/
|
|
337
|
-
_startIfNecessary() {
|
|
338
|
-
let preference = this.kernelPreference;
|
|
339
|
-
if (this.isDisposed ||
|
|
340
|
-
this.kernel ||
|
|
341
|
-
preference.shouldStart === false ||
|
|
342
|
-
preference.canStart === false) {
|
|
343
|
-
return Promise.resolve();
|
|
344
|
-
}
|
|
345
|
-
// Try to use an existing kernel.
|
|
346
|
-
if (preference.id) {
|
|
347
|
-
return this._changeKernel({ id: preference.id })
|
|
348
|
-
.then(() => undefined)
|
|
349
|
-
.catch(() => this._selectKernel(false));
|
|
350
|
-
}
|
|
351
|
-
let name = ClientSession.getDefaultKernel({
|
|
352
|
-
specs: this.manager.specs,
|
|
353
|
-
sessions: this.manager.running(),
|
|
354
|
-
preference
|
|
355
|
-
});
|
|
356
|
-
if (name) {
|
|
357
|
-
return this._changeKernel({ name })
|
|
358
|
-
.then(() => undefined)
|
|
359
|
-
.catch(() => this._selectKernel(false));
|
|
360
|
-
}
|
|
361
|
-
return this._selectKernel(false);
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Change the kernel.
|
|
365
|
-
*/
|
|
366
|
-
_changeKernel(options) {
|
|
367
|
-
if (this.isDisposed) {
|
|
368
|
-
return Promise.reject('Disposed');
|
|
369
|
-
}
|
|
370
|
-
let session = this._session;
|
|
371
|
-
if (session) {
|
|
372
|
-
return session.changeKernel(options);
|
|
373
|
-
}
|
|
374
|
-
else {
|
|
375
|
-
return this._startSession(options);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Select a kernel.
|
|
380
|
-
*
|
|
381
|
-
* @param cancelable: whether the dialog should have a cancel button.
|
|
382
|
-
*/
|
|
383
|
-
_selectKernel(cancelable) {
|
|
384
|
-
if (this.isDisposed) {
|
|
385
|
-
return Promise.resolve();
|
|
386
|
-
}
|
|
387
|
-
const buttons = cancelable
|
|
388
|
-
? [dialog_1.Dialog.cancelButton(), dialog_1.Dialog.okButton({ label: 'SELECT' })]
|
|
389
|
-
: [dialog_1.Dialog.okButton({ label: 'SELECT' })];
|
|
390
|
-
let dialog = (this._dialog = new dialog_1.Dialog({
|
|
391
|
-
title: 'Select Kernel',
|
|
392
|
-
body: new Private.KernelSelector(this),
|
|
393
|
-
buttons
|
|
394
|
-
}));
|
|
395
|
-
return dialog
|
|
396
|
-
.launch()
|
|
397
|
-
.then(result => {
|
|
398
|
-
if (this.isDisposed || !result.button.accept) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
let model = result.value;
|
|
402
|
-
if (model === null && this._session) {
|
|
403
|
-
return this.shutdown().then(() => {
|
|
404
|
-
this._kernelChanged.emit({ oldValue: null, newValue: null });
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
if (model) {
|
|
408
|
-
return this._changeKernel(model).then(() => undefined);
|
|
409
|
-
}
|
|
410
|
-
})
|
|
411
|
-
.then(() => {
|
|
412
|
-
this._dialog = null;
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Start a session and set up its signals.
|
|
417
|
-
*/
|
|
418
|
-
_startSession(model) {
|
|
419
|
-
if (this.isDisposed) {
|
|
420
|
-
return Promise.reject('Session is disposed.');
|
|
421
|
-
}
|
|
422
|
-
return this.manager
|
|
423
|
-
.startNew({
|
|
424
|
-
path: this._path,
|
|
425
|
-
type: this._type,
|
|
426
|
-
name: this._name,
|
|
427
|
-
kernelName: model ? model.name : undefined,
|
|
428
|
-
kernelId: model ? model.id : undefined
|
|
429
|
-
})
|
|
430
|
-
.then(session => {
|
|
431
|
-
return this._handleNewSession(session);
|
|
432
|
-
})
|
|
433
|
-
.catch(err => {
|
|
434
|
-
this._handleSessionError(err);
|
|
435
|
-
return Promise.reject(err);
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
/**
|
|
439
|
-
* Handle a new session object.
|
|
440
|
-
*/
|
|
441
|
-
_handleNewSession(session) {
|
|
442
|
-
if (this.isDisposed) {
|
|
443
|
-
throw Error('Disposed');
|
|
444
|
-
}
|
|
445
|
-
if (this._session) {
|
|
446
|
-
this._session.dispose();
|
|
447
|
-
}
|
|
448
|
-
this._session = session;
|
|
449
|
-
if (session.path !== this._path) {
|
|
450
|
-
this._path = session.path;
|
|
451
|
-
this._propertyChanged.emit('path');
|
|
452
|
-
}
|
|
453
|
-
if (session.name !== this._name) {
|
|
454
|
-
this._name = session.name;
|
|
455
|
-
this._propertyChanged.emit('name');
|
|
456
|
-
}
|
|
457
|
-
if (session.type !== this._type) {
|
|
458
|
-
this._type = session.type;
|
|
459
|
-
this._propertyChanged.emit('type');
|
|
460
|
-
}
|
|
461
|
-
session.terminated.connect(this._onTerminated, this);
|
|
462
|
-
session.propertyChanged.connect(this._onPropertyChanged, this);
|
|
463
|
-
session.kernelChanged.connect(this._onKernelChanged, this);
|
|
464
|
-
session.statusChanged.connect(this._onStatusChanged, this);
|
|
465
|
-
session.iopubMessage.connect(this._onIopubMessage, this);
|
|
466
|
-
session.unhandledMessage.connect(this._onUnhandledMessage, this);
|
|
467
|
-
this._prevKernelName = session.kernel.name;
|
|
468
|
-
// The session kernel was disposed above when the session was disposed, so
|
|
469
|
-
// the oldValue should be null.
|
|
470
|
-
this._kernelChanged.emit({ oldValue: null, newValue: session.kernel });
|
|
471
|
-
return session.kernel;
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Handle an error in session startup.
|
|
475
|
-
*/
|
|
476
|
-
_handleSessionError(err) {
|
|
477
|
-
return err.response
|
|
478
|
-
.text()
|
|
479
|
-
.then(text => {
|
|
480
|
-
let message = err.message;
|
|
481
|
-
try {
|
|
482
|
-
message = JSON.parse(text)['traceback'];
|
|
483
|
-
}
|
|
484
|
-
catch (err) {
|
|
485
|
-
// no-op
|
|
486
|
-
}
|
|
487
|
-
let dialog = (this._dialog = new dialog_1.Dialog({
|
|
488
|
-
title: 'Error Starting Kernel',
|
|
489
|
-
body: React.createElement("pre", null, message),
|
|
490
|
-
buttons: [dialog_1.Dialog.okButton()]
|
|
491
|
-
}));
|
|
492
|
-
return dialog.launch();
|
|
493
|
-
})
|
|
494
|
-
.then(() => {
|
|
495
|
-
this._dialog = null;
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Handle a session termination.
|
|
500
|
-
*/
|
|
501
|
-
_onTerminated() {
|
|
502
|
-
let kernel = this.kernel;
|
|
503
|
-
if (this._session) {
|
|
504
|
-
this._session.dispose();
|
|
505
|
-
}
|
|
506
|
-
this._session = null;
|
|
507
|
-
this._terminated.emit(undefined);
|
|
508
|
-
if (kernel) {
|
|
509
|
-
this._kernelChanged.emit({ oldValue: null, newValue: null });
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
/**
|
|
513
|
-
* Handle a change to a session property.
|
|
514
|
-
*/
|
|
515
|
-
_onPropertyChanged(sender, property) {
|
|
516
|
-
switch (property) {
|
|
517
|
-
case 'path':
|
|
518
|
-
this._path = sender.path;
|
|
519
|
-
break;
|
|
520
|
-
case 'name':
|
|
521
|
-
this._name = sender.name;
|
|
522
|
-
break;
|
|
523
|
-
default:
|
|
524
|
-
this._type = sender.type;
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
this._propertyChanged.emit(property);
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
* Handle a change to the kernel.
|
|
531
|
-
*/
|
|
532
|
-
_onKernelChanged(sender, args) {
|
|
533
|
-
this._kernelChanged.emit(args);
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Handle a change to the session status.
|
|
537
|
-
*/
|
|
538
|
-
_onStatusChanged() {
|
|
539
|
-
// Set that this kernel is busy, if we haven't already
|
|
540
|
-
// If we have already, and now we aren't busy, dispose
|
|
541
|
-
// of the busy disposable.
|
|
542
|
-
if (this._setBusy) {
|
|
543
|
-
if (this.status === 'busy') {
|
|
544
|
-
if (!this._busyDisposable) {
|
|
545
|
-
this._busyDisposable = this._setBusy();
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
else {
|
|
549
|
-
if (this._busyDisposable) {
|
|
550
|
-
this._busyDisposable.dispose();
|
|
551
|
-
this._busyDisposable = null;
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
this._statusChanged.emit(this.status);
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* Handle an iopub message.
|
|
559
|
-
*/
|
|
560
|
-
_onIopubMessage(sender, message) {
|
|
561
|
-
this._iopubMessage.emit(message);
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* Handle an unhandled message.
|
|
565
|
-
*/
|
|
566
|
-
_onUnhandledMessage(sender, message) {
|
|
567
|
-
this._unhandledMessage.emit(message);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
exports.ClientSession = ClientSession;
|
|
571
|
-
/**
|
|
572
|
-
* A namespace for `ClientSession` statics.
|
|
573
|
-
*/
|
|
574
|
-
(function (ClientSession) {
|
|
575
|
-
/**
|
|
576
|
-
* Restart a kernel if the user accepts the risk.
|
|
577
|
-
*
|
|
578
|
-
* Returns a promise resolving with whether the kernel was restarted.
|
|
579
|
-
*/
|
|
580
|
-
function restartKernel(kernel) {
|
|
581
|
-
let restartBtn = dialog_1.Dialog.warnButton({ label: 'RESTART ' });
|
|
582
|
-
return dialog_1.showDialog({
|
|
583
|
-
title: 'Restart Kernel?',
|
|
584
|
-
body: 'Do you want to restart the current kernel? All variables will be lost.',
|
|
585
|
-
buttons: [dialog_1.Dialog.cancelButton(), restartBtn]
|
|
586
|
-
}).then(result => {
|
|
587
|
-
if (kernel.isDisposed) {
|
|
588
|
-
return Promise.resolve(false);
|
|
589
|
-
}
|
|
590
|
-
if (result.button.accept) {
|
|
591
|
-
return kernel.restart().then(() => {
|
|
592
|
-
return true;
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
|
-
return false;
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
|
-
ClientSession.restartKernel = restartKernel;
|
|
599
|
-
/**
|
|
600
|
-
* Get the default kernel name given select options.
|
|
601
|
-
*/
|
|
602
|
-
function getDefaultKernel(options) {
|
|
603
|
-
return Private.getDefaultKernel(options);
|
|
604
|
-
}
|
|
605
|
-
ClientSession.getDefaultKernel = getDefaultKernel;
|
|
606
|
-
/**
|
|
607
|
-
* Populate a kernel dropdown list.
|
|
608
|
-
*
|
|
609
|
-
* @param node - The node to populate.
|
|
610
|
-
*
|
|
611
|
-
* @param options - The options used to populate the kernels.
|
|
612
|
-
*
|
|
613
|
-
* #### Notes
|
|
614
|
-
* Populates the list with separated sections:
|
|
615
|
-
* - Kernels matching the preferred language (display names).
|
|
616
|
-
* - "None" signifying no kernel.
|
|
617
|
-
* - The remaining kernels.
|
|
618
|
-
* - Sessions matching the preferred language (file names).
|
|
619
|
-
* - The remaining sessions.
|
|
620
|
-
* If no preferred language is given or no kernels are found using
|
|
621
|
-
* the preferred language, the default kernel is used in the first
|
|
622
|
-
* section. Kernels are sorted by display name. Sessions display the
|
|
623
|
-
* base name of the file with an ellipsis overflow and a tooltip with
|
|
624
|
-
* the explicit session information.
|
|
625
|
-
*/
|
|
626
|
-
function populateKernelSelect(node, options) {
|
|
627
|
-
return Private.populateKernelSelect(node, options);
|
|
628
|
-
}
|
|
629
|
-
ClientSession.populateKernelSelect = populateKernelSelect;
|
|
630
|
-
})(ClientSession = exports.ClientSession || (exports.ClientSession = {}));
|
|
631
|
-
/**
|
|
632
|
-
* The namespace for module private data.
|
|
633
|
-
*/
|
|
634
|
-
var Private;
|
|
635
|
-
(function (Private) {
|
|
636
|
-
/**
|
|
637
|
-
* A widget that provides a kernel selection.
|
|
638
|
-
*/
|
|
639
|
-
class KernelSelector extends widgets_1.Widget {
|
|
640
|
-
/**
|
|
641
|
-
* Create a new kernel selector widget.
|
|
642
|
-
*/
|
|
643
|
-
constructor(session) {
|
|
644
|
-
super({ node: createSelectorNode(session) });
|
|
645
|
-
}
|
|
646
|
-
/**
|
|
647
|
-
* Get the value of the kernel selector widget.
|
|
648
|
-
*/
|
|
649
|
-
getValue() {
|
|
650
|
-
let selector = this.node.querySelector('select');
|
|
651
|
-
return JSON.parse(selector.value);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
Private.KernelSelector = KernelSelector;
|
|
655
|
-
/**
|
|
656
|
-
* Create a node for a kernel selector widget.
|
|
657
|
-
*/
|
|
658
|
-
function createSelectorNode(session) {
|
|
659
|
-
// Create the dialog body.
|
|
660
|
-
let body = document.createElement('div');
|
|
661
|
-
let text = document.createElement('label');
|
|
662
|
-
text.innerHTML = `Select kernel for: "${session.name}"`;
|
|
663
|
-
body.appendChild(text);
|
|
664
|
-
let options = getKernelSearch(session);
|
|
665
|
-
let selector = document.createElement('select');
|
|
666
|
-
ClientSession.populateKernelSelect(selector, options);
|
|
667
|
-
body.appendChild(selector);
|
|
668
|
-
return body;
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Get the default kernel name given select options.
|
|
672
|
-
*/
|
|
673
|
-
function getDefaultKernel(options) {
|
|
674
|
-
let { specs, preference } = options;
|
|
675
|
-
let { name, language, shouldStart, canStart, autoStartDefault } = preference;
|
|
676
|
-
if (!specs || shouldStart === false || canStart === false) {
|
|
677
|
-
return null;
|
|
678
|
-
}
|
|
679
|
-
let defaultName = autoStartDefault ? specs.default : null;
|
|
680
|
-
if (!name && !language) {
|
|
681
|
-
return defaultName;
|
|
682
|
-
}
|
|
683
|
-
// Look for an exact match of a spec name.
|
|
684
|
-
for (let specName in specs.kernelspecs) {
|
|
685
|
-
if (specName === name) {
|
|
686
|
-
return name;
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
// Bail if there is no language.
|
|
690
|
-
if (!language) {
|
|
691
|
-
return defaultName;
|
|
692
|
-
}
|
|
693
|
-
// Check for a single kernel matching the language.
|
|
694
|
-
let matches = [];
|
|
695
|
-
for (let specName in specs.kernelspecs) {
|
|
696
|
-
let kernelLanguage = specs.kernelspecs[specName].language;
|
|
697
|
-
if (language === kernelLanguage) {
|
|
698
|
-
matches.push(specName);
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
if (matches.length === 1) {
|
|
702
|
-
let specName = matches[0];
|
|
703
|
-
console.log('No exact match found for ' +
|
|
704
|
-
specName +
|
|
705
|
-
', using kernel ' +
|
|
706
|
-
specName +
|
|
707
|
-
' that matches ' +
|
|
708
|
-
'language=' +
|
|
709
|
-
language);
|
|
710
|
-
return specName;
|
|
711
|
-
}
|
|
712
|
-
// No matches found.
|
|
713
|
-
return defaultName;
|
|
714
|
-
}
|
|
715
|
-
Private.getDefaultKernel = getDefaultKernel;
|
|
716
|
-
/**
|
|
717
|
-
* Populate a kernel select node for the session.
|
|
718
|
-
*/
|
|
719
|
-
function populateKernelSelect(node, options) {
|
|
720
|
-
while (node.firstChild) {
|
|
721
|
-
node.removeChild(node.firstChild);
|
|
722
|
-
}
|
|
723
|
-
let { preference, sessions, specs } = options;
|
|
724
|
-
let { name, id, language, canStart, shouldStart } = preference;
|
|
725
|
-
if (!specs || canStart === false) {
|
|
726
|
-
node.appendChild(optionForNone());
|
|
727
|
-
node.value = 'null';
|
|
728
|
-
node.disabled = true;
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
node.disabled = false;
|
|
732
|
-
// Create mappings of display names and languages for kernel name.
|
|
733
|
-
let displayNames = Object.create(null);
|
|
734
|
-
let languages = Object.create(null);
|
|
735
|
-
for (let name in specs.kernelspecs) {
|
|
736
|
-
let spec = specs.kernelspecs[name];
|
|
737
|
-
displayNames[name] = spec.display_name;
|
|
738
|
-
languages[name] = spec.language;
|
|
739
|
-
}
|
|
740
|
-
// Handle a kernel by name.
|
|
741
|
-
let names = [];
|
|
742
|
-
if (name && name in specs.kernelspecs) {
|
|
743
|
-
names.push(name);
|
|
744
|
-
}
|
|
745
|
-
// Then look by language.
|
|
746
|
-
if (language) {
|
|
747
|
-
for (let specName in specs.kernelspecs) {
|
|
748
|
-
if (name !== specName && languages[specName] === language) {
|
|
749
|
-
names.push(specName);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
// Use the default kernel if no kernels were found.
|
|
754
|
-
if (!names.length) {
|
|
755
|
-
names.push(specs.default);
|
|
756
|
-
}
|
|
757
|
-
// Handle a preferred kernels in order of display name.
|
|
758
|
-
let preferred = document.createElement('optgroup');
|
|
759
|
-
preferred.label = 'Start Preferred Kernel';
|
|
760
|
-
names.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
761
|
-
for (let name of names) {
|
|
762
|
-
preferred.appendChild(optionForName(name, displayNames[name]));
|
|
763
|
-
}
|
|
764
|
-
if (preferred.firstChild) {
|
|
765
|
-
node.appendChild(preferred);
|
|
766
|
-
}
|
|
767
|
-
// Add an option for no kernel
|
|
768
|
-
node.appendChild(optionForNone());
|
|
769
|
-
let other = document.createElement('optgroup');
|
|
770
|
-
other.label = 'Start Other Kernel';
|
|
771
|
-
// Add the rest of the kernel names in alphabetical order.
|
|
772
|
-
let otherNames = [];
|
|
773
|
-
for (let specName in specs.kernelspecs) {
|
|
774
|
-
if (names.indexOf(specName) !== -1) {
|
|
775
|
-
continue;
|
|
776
|
-
}
|
|
777
|
-
otherNames.push(specName);
|
|
778
|
-
}
|
|
779
|
-
otherNames.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
780
|
-
for (let otherName of otherNames) {
|
|
781
|
-
other.appendChild(optionForName(otherName, displayNames[otherName]));
|
|
782
|
-
}
|
|
783
|
-
// Add a separator option if there were any other names.
|
|
784
|
-
if (otherNames.length) {
|
|
785
|
-
node.appendChild(other);
|
|
786
|
-
}
|
|
787
|
-
// Handle the default value.
|
|
788
|
-
if (shouldStart === false) {
|
|
789
|
-
node.value = 'null';
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
node.selectedIndex = 0;
|
|
793
|
-
}
|
|
794
|
-
// Bail if there are no sessions.
|
|
795
|
-
if (!sessions) {
|
|
796
|
-
return;
|
|
797
|
-
}
|
|
798
|
-
// Add the sessions using the preferred language first.
|
|
799
|
-
let matchingSessions = [];
|
|
800
|
-
let otherSessions = [];
|
|
801
|
-
algorithm_1.each(sessions, session => {
|
|
802
|
-
if (language &&
|
|
803
|
-
languages[session.kernel.name] === language &&
|
|
804
|
-
session.kernel.id !== id) {
|
|
805
|
-
matchingSessions.push(session);
|
|
806
|
-
}
|
|
807
|
-
else if (session.kernel.id !== id) {
|
|
808
|
-
otherSessions.push(session);
|
|
809
|
-
}
|
|
810
|
-
});
|
|
811
|
-
let matching = document.createElement('optgroup');
|
|
812
|
-
matching.label = 'Use Kernel from Preferred Session';
|
|
813
|
-
node.appendChild(matching);
|
|
814
|
-
if (matchingSessions.length) {
|
|
815
|
-
matchingSessions.sort((a, b) => {
|
|
816
|
-
return a.path.localeCompare(b.path);
|
|
817
|
-
});
|
|
818
|
-
algorithm_1.each(matchingSessions, session => {
|
|
819
|
-
let name = displayNames[session.kernel.name];
|
|
820
|
-
matching.appendChild(optionForSession(session, name));
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
|
-
let otherSessionsNode = document.createElement('optgroup');
|
|
824
|
-
otherSessionsNode.label = 'Use Kernel from Other Session';
|
|
825
|
-
node.appendChild(otherSessionsNode);
|
|
826
|
-
if (otherSessions.length) {
|
|
827
|
-
otherSessions.sort((a, b) => {
|
|
828
|
-
return a.path.localeCompare(b.path);
|
|
829
|
-
});
|
|
830
|
-
algorithm_1.each(otherSessions, session => {
|
|
831
|
-
let name = displayNames[session.kernel.name] || session.kernel.name;
|
|
832
|
-
otherSessionsNode.appendChild(optionForSession(session, name));
|
|
833
|
-
});
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
Private.populateKernelSelect = populateKernelSelect;
|
|
837
|
-
/**
|
|
838
|
-
* Get the kernel search options given a client session and sesion manager.
|
|
839
|
-
*/
|
|
840
|
-
function getKernelSearch(session) {
|
|
841
|
-
return {
|
|
842
|
-
specs: session.manager.specs,
|
|
843
|
-
sessions: session.manager.running(),
|
|
844
|
-
preference: session.kernelPreference
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
/**
|
|
848
|
-
* Create an option element for a kernel name.
|
|
849
|
-
*/
|
|
850
|
-
function optionForName(name, displayName) {
|
|
851
|
-
let option = document.createElement('option');
|
|
852
|
-
option.text = displayName;
|
|
853
|
-
option.value = JSON.stringify({ name });
|
|
854
|
-
return option;
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Create an option for no kernel.
|
|
858
|
-
*/
|
|
859
|
-
function optionForNone() {
|
|
860
|
-
let group = document.createElement('optgroup');
|
|
861
|
-
group.label = 'Use No Kernel';
|
|
862
|
-
let option = document.createElement('option');
|
|
863
|
-
option.text = 'No Kernel';
|
|
864
|
-
option.value = 'null';
|
|
865
|
-
group.appendChild(option);
|
|
866
|
-
return group;
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* Create an option element for a session.
|
|
870
|
-
*/
|
|
871
|
-
function optionForSession(session, displayName) {
|
|
872
|
-
let option = document.createElement('option');
|
|
873
|
-
let sessionName = session.name || coreutils_1.PathExt.basename(session.path);
|
|
874
|
-
option.text = sessionName;
|
|
875
|
-
option.value = JSON.stringify({ id: session.kernel.id });
|
|
876
|
-
option.title =
|
|
877
|
-
`Path: ${session.path}\n` +
|
|
878
|
-
`Name: ${sessionName}\n` +
|
|
879
|
-
`Kernel Name: ${displayName}\n` +
|
|
880
|
-
`Kernel Id: ${session.kernel.id}`;
|
|
881
|
-
return option;
|
|
882
|
-
}
|
|
883
|
-
})(Private || (Private = {}));
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Jupyter Development Team.
|
|
3
|
+
// Distributed under the terms of the Modified BSD License.
|
|
4
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
13
|
+
if (mod && mod.__esModule) return mod;
|
|
14
|
+
var result = {};
|
|
15
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
16
|
+
result["default"] = mod;
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
const coreutils_1 = require("@jupyterlab/coreutils");
|
|
21
|
+
const coreutils_2 = require("@phosphor/coreutils");
|
|
22
|
+
const algorithm_1 = require("@phosphor/algorithm");
|
|
23
|
+
const coreutils_3 = require("@phosphor/coreutils");
|
|
24
|
+
const signaling_1 = require("@phosphor/signaling");
|
|
25
|
+
const widgets_1 = require("@phosphor/widgets");
|
|
26
|
+
const React = __importStar(require("react"));
|
|
27
|
+
const dialog_1 = require("./dialog");
|
|
28
|
+
/**
|
|
29
|
+
* The default implementation of client session object.
|
|
30
|
+
*/
|
|
31
|
+
class ClientSession {
|
|
32
|
+
/**
|
|
33
|
+
* Construct a new client session.
|
|
34
|
+
*/
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this._path = '';
|
|
37
|
+
this._name = '';
|
|
38
|
+
this._type = '';
|
|
39
|
+
this._prevKernelName = '';
|
|
40
|
+
this._isDisposed = false;
|
|
41
|
+
this._session = null;
|
|
42
|
+
this._ready = new coreutils_3.PromiseDelegate();
|
|
43
|
+
this._initializing = false;
|
|
44
|
+
this._isReady = false;
|
|
45
|
+
this._terminated = new signaling_1.Signal(this);
|
|
46
|
+
this._kernelChanged = new signaling_1.Signal(this);
|
|
47
|
+
this._statusChanged = new signaling_1.Signal(this);
|
|
48
|
+
this._iopubMessage = new signaling_1.Signal(this);
|
|
49
|
+
this._unhandledMessage = new signaling_1.Signal(this);
|
|
50
|
+
this._propertyChanged = new signaling_1.Signal(this);
|
|
51
|
+
this._dialog = null;
|
|
52
|
+
this._busyDisposable = null;
|
|
53
|
+
this.manager = options.manager;
|
|
54
|
+
this._path = options.path || coreutils_2.UUID.uuid4();
|
|
55
|
+
this._type = options.type || '';
|
|
56
|
+
this._name = options.name || '';
|
|
57
|
+
this._setBusy = options.setBusy;
|
|
58
|
+
this._kernelPreference = options.kernelPreference || {};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A signal emitted when the session is shut down.
|
|
62
|
+
*/
|
|
63
|
+
get terminated() {
|
|
64
|
+
return this._terminated;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A signal emitted when the kernel changes.
|
|
68
|
+
*/
|
|
69
|
+
get kernelChanged() {
|
|
70
|
+
return this._kernelChanged;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A signal emitted when the status changes.
|
|
74
|
+
*/
|
|
75
|
+
get statusChanged() {
|
|
76
|
+
return this._statusChanged;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A signal emitted for iopub kernel messages.
|
|
80
|
+
*/
|
|
81
|
+
get iopubMessage() {
|
|
82
|
+
return this._iopubMessage;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A signal emitted for an unhandled kernel message.
|
|
86
|
+
*/
|
|
87
|
+
get unhandledMessage() {
|
|
88
|
+
return this._unhandledMessage;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A signal emitted when a session property changes.
|
|
92
|
+
*/
|
|
93
|
+
get propertyChanged() {
|
|
94
|
+
return this._propertyChanged;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The current kernel of the session.
|
|
98
|
+
*/
|
|
99
|
+
get kernel() {
|
|
100
|
+
return this._session ? this._session.kernel : null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The current path of the session.
|
|
104
|
+
*/
|
|
105
|
+
get path() {
|
|
106
|
+
return this._path;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The current name of the session.
|
|
110
|
+
*/
|
|
111
|
+
get name() {
|
|
112
|
+
return this._name;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The type of the client session.
|
|
116
|
+
*/
|
|
117
|
+
get type() {
|
|
118
|
+
return this._type;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The kernel preference of the session.
|
|
122
|
+
*/
|
|
123
|
+
get kernelPreference() {
|
|
124
|
+
return this._kernelPreference;
|
|
125
|
+
}
|
|
126
|
+
set kernelPreference(value) {
|
|
127
|
+
this._kernelPreference = value;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The current status of the session.
|
|
131
|
+
*/
|
|
132
|
+
get status() {
|
|
133
|
+
if (!this.isReady) {
|
|
134
|
+
return 'starting';
|
|
135
|
+
}
|
|
136
|
+
return this._session ? this._session.status : 'dead';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Whether the session is ready.
|
|
140
|
+
*/
|
|
141
|
+
get isReady() {
|
|
142
|
+
return this._isReady;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* A promise that is fulfilled when the session is ready.
|
|
146
|
+
*/
|
|
147
|
+
get ready() {
|
|
148
|
+
return this._ready.promise;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The display name of the current kernel.
|
|
152
|
+
*/
|
|
153
|
+
get kernelDisplayName() {
|
|
154
|
+
let kernel = this.kernel;
|
|
155
|
+
if (!kernel) {
|
|
156
|
+
return 'No Kernel!';
|
|
157
|
+
}
|
|
158
|
+
let specs = this.manager.specs;
|
|
159
|
+
if (!specs) {
|
|
160
|
+
return 'Unknown!';
|
|
161
|
+
}
|
|
162
|
+
let spec = specs.kernelspecs[kernel.name];
|
|
163
|
+
return spec ? spec.display_name : kernel.name;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Test whether the context is disposed.
|
|
167
|
+
*/
|
|
168
|
+
get isDisposed() {
|
|
169
|
+
return this._isDisposed;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Dispose of the resources held by the context.
|
|
173
|
+
*/
|
|
174
|
+
dispose() {
|
|
175
|
+
if (this._isDisposed) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this._isDisposed = true;
|
|
179
|
+
if (this._session) {
|
|
180
|
+
this._session = null;
|
|
181
|
+
}
|
|
182
|
+
if (this._dialog) {
|
|
183
|
+
this._dialog.dispose();
|
|
184
|
+
}
|
|
185
|
+
signaling_1.Signal.clearData(this);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Change the current kernel associated with the document.
|
|
189
|
+
*/
|
|
190
|
+
changeKernel(options) {
|
|
191
|
+
return this.initialize().then(() => {
|
|
192
|
+
if (this.isDisposed) {
|
|
193
|
+
return Promise.reject('Disposed');
|
|
194
|
+
}
|
|
195
|
+
return this._changeKernel(options);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Select a kernel for the session.
|
|
200
|
+
*/
|
|
201
|
+
selectKernel() {
|
|
202
|
+
return this.initialize().then(() => {
|
|
203
|
+
if (this.isDisposed) {
|
|
204
|
+
return Promise.reject('Disposed');
|
|
205
|
+
}
|
|
206
|
+
return this._selectKernel(true);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Kill the kernel and shutdown the session.
|
|
211
|
+
*
|
|
212
|
+
* @returns A promise that resolves when the session is shut down.
|
|
213
|
+
*/
|
|
214
|
+
shutdown() {
|
|
215
|
+
const session = this._session;
|
|
216
|
+
if (this.isDisposed || !session) {
|
|
217
|
+
return Promise.resolve();
|
|
218
|
+
}
|
|
219
|
+
this._session = null;
|
|
220
|
+
return session.shutdown();
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Restart the session.
|
|
224
|
+
*
|
|
225
|
+
* @returns A promise that resolves with whether the kernel has restarted.
|
|
226
|
+
*
|
|
227
|
+
* #### Notes
|
|
228
|
+
* If there is a running kernel, present a dialog.
|
|
229
|
+
* If there is no kernel, we start a kernel with the last run
|
|
230
|
+
* kernel name and resolves with `true`.
|
|
231
|
+
*/
|
|
232
|
+
restart() {
|
|
233
|
+
return this.initialize().then(() => {
|
|
234
|
+
if (this.isDisposed) {
|
|
235
|
+
return Promise.reject('session already disposed');
|
|
236
|
+
}
|
|
237
|
+
let kernel = this.kernel;
|
|
238
|
+
if (!kernel) {
|
|
239
|
+
if (this._prevKernelName) {
|
|
240
|
+
return this.changeKernel({ name: this._prevKernelName }).then(() => true);
|
|
241
|
+
}
|
|
242
|
+
// Bail if there is no previous kernel to start.
|
|
243
|
+
return Promise.reject('No kernel to restart');
|
|
244
|
+
}
|
|
245
|
+
return ClientSession.restartKernel(kernel);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Change the session path.
|
|
250
|
+
*
|
|
251
|
+
* @param path - The new session path.
|
|
252
|
+
*
|
|
253
|
+
* @returns A promise that resolves when the session has renamed.
|
|
254
|
+
*
|
|
255
|
+
* #### Notes
|
|
256
|
+
* This uses the Jupyter REST API, and the response is validated.
|
|
257
|
+
* The promise is fulfilled on a valid response and rejected otherwise.
|
|
258
|
+
*/
|
|
259
|
+
setPath(path) {
|
|
260
|
+
if (this.isDisposed || this._path === path) {
|
|
261
|
+
return Promise.resolve();
|
|
262
|
+
}
|
|
263
|
+
this._path = path;
|
|
264
|
+
if (this._session) {
|
|
265
|
+
return this._session.setPath(path);
|
|
266
|
+
}
|
|
267
|
+
this._propertyChanged.emit('path');
|
|
268
|
+
return Promise.resolve();
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Change the session name.
|
|
272
|
+
*/
|
|
273
|
+
setName(name) {
|
|
274
|
+
if (this.isDisposed || this._name === name) {
|
|
275
|
+
return Promise.resolve();
|
|
276
|
+
}
|
|
277
|
+
this._name = name;
|
|
278
|
+
if (this._session) {
|
|
279
|
+
return this._session.setName(name);
|
|
280
|
+
}
|
|
281
|
+
this._propertyChanged.emit('name');
|
|
282
|
+
return Promise.resolve();
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Change the session type.
|
|
286
|
+
*/
|
|
287
|
+
setType(type) {
|
|
288
|
+
if (this.isDisposed || this._type === type) {
|
|
289
|
+
return Promise.resolve();
|
|
290
|
+
}
|
|
291
|
+
this._type = type;
|
|
292
|
+
if (this._session) {
|
|
293
|
+
return this._session.setType(name);
|
|
294
|
+
}
|
|
295
|
+
this._propertyChanged.emit('type');
|
|
296
|
+
return Promise.resolve();
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Initialize the session.
|
|
300
|
+
*
|
|
301
|
+
* #### Notes
|
|
302
|
+
* If a server session exists on the current path, we will connect to it.
|
|
303
|
+
* If preferences include disabling `canStart` or `shouldStart`, no
|
|
304
|
+
* server session will be started.
|
|
305
|
+
* If a kernel id is given, we attempt to start a session with that id.
|
|
306
|
+
* If a default kernel is available, we connect to it.
|
|
307
|
+
* Otherwise we ask the user to select a kernel.
|
|
308
|
+
*/
|
|
309
|
+
initialize() {
|
|
310
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
+
if (this._initializing || this._isReady) {
|
|
312
|
+
return this._ready.promise;
|
|
313
|
+
}
|
|
314
|
+
this._initializing = true;
|
|
315
|
+
let manager = this.manager;
|
|
316
|
+
yield manager.ready;
|
|
317
|
+
let model = algorithm_1.find(manager.running(), item => {
|
|
318
|
+
return item.path === this._path;
|
|
319
|
+
});
|
|
320
|
+
if (model) {
|
|
321
|
+
try {
|
|
322
|
+
let session = manager.connectTo(model);
|
|
323
|
+
this._handleNewSession(session);
|
|
324
|
+
}
|
|
325
|
+
catch (err) {
|
|
326
|
+
this._handleSessionError(err);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
yield this._startIfNecessary();
|
|
330
|
+
this._isReady = true;
|
|
331
|
+
this._ready.resolve(undefined);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Start the session if necessary.
|
|
336
|
+
*/
|
|
337
|
+
_startIfNecessary() {
|
|
338
|
+
let preference = this.kernelPreference;
|
|
339
|
+
if (this.isDisposed ||
|
|
340
|
+
this.kernel ||
|
|
341
|
+
preference.shouldStart === false ||
|
|
342
|
+
preference.canStart === false) {
|
|
343
|
+
return Promise.resolve();
|
|
344
|
+
}
|
|
345
|
+
// Try to use an existing kernel.
|
|
346
|
+
if (preference.id) {
|
|
347
|
+
return this._changeKernel({ id: preference.id })
|
|
348
|
+
.then(() => undefined)
|
|
349
|
+
.catch(() => this._selectKernel(false));
|
|
350
|
+
}
|
|
351
|
+
let name = ClientSession.getDefaultKernel({
|
|
352
|
+
specs: this.manager.specs,
|
|
353
|
+
sessions: this.manager.running(),
|
|
354
|
+
preference
|
|
355
|
+
});
|
|
356
|
+
if (name) {
|
|
357
|
+
return this._changeKernel({ name })
|
|
358
|
+
.then(() => undefined)
|
|
359
|
+
.catch(() => this._selectKernel(false));
|
|
360
|
+
}
|
|
361
|
+
return this._selectKernel(false);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Change the kernel.
|
|
365
|
+
*/
|
|
366
|
+
_changeKernel(options) {
|
|
367
|
+
if (this.isDisposed) {
|
|
368
|
+
return Promise.reject('Disposed');
|
|
369
|
+
}
|
|
370
|
+
let session = this._session;
|
|
371
|
+
if (session) {
|
|
372
|
+
return session.changeKernel(options);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
return this._startSession(options);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Select a kernel.
|
|
380
|
+
*
|
|
381
|
+
* @param cancelable: whether the dialog should have a cancel button.
|
|
382
|
+
*/
|
|
383
|
+
_selectKernel(cancelable) {
|
|
384
|
+
if (this.isDisposed) {
|
|
385
|
+
return Promise.resolve();
|
|
386
|
+
}
|
|
387
|
+
const buttons = cancelable
|
|
388
|
+
? [dialog_1.Dialog.cancelButton(), dialog_1.Dialog.okButton({ label: 'SELECT' })]
|
|
389
|
+
: [dialog_1.Dialog.okButton({ label: 'SELECT' })];
|
|
390
|
+
let dialog = (this._dialog = new dialog_1.Dialog({
|
|
391
|
+
title: 'Select Kernel',
|
|
392
|
+
body: new Private.KernelSelector(this),
|
|
393
|
+
buttons
|
|
394
|
+
}));
|
|
395
|
+
return dialog
|
|
396
|
+
.launch()
|
|
397
|
+
.then(result => {
|
|
398
|
+
if (this.isDisposed || !result.button.accept) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
let model = result.value;
|
|
402
|
+
if (model === null && this._session) {
|
|
403
|
+
return this.shutdown().then(() => {
|
|
404
|
+
this._kernelChanged.emit({ oldValue: null, newValue: null });
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
if (model) {
|
|
408
|
+
return this._changeKernel(model).then(() => undefined);
|
|
409
|
+
}
|
|
410
|
+
})
|
|
411
|
+
.then(() => {
|
|
412
|
+
this._dialog = null;
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Start a session and set up its signals.
|
|
417
|
+
*/
|
|
418
|
+
_startSession(model) {
|
|
419
|
+
if (this.isDisposed) {
|
|
420
|
+
return Promise.reject('Session is disposed.');
|
|
421
|
+
}
|
|
422
|
+
return this.manager
|
|
423
|
+
.startNew({
|
|
424
|
+
path: this._path,
|
|
425
|
+
type: this._type,
|
|
426
|
+
name: this._name,
|
|
427
|
+
kernelName: model ? model.name : undefined,
|
|
428
|
+
kernelId: model ? model.id : undefined
|
|
429
|
+
})
|
|
430
|
+
.then(session => {
|
|
431
|
+
return this._handleNewSession(session);
|
|
432
|
+
})
|
|
433
|
+
.catch(err => {
|
|
434
|
+
this._handleSessionError(err);
|
|
435
|
+
return Promise.reject(err);
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Handle a new session object.
|
|
440
|
+
*/
|
|
441
|
+
_handleNewSession(session) {
|
|
442
|
+
if (this.isDisposed) {
|
|
443
|
+
throw Error('Disposed');
|
|
444
|
+
}
|
|
445
|
+
if (this._session) {
|
|
446
|
+
this._session.dispose();
|
|
447
|
+
}
|
|
448
|
+
this._session = session;
|
|
449
|
+
if (session.path !== this._path) {
|
|
450
|
+
this._path = session.path;
|
|
451
|
+
this._propertyChanged.emit('path');
|
|
452
|
+
}
|
|
453
|
+
if (session.name !== this._name) {
|
|
454
|
+
this._name = session.name;
|
|
455
|
+
this._propertyChanged.emit('name');
|
|
456
|
+
}
|
|
457
|
+
if (session.type !== this._type) {
|
|
458
|
+
this._type = session.type;
|
|
459
|
+
this._propertyChanged.emit('type');
|
|
460
|
+
}
|
|
461
|
+
session.terminated.connect(this._onTerminated, this);
|
|
462
|
+
session.propertyChanged.connect(this._onPropertyChanged, this);
|
|
463
|
+
session.kernelChanged.connect(this._onKernelChanged, this);
|
|
464
|
+
session.statusChanged.connect(this._onStatusChanged, this);
|
|
465
|
+
session.iopubMessage.connect(this._onIopubMessage, this);
|
|
466
|
+
session.unhandledMessage.connect(this._onUnhandledMessage, this);
|
|
467
|
+
this._prevKernelName = session.kernel.name;
|
|
468
|
+
// The session kernel was disposed above when the session was disposed, so
|
|
469
|
+
// the oldValue should be null.
|
|
470
|
+
this._kernelChanged.emit({ oldValue: null, newValue: session.kernel });
|
|
471
|
+
return session.kernel;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Handle an error in session startup.
|
|
475
|
+
*/
|
|
476
|
+
_handleSessionError(err) {
|
|
477
|
+
return err.response
|
|
478
|
+
.text()
|
|
479
|
+
.then(text => {
|
|
480
|
+
let message = err.message;
|
|
481
|
+
try {
|
|
482
|
+
message = JSON.parse(text)['traceback'];
|
|
483
|
+
}
|
|
484
|
+
catch (err) {
|
|
485
|
+
// no-op
|
|
486
|
+
}
|
|
487
|
+
let dialog = (this._dialog = new dialog_1.Dialog({
|
|
488
|
+
title: 'Error Starting Kernel',
|
|
489
|
+
body: React.createElement("pre", null, message),
|
|
490
|
+
buttons: [dialog_1.Dialog.okButton()]
|
|
491
|
+
}));
|
|
492
|
+
return dialog.launch();
|
|
493
|
+
})
|
|
494
|
+
.then(() => {
|
|
495
|
+
this._dialog = null;
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Handle a session termination.
|
|
500
|
+
*/
|
|
501
|
+
_onTerminated() {
|
|
502
|
+
let kernel = this.kernel;
|
|
503
|
+
if (this._session) {
|
|
504
|
+
this._session.dispose();
|
|
505
|
+
}
|
|
506
|
+
this._session = null;
|
|
507
|
+
this._terminated.emit(undefined);
|
|
508
|
+
if (kernel) {
|
|
509
|
+
this._kernelChanged.emit({ oldValue: null, newValue: null });
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Handle a change to a session property.
|
|
514
|
+
*/
|
|
515
|
+
_onPropertyChanged(sender, property) {
|
|
516
|
+
switch (property) {
|
|
517
|
+
case 'path':
|
|
518
|
+
this._path = sender.path;
|
|
519
|
+
break;
|
|
520
|
+
case 'name':
|
|
521
|
+
this._name = sender.name;
|
|
522
|
+
break;
|
|
523
|
+
default:
|
|
524
|
+
this._type = sender.type;
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
this._propertyChanged.emit(property);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Handle a change to the kernel.
|
|
531
|
+
*/
|
|
532
|
+
_onKernelChanged(sender, args) {
|
|
533
|
+
this._kernelChanged.emit(args);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Handle a change to the session status.
|
|
537
|
+
*/
|
|
538
|
+
_onStatusChanged() {
|
|
539
|
+
// Set that this kernel is busy, if we haven't already
|
|
540
|
+
// If we have already, and now we aren't busy, dispose
|
|
541
|
+
// of the busy disposable.
|
|
542
|
+
if (this._setBusy) {
|
|
543
|
+
if (this.status === 'busy') {
|
|
544
|
+
if (!this._busyDisposable) {
|
|
545
|
+
this._busyDisposable = this._setBusy();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
if (this._busyDisposable) {
|
|
550
|
+
this._busyDisposable.dispose();
|
|
551
|
+
this._busyDisposable = null;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
this._statusChanged.emit(this.status);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Handle an iopub message.
|
|
559
|
+
*/
|
|
560
|
+
_onIopubMessage(sender, message) {
|
|
561
|
+
this._iopubMessage.emit(message);
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Handle an unhandled message.
|
|
565
|
+
*/
|
|
566
|
+
_onUnhandledMessage(sender, message) {
|
|
567
|
+
this._unhandledMessage.emit(message);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
exports.ClientSession = ClientSession;
|
|
571
|
+
/**
|
|
572
|
+
* A namespace for `ClientSession` statics.
|
|
573
|
+
*/
|
|
574
|
+
(function (ClientSession) {
|
|
575
|
+
/**
|
|
576
|
+
* Restart a kernel if the user accepts the risk.
|
|
577
|
+
*
|
|
578
|
+
* Returns a promise resolving with whether the kernel was restarted.
|
|
579
|
+
*/
|
|
580
|
+
function restartKernel(kernel) {
|
|
581
|
+
let restartBtn = dialog_1.Dialog.warnButton({ label: 'RESTART ' });
|
|
582
|
+
return dialog_1.showDialog({
|
|
583
|
+
title: 'Restart Kernel?',
|
|
584
|
+
body: 'Do you want to restart the current kernel? All variables will be lost.',
|
|
585
|
+
buttons: [dialog_1.Dialog.cancelButton(), restartBtn]
|
|
586
|
+
}).then(result => {
|
|
587
|
+
if (kernel.isDisposed) {
|
|
588
|
+
return Promise.resolve(false);
|
|
589
|
+
}
|
|
590
|
+
if (result.button.accept) {
|
|
591
|
+
return kernel.restart().then(() => {
|
|
592
|
+
return true;
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
return false;
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
ClientSession.restartKernel = restartKernel;
|
|
599
|
+
/**
|
|
600
|
+
* Get the default kernel name given select options.
|
|
601
|
+
*/
|
|
602
|
+
function getDefaultKernel(options) {
|
|
603
|
+
return Private.getDefaultKernel(options);
|
|
604
|
+
}
|
|
605
|
+
ClientSession.getDefaultKernel = getDefaultKernel;
|
|
606
|
+
/**
|
|
607
|
+
* Populate a kernel dropdown list.
|
|
608
|
+
*
|
|
609
|
+
* @param node - The node to populate.
|
|
610
|
+
*
|
|
611
|
+
* @param options - The options used to populate the kernels.
|
|
612
|
+
*
|
|
613
|
+
* #### Notes
|
|
614
|
+
* Populates the list with separated sections:
|
|
615
|
+
* - Kernels matching the preferred language (display names).
|
|
616
|
+
* - "None" signifying no kernel.
|
|
617
|
+
* - The remaining kernels.
|
|
618
|
+
* - Sessions matching the preferred language (file names).
|
|
619
|
+
* - The remaining sessions.
|
|
620
|
+
* If no preferred language is given or no kernels are found using
|
|
621
|
+
* the preferred language, the default kernel is used in the first
|
|
622
|
+
* section. Kernels are sorted by display name. Sessions display the
|
|
623
|
+
* base name of the file with an ellipsis overflow and a tooltip with
|
|
624
|
+
* the explicit session information.
|
|
625
|
+
*/
|
|
626
|
+
function populateKernelSelect(node, options) {
|
|
627
|
+
return Private.populateKernelSelect(node, options);
|
|
628
|
+
}
|
|
629
|
+
ClientSession.populateKernelSelect = populateKernelSelect;
|
|
630
|
+
})(ClientSession = exports.ClientSession || (exports.ClientSession = {}));
|
|
631
|
+
/**
|
|
632
|
+
* The namespace for module private data.
|
|
633
|
+
*/
|
|
634
|
+
var Private;
|
|
635
|
+
(function (Private) {
|
|
636
|
+
/**
|
|
637
|
+
* A widget that provides a kernel selection.
|
|
638
|
+
*/
|
|
639
|
+
class KernelSelector extends widgets_1.Widget {
|
|
640
|
+
/**
|
|
641
|
+
* Create a new kernel selector widget.
|
|
642
|
+
*/
|
|
643
|
+
constructor(session) {
|
|
644
|
+
super({ node: createSelectorNode(session) });
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Get the value of the kernel selector widget.
|
|
648
|
+
*/
|
|
649
|
+
getValue() {
|
|
650
|
+
let selector = this.node.querySelector('select');
|
|
651
|
+
return JSON.parse(selector.value);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
Private.KernelSelector = KernelSelector;
|
|
655
|
+
/**
|
|
656
|
+
* Create a node for a kernel selector widget.
|
|
657
|
+
*/
|
|
658
|
+
function createSelectorNode(session) {
|
|
659
|
+
// Create the dialog body.
|
|
660
|
+
let body = document.createElement('div');
|
|
661
|
+
let text = document.createElement('label');
|
|
662
|
+
text.innerHTML = `Select kernel for: "${session.name}"`;
|
|
663
|
+
body.appendChild(text);
|
|
664
|
+
let options = getKernelSearch(session);
|
|
665
|
+
let selector = document.createElement('select');
|
|
666
|
+
ClientSession.populateKernelSelect(selector, options);
|
|
667
|
+
body.appendChild(selector);
|
|
668
|
+
return body;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Get the default kernel name given select options.
|
|
672
|
+
*/
|
|
673
|
+
function getDefaultKernel(options) {
|
|
674
|
+
let { specs, preference } = options;
|
|
675
|
+
let { name, language, shouldStart, canStart, autoStartDefault } = preference;
|
|
676
|
+
if (!specs || shouldStart === false || canStart === false) {
|
|
677
|
+
return null;
|
|
678
|
+
}
|
|
679
|
+
let defaultName = autoStartDefault ? specs.default : null;
|
|
680
|
+
if (!name && !language) {
|
|
681
|
+
return defaultName;
|
|
682
|
+
}
|
|
683
|
+
// Look for an exact match of a spec name.
|
|
684
|
+
for (let specName in specs.kernelspecs) {
|
|
685
|
+
if (specName === name) {
|
|
686
|
+
return name;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
// Bail if there is no language.
|
|
690
|
+
if (!language) {
|
|
691
|
+
return defaultName;
|
|
692
|
+
}
|
|
693
|
+
// Check for a single kernel matching the language.
|
|
694
|
+
let matches = [];
|
|
695
|
+
for (let specName in specs.kernelspecs) {
|
|
696
|
+
let kernelLanguage = specs.kernelspecs[specName].language;
|
|
697
|
+
if (language === kernelLanguage) {
|
|
698
|
+
matches.push(specName);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
if (matches.length === 1) {
|
|
702
|
+
let specName = matches[0];
|
|
703
|
+
console.log('No exact match found for ' +
|
|
704
|
+
specName +
|
|
705
|
+
', using kernel ' +
|
|
706
|
+
specName +
|
|
707
|
+
' that matches ' +
|
|
708
|
+
'language=' +
|
|
709
|
+
language);
|
|
710
|
+
return specName;
|
|
711
|
+
}
|
|
712
|
+
// No matches found.
|
|
713
|
+
return defaultName;
|
|
714
|
+
}
|
|
715
|
+
Private.getDefaultKernel = getDefaultKernel;
|
|
716
|
+
/**
|
|
717
|
+
* Populate a kernel select node for the session.
|
|
718
|
+
*/
|
|
719
|
+
function populateKernelSelect(node, options) {
|
|
720
|
+
while (node.firstChild) {
|
|
721
|
+
node.removeChild(node.firstChild);
|
|
722
|
+
}
|
|
723
|
+
let { preference, sessions, specs } = options;
|
|
724
|
+
let { name, id, language, canStart, shouldStart } = preference;
|
|
725
|
+
if (!specs || canStart === false) {
|
|
726
|
+
node.appendChild(optionForNone());
|
|
727
|
+
node.value = 'null';
|
|
728
|
+
node.disabled = true;
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
node.disabled = false;
|
|
732
|
+
// Create mappings of display names and languages for kernel name.
|
|
733
|
+
let displayNames = Object.create(null);
|
|
734
|
+
let languages = Object.create(null);
|
|
735
|
+
for (let name in specs.kernelspecs) {
|
|
736
|
+
let spec = specs.kernelspecs[name];
|
|
737
|
+
displayNames[name] = spec.display_name;
|
|
738
|
+
languages[name] = spec.language;
|
|
739
|
+
}
|
|
740
|
+
// Handle a kernel by name.
|
|
741
|
+
let names = [];
|
|
742
|
+
if (name && name in specs.kernelspecs) {
|
|
743
|
+
names.push(name);
|
|
744
|
+
}
|
|
745
|
+
// Then look by language.
|
|
746
|
+
if (language) {
|
|
747
|
+
for (let specName in specs.kernelspecs) {
|
|
748
|
+
if (name !== specName && languages[specName] === language) {
|
|
749
|
+
names.push(specName);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
// Use the default kernel if no kernels were found.
|
|
754
|
+
if (!names.length) {
|
|
755
|
+
names.push(specs.default);
|
|
756
|
+
}
|
|
757
|
+
// Handle a preferred kernels in order of display name.
|
|
758
|
+
let preferred = document.createElement('optgroup');
|
|
759
|
+
preferred.label = 'Start Preferred Kernel';
|
|
760
|
+
names.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
761
|
+
for (let name of names) {
|
|
762
|
+
preferred.appendChild(optionForName(name, displayNames[name]));
|
|
763
|
+
}
|
|
764
|
+
if (preferred.firstChild) {
|
|
765
|
+
node.appendChild(preferred);
|
|
766
|
+
}
|
|
767
|
+
// Add an option for no kernel
|
|
768
|
+
node.appendChild(optionForNone());
|
|
769
|
+
let other = document.createElement('optgroup');
|
|
770
|
+
other.label = 'Start Other Kernel';
|
|
771
|
+
// Add the rest of the kernel names in alphabetical order.
|
|
772
|
+
let otherNames = [];
|
|
773
|
+
for (let specName in specs.kernelspecs) {
|
|
774
|
+
if (names.indexOf(specName) !== -1) {
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
otherNames.push(specName);
|
|
778
|
+
}
|
|
779
|
+
otherNames.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
780
|
+
for (let otherName of otherNames) {
|
|
781
|
+
other.appendChild(optionForName(otherName, displayNames[otherName]));
|
|
782
|
+
}
|
|
783
|
+
// Add a separator option if there were any other names.
|
|
784
|
+
if (otherNames.length) {
|
|
785
|
+
node.appendChild(other);
|
|
786
|
+
}
|
|
787
|
+
// Handle the default value.
|
|
788
|
+
if (shouldStart === false) {
|
|
789
|
+
node.value = 'null';
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
node.selectedIndex = 0;
|
|
793
|
+
}
|
|
794
|
+
// Bail if there are no sessions.
|
|
795
|
+
if (!sessions) {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
// Add the sessions using the preferred language first.
|
|
799
|
+
let matchingSessions = [];
|
|
800
|
+
let otherSessions = [];
|
|
801
|
+
algorithm_1.each(sessions, session => {
|
|
802
|
+
if (language &&
|
|
803
|
+
languages[session.kernel.name] === language &&
|
|
804
|
+
session.kernel.id !== id) {
|
|
805
|
+
matchingSessions.push(session);
|
|
806
|
+
}
|
|
807
|
+
else if (session.kernel.id !== id) {
|
|
808
|
+
otherSessions.push(session);
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
let matching = document.createElement('optgroup');
|
|
812
|
+
matching.label = 'Use Kernel from Preferred Session';
|
|
813
|
+
node.appendChild(matching);
|
|
814
|
+
if (matchingSessions.length) {
|
|
815
|
+
matchingSessions.sort((a, b) => {
|
|
816
|
+
return a.path.localeCompare(b.path);
|
|
817
|
+
});
|
|
818
|
+
algorithm_1.each(matchingSessions, session => {
|
|
819
|
+
let name = displayNames[session.kernel.name];
|
|
820
|
+
matching.appendChild(optionForSession(session, name));
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
let otherSessionsNode = document.createElement('optgroup');
|
|
824
|
+
otherSessionsNode.label = 'Use Kernel from Other Session';
|
|
825
|
+
node.appendChild(otherSessionsNode);
|
|
826
|
+
if (otherSessions.length) {
|
|
827
|
+
otherSessions.sort((a, b) => {
|
|
828
|
+
return a.path.localeCompare(b.path);
|
|
829
|
+
});
|
|
830
|
+
algorithm_1.each(otherSessions, session => {
|
|
831
|
+
let name = displayNames[session.kernel.name] || session.kernel.name;
|
|
832
|
+
otherSessionsNode.appendChild(optionForSession(session, name));
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
Private.populateKernelSelect = populateKernelSelect;
|
|
837
|
+
/**
|
|
838
|
+
* Get the kernel search options given a client session and sesion manager.
|
|
839
|
+
*/
|
|
840
|
+
function getKernelSearch(session) {
|
|
841
|
+
return {
|
|
842
|
+
specs: session.manager.specs,
|
|
843
|
+
sessions: session.manager.running(),
|
|
844
|
+
preference: session.kernelPreference
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Create an option element for a kernel name.
|
|
849
|
+
*/
|
|
850
|
+
function optionForName(name, displayName) {
|
|
851
|
+
let option = document.createElement('option');
|
|
852
|
+
option.text = displayName;
|
|
853
|
+
option.value = JSON.stringify({ name });
|
|
854
|
+
return option;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Create an option for no kernel.
|
|
858
|
+
*/
|
|
859
|
+
function optionForNone() {
|
|
860
|
+
let group = document.createElement('optgroup');
|
|
861
|
+
group.label = 'Use No Kernel';
|
|
862
|
+
let option = document.createElement('option');
|
|
863
|
+
option.text = 'No Kernel';
|
|
864
|
+
option.value = 'null';
|
|
865
|
+
group.appendChild(option);
|
|
866
|
+
return group;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Create an option element for a session.
|
|
870
|
+
*/
|
|
871
|
+
function optionForSession(session, displayName) {
|
|
872
|
+
let option = document.createElement('option');
|
|
873
|
+
let sessionName = session.name || coreutils_1.PathExt.basename(session.path);
|
|
874
|
+
option.text = sessionName;
|
|
875
|
+
option.value = JSON.stringify({ id: session.kernel.id });
|
|
876
|
+
option.title =
|
|
877
|
+
`Path: ${session.path}\n` +
|
|
878
|
+
`Name: ${sessionName}\n` +
|
|
879
|
+
`Kernel Name: ${displayName}\n` +
|
|
880
|
+
`Kernel Id: ${session.kernel.id}`;
|
|
881
|
+
return option;
|
|
882
|
+
}
|
|
883
|
+
})(Private || (Private = {}));
|