@jupyterlab/apputils 4.4.0-alpha.2 → 4.4.0-beta.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/lib/domutils.d.ts +1 -0
- package/lib/domutils.js +1 -0
- package/lib/domutils.js.map +1 -1
- package/lib/mainareawidget.js +2 -1
- package/lib/mainareawidget.js.map +1 -1
- package/lib/sanitizer.d.ts +7 -0
- package/lib/sanitizer.js +21 -12
- package/lib/sanitizer.js.map +1 -1
- package/lib/sessioncontext.d.ts +96 -0
- package/lib/sessioncontext.js +248 -203
- package/lib/sessioncontext.js.map +1 -1
- package/lib/testutils.js +5 -4
- package/lib/testutils.js.map +1 -1
- package/lib/toolbar/registry.js +4 -2
- package/lib/toolbar/registry.js.map +1 -1
- package/package.json +11 -11
- package/src/domutils.ts +1 -0
- package/src/mainareawidget.ts +2 -1
- package/src/sanitizer.ts +25 -13
- package/src/sessioncontext.tsx +358 -251
- package/src/testutils.ts +1 -0
- package/src/toolbar/registry.ts +5 -1
package/src/sessioncontext.tsx
CHANGED
|
@@ -197,6 +197,14 @@ export interface ISessionContext extends IObservableDisposable {
|
|
|
197
197
|
*/
|
|
198
198
|
readonly prevKernelName: string;
|
|
199
199
|
|
|
200
|
+
/**
|
|
201
|
+
* The kernel manager
|
|
202
|
+
*
|
|
203
|
+
* #### Notes
|
|
204
|
+
* In the next major version of this interface, a kernel manager is required.
|
|
205
|
+
*/
|
|
206
|
+
readonly kernelManager?: Kernel.IManager;
|
|
207
|
+
|
|
200
208
|
/**
|
|
201
209
|
* The session manager used by the session.
|
|
202
210
|
*/
|
|
@@ -347,6 +355,7 @@ export class SessionContext implements ISessionContext {
|
|
|
347
355
|
* Construct a new session context.
|
|
348
356
|
*/
|
|
349
357
|
constructor(options: SessionContext.IOptions) {
|
|
358
|
+
this.kernelManager = options.kernelManager;
|
|
350
359
|
this.sessionManager = options.sessionManager;
|
|
351
360
|
this.specsManager = options.specsManager;
|
|
352
361
|
this.translator = options.translator || nullTranslator;
|
|
@@ -523,6 +532,11 @@ export class SessionContext implements ISessionContext {
|
|
|
523
532
|
return this._isRestarting;
|
|
524
533
|
}
|
|
525
534
|
|
|
535
|
+
/**
|
|
536
|
+
* The kernel manager
|
|
537
|
+
*/
|
|
538
|
+
readonly kernelManager?: Kernel.IManager;
|
|
539
|
+
|
|
526
540
|
/**
|
|
527
541
|
* The session manager used by the session.
|
|
528
542
|
*/
|
|
@@ -1247,6 +1261,14 @@ export namespace SessionContext {
|
|
|
1247
1261
|
* The options used to initialize a context.
|
|
1248
1262
|
*/
|
|
1249
1263
|
export interface IOptions {
|
|
1264
|
+
/**
|
|
1265
|
+
* A kernel manager instance.
|
|
1266
|
+
*
|
|
1267
|
+
* #### Notes
|
|
1268
|
+
* In the next version of this package, `kernelManager` will be required.
|
|
1269
|
+
*/
|
|
1270
|
+
kernelManager?: Kernel.IManager;
|
|
1271
|
+
|
|
1250
1272
|
/**
|
|
1251
1273
|
* A session manager instance.
|
|
1252
1274
|
*/
|
|
@@ -1292,6 +1314,11 @@ export namespace SessionContext {
|
|
|
1292
1314
|
* An interface for populating a kernel selector.
|
|
1293
1315
|
*/
|
|
1294
1316
|
export interface IKernelSearch {
|
|
1317
|
+
/**
|
|
1318
|
+
* The current running kernels.
|
|
1319
|
+
*/
|
|
1320
|
+
kernels?: Iterable<Kernel.IModel>;
|
|
1321
|
+
|
|
1295
1322
|
/**
|
|
1296
1323
|
* The Kernel specs.
|
|
1297
1324
|
*/
|
|
@@ -1339,18 +1366,17 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
|
|
|
1339
1366
|
if (sessionContext.isDisposed) {
|
|
1340
1367
|
return Promise.resolve();
|
|
1341
1368
|
}
|
|
1342
|
-
const trans = this._translator.load('jupyterlab');
|
|
1343
1369
|
|
|
1344
|
-
|
|
1345
|
-
|
|
1370
|
+
const translator = this._translator;
|
|
1371
|
+
const trans = translator.load('jupyterlab');
|
|
1372
|
+
|
|
1373
|
+
// If there is no existing kernel, offer the option to keep no kernel.
|
|
1346
1374
|
let label = trans.__('Cancel');
|
|
1347
1375
|
if (sessionContext.hasNoKernel) {
|
|
1348
1376
|
label = sessionContext.kernelDisplayName;
|
|
1349
1377
|
}
|
|
1350
1378
|
const buttons = [
|
|
1351
|
-
Dialog.cancelButton({
|
|
1352
|
-
label
|
|
1353
|
-
}),
|
|
1379
|
+
Dialog.cancelButton({ label }),
|
|
1354
1380
|
Dialog.okButton({
|
|
1355
1381
|
label: trans.__('Select'),
|
|
1356
1382
|
ariaLabel: trans.__('Select Kernel')
|
|
@@ -1362,7 +1388,7 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
|
|
|
1362
1388
|
|
|
1363
1389
|
const dialog = new Dialog({
|
|
1364
1390
|
title: trans.__('Select Kernel'),
|
|
1365
|
-
body:
|
|
1391
|
+
body: Private.createKernelSelector(sessionContext, translator),
|
|
1366
1392
|
buttons,
|
|
1367
1393
|
checkbox: hasCheckbox
|
|
1368
1394
|
? {
|
|
@@ -1484,21 +1510,317 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
|
|
|
1484
1510
|
private _settingRegistry: ISettingRegistry | null;
|
|
1485
1511
|
}
|
|
1486
1512
|
|
|
1513
|
+
export namespace SessionContextDialogs {
|
|
1514
|
+
/**
|
|
1515
|
+
* An interface that abstracts the available kernel switching choices.
|
|
1516
|
+
*/
|
|
1517
|
+
export interface IKernelOptions {
|
|
1518
|
+
/**
|
|
1519
|
+
* Whether kernel options should be disabled.
|
|
1520
|
+
*/
|
|
1521
|
+
disabled?: boolean;
|
|
1522
|
+
|
|
1523
|
+
/**
|
|
1524
|
+
* An array of kernel option groups that correspond with `<optgroup>`.
|
|
1525
|
+
*/
|
|
1526
|
+
groups: Array<{
|
|
1527
|
+
/**
|
|
1528
|
+
* The option group label.
|
|
1529
|
+
*/
|
|
1530
|
+
label: string;
|
|
1531
|
+
|
|
1532
|
+
/**
|
|
1533
|
+
* Individual kernel (and spec) options that correspond with `<option>`.
|
|
1534
|
+
*/
|
|
1535
|
+
options: Array<{
|
|
1536
|
+
/**
|
|
1537
|
+
* Whether the option is selected.
|
|
1538
|
+
*/
|
|
1539
|
+
selected?: boolean;
|
|
1540
|
+
|
|
1541
|
+
/**
|
|
1542
|
+
* The display text of the option.
|
|
1543
|
+
*/
|
|
1544
|
+
text: string;
|
|
1545
|
+
|
|
1546
|
+
/**
|
|
1547
|
+
* The display title of the option.
|
|
1548
|
+
*/
|
|
1549
|
+
title?: string;
|
|
1550
|
+
|
|
1551
|
+
/**
|
|
1552
|
+
* The underlying (stringified JSON) value of the option.
|
|
1553
|
+
*/
|
|
1554
|
+
value: string;
|
|
1555
|
+
}>;
|
|
1556
|
+
}>;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
/**
|
|
1560
|
+
* Returns available kernel options grouped based on session context.
|
|
1561
|
+
*
|
|
1562
|
+
* #### Notes
|
|
1563
|
+
* If a language preference is set in the given session context, the options
|
|
1564
|
+
* returned are grouped with the language preference at the top:
|
|
1565
|
+
*
|
|
1566
|
+
* - (Start %1 Kernel, language)
|
|
1567
|
+
* - { all kernelspecs whose language matches in alphabetical order }
|
|
1568
|
+
* - (Use No Kernel)
|
|
1569
|
+
* - `No Kernel`
|
|
1570
|
+
* - (Start Kernel)
|
|
1571
|
+
* - { all other kernelspecs in alphabetical order }
|
|
1572
|
+
* - (Connect to Existing %1 Kernel, language)
|
|
1573
|
+
* - { all running kernels whose language matches in alphabetical order }
|
|
1574
|
+
* - (Connect to Kernel)
|
|
1575
|
+
* - { all other running kernels in alphabetical order }
|
|
1576
|
+
*
|
|
1577
|
+
* If no language preference is set, these groups and options are returned:
|
|
1578
|
+
*
|
|
1579
|
+
* - (Start Kernel)
|
|
1580
|
+
* - { all kernelspecs in alphabetical order }
|
|
1581
|
+
* - (Use No Kernel)
|
|
1582
|
+
* - `No Kernel`
|
|
1583
|
+
* - (Connect to Existing Kernel)
|
|
1584
|
+
* - { all running kernels in alphabetical order }
|
|
1585
|
+
*
|
|
1586
|
+
* If the session has a kernel ID and a kernel exists with that id, its
|
|
1587
|
+
* corresponding option has `selected` set to `true`. Otherwise if the session
|
|
1588
|
+
* context language preference is set, the first kernelspec that matches it is
|
|
1589
|
+
* selected.
|
|
1590
|
+
*/
|
|
1591
|
+
export function kernelOptions(
|
|
1592
|
+
sessionContext: ISessionContext,
|
|
1593
|
+
translator: ITranslator | null = null
|
|
1594
|
+
): IKernelOptions {
|
|
1595
|
+
const options: IKernelOptions = { disabled: false, groups: [] };
|
|
1596
|
+
// Create mapping of sessions and kernel ids.
|
|
1597
|
+
const kernels = Array.from(
|
|
1598
|
+
sessionContext.kernelManager?.running() ??
|
|
1599
|
+
// If kernel manager is unavailable use kernels from running sessions.
|
|
1600
|
+
// TODO: Remove this (next version) when kernel manager is guaranteed.
|
|
1601
|
+
Array.from(sessionContext.sessionManager.running())
|
|
1602
|
+
.filter(session => !!session.kernel)
|
|
1603
|
+
.map(session => session.kernel!)
|
|
1604
|
+
);
|
|
1605
|
+
const sessions = Array.from(
|
|
1606
|
+
sessionContext.sessionManager.running() ?? []
|
|
1607
|
+
).reduce(
|
|
1608
|
+
(sessions, session) => {
|
|
1609
|
+
if (session.kernel?.id) sessions[session.kernel.id] = session;
|
|
1610
|
+
return sessions;
|
|
1611
|
+
},
|
|
1612
|
+
{} as { [kernel: string]: Session.IModel }
|
|
1613
|
+
);
|
|
1614
|
+
const preference = {
|
|
1615
|
+
...sessionContext.kernelPreference,
|
|
1616
|
+
id: sessionContext.session?.kernel?.id
|
|
1617
|
+
};
|
|
1618
|
+
const currentKernelDisplayName = !sessionContext.hasNoKernel
|
|
1619
|
+
? sessionContext.kernelDisplayName
|
|
1620
|
+
: null;
|
|
1621
|
+
const specs = {
|
|
1622
|
+
default: '',
|
|
1623
|
+
kernelspecs: Object.create(null) as {
|
|
1624
|
+
[key: string]: KernelSpec.ISpecModel;
|
|
1625
|
+
},
|
|
1626
|
+
...sessionContext.specsManager.specs
|
|
1627
|
+
};
|
|
1628
|
+
|
|
1629
|
+
// Create mapping of languages and kernel names.
|
|
1630
|
+
const sorted: KernelSpec.ISpecModel[] = [];
|
|
1631
|
+
const languages: { [key: string]: string } = Object.create(null);
|
|
1632
|
+
|
|
1633
|
+
for (const name in specs.kernelspecs) {
|
|
1634
|
+
sorted.push(specs.kernelspecs[name]!);
|
|
1635
|
+
languages[name] = specs.kernelspecs[name]!.language;
|
|
1636
|
+
}
|
|
1637
|
+
sorted.sort((a, b) => a.display_name.localeCompare(b.display_name));
|
|
1638
|
+
translator = translator || nullTranslator;
|
|
1639
|
+
|
|
1640
|
+
const trans = translator.load('jupyterlab');
|
|
1641
|
+
const language =
|
|
1642
|
+
preference.language ||
|
|
1643
|
+
languages[preference.name!] ||
|
|
1644
|
+
(preference.id ? languages[sessions[preference.id]?.name] : '');
|
|
1645
|
+
const labels = {
|
|
1646
|
+
connectKernel: trans.__('Connect to Existing Kernel'),
|
|
1647
|
+
startPreferred: trans.__('Start %1 Kernel', language),
|
|
1648
|
+
startOther: trans.__('Start Kernel'),
|
|
1649
|
+
connectToPreferred: trans.__('Connect to Existing %1 Kernel', language),
|
|
1650
|
+
connectToOther: trans.__('Connect to Other Kernel'),
|
|
1651
|
+
noKernel: trans.__('No Kernel'),
|
|
1652
|
+
startKernel: trans.__('Start Kernel'),
|
|
1653
|
+
useNoKernel: trans.__('Use No Kernel')
|
|
1654
|
+
};
|
|
1655
|
+
const noKernel = {
|
|
1656
|
+
label: labels.useNoKernel,
|
|
1657
|
+
options: [
|
|
1658
|
+
{
|
|
1659
|
+
text: labels.noKernel,
|
|
1660
|
+
title: labels.noKernel,
|
|
1661
|
+
value: JSON.stringify(null)
|
|
1662
|
+
}
|
|
1663
|
+
]
|
|
1664
|
+
};
|
|
1665
|
+
const optionForKernel = (
|
|
1666
|
+
kernel: Kernel.IModel,
|
|
1667
|
+
displayName?: string,
|
|
1668
|
+
session?: Session.IModel
|
|
1669
|
+
): IKernelOptions['groups'][number]['options'][number] => {
|
|
1670
|
+
const sessionName = session
|
|
1671
|
+
? session.name || PathExt.basename(session.path)
|
|
1672
|
+
: kernel.name || trans.__('Unknown Kernel');
|
|
1673
|
+
return {
|
|
1674
|
+
text: `${sessionName} (${kernel.id.split('-')[0]})`,
|
|
1675
|
+
title:
|
|
1676
|
+
(session ? `${trans.__('Path: %1', session.path)}\n` : ``) +
|
|
1677
|
+
`${trans.__('Name: %1', sessionName)}\n` +
|
|
1678
|
+
`${trans.__('Kernel Name: %1', displayName ?? kernel.name)}\n` +
|
|
1679
|
+
`${trans.__('Kernel Id: %1', kernel.id)}`,
|
|
1680
|
+
value: JSON.stringify({ id: kernel.id })
|
|
1681
|
+
};
|
|
1682
|
+
};
|
|
1683
|
+
const optionForSpec = (
|
|
1684
|
+
spec: KernelSpec.ISpecModel
|
|
1685
|
+
): IKernelOptions['groups'][number]['options'][number] => ({
|
|
1686
|
+
text: spec.display_name,
|
|
1687
|
+
value: JSON.stringify({ name: spec.name })
|
|
1688
|
+
});
|
|
1689
|
+
|
|
1690
|
+
// If a kernel cannot be started, bail.
|
|
1691
|
+
if (preference.canStart === false) {
|
|
1692
|
+
options.disabled = true;
|
|
1693
|
+
options.groups.push(noKernel);
|
|
1694
|
+
return options;
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
// Create kernel option groups based on whether language preference exists.
|
|
1698
|
+
if (language) {
|
|
1699
|
+
// Add all kernelspecs, separating out the preferred language first.
|
|
1700
|
+
const preferred: IKernelOptions['groups'][number] = {
|
|
1701
|
+
label: labels.startPreferred,
|
|
1702
|
+
options: []
|
|
1703
|
+
};
|
|
1704
|
+
const other: IKernelOptions['groups'][number] = {
|
|
1705
|
+
label: labels.startOther,
|
|
1706
|
+
options: []
|
|
1707
|
+
};
|
|
1708
|
+
const preferredRunning: IKernelOptions['groups'][number] = {
|
|
1709
|
+
label: labels.connectToPreferred,
|
|
1710
|
+
options: []
|
|
1711
|
+
};
|
|
1712
|
+
const otherRunning: IKernelOptions['groups'][number] = {
|
|
1713
|
+
label: labels.connectToOther,
|
|
1714
|
+
options: []
|
|
1715
|
+
};
|
|
1716
|
+
for (const spec of sorted) {
|
|
1717
|
+
(spec.language === language ? preferred : other).options.push(
|
|
1718
|
+
optionForSpec(spec)
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
options.groups.push(preferred);
|
|
1722
|
+
options.groups.push(noKernel);
|
|
1723
|
+
options.groups.push(other);
|
|
1724
|
+
kernels
|
|
1725
|
+
.map(kernel => ({
|
|
1726
|
+
option: optionForKernel(
|
|
1727
|
+
kernel,
|
|
1728
|
+
specs.kernelspecs[kernel.name]?.display_name ?? '',
|
|
1729
|
+
sessions[kernel.id]
|
|
1730
|
+
),
|
|
1731
|
+
language: languages[kernel.name]
|
|
1732
|
+
}))
|
|
1733
|
+
.sort((a, b) => a.option.text.localeCompare(b.option.text))
|
|
1734
|
+
.forEach(kernel =>
|
|
1735
|
+
(language === kernel.language
|
|
1736
|
+
? preferredRunning
|
|
1737
|
+
: otherRunning
|
|
1738
|
+
).options.push(kernel.option)
|
|
1739
|
+
);
|
|
1740
|
+
if (preferredRunning.options.length)
|
|
1741
|
+
options.groups.push(preferredRunning);
|
|
1742
|
+
if (otherRunning.options.length) options.groups.push(otherRunning);
|
|
1743
|
+
} else {
|
|
1744
|
+
// Add kernelspecs first.
|
|
1745
|
+
options.groups.push({
|
|
1746
|
+
label: labels.startKernel,
|
|
1747
|
+
options: sorted.map(spec => optionForSpec(spec))
|
|
1748
|
+
});
|
|
1749
|
+
|
|
1750
|
+
// Next add the no kernel option.
|
|
1751
|
+
options.groups.push(noKernel);
|
|
1752
|
+
|
|
1753
|
+
// Add running kernels.
|
|
1754
|
+
options.groups.push({
|
|
1755
|
+
label: labels.connectKernel,
|
|
1756
|
+
options: kernels
|
|
1757
|
+
.map(kernel =>
|
|
1758
|
+
optionForKernel(
|
|
1759
|
+
kernel,
|
|
1760
|
+
specs.kernelspecs[kernel.name]?.display_name ?? '',
|
|
1761
|
+
sessions[kernel.id]
|
|
1762
|
+
)
|
|
1763
|
+
)
|
|
1764
|
+
.sort((a, b) => a.text.localeCompare(b.text))
|
|
1765
|
+
});
|
|
1766
|
+
}
|
|
1767
|
+
// Set the selected option.
|
|
1768
|
+
if (preference.id || currentKernelDisplayName || preference.name) {
|
|
1769
|
+
for (const group of options.groups) {
|
|
1770
|
+
for (const option of group.options) {
|
|
1771
|
+
const choice = JSON.parse(option.value) as null | Kernel.IModel;
|
|
1772
|
+
if (!choice) continue;
|
|
1773
|
+
if (preference.id) {
|
|
1774
|
+
if (preference.id === choice.id) {
|
|
1775
|
+
option.selected = true;
|
|
1776
|
+
return options;
|
|
1777
|
+
}
|
|
1778
|
+
continue;
|
|
1779
|
+
}
|
|
1780
|
+
if (currentKernelDisplayName) {
|
|
1781
|
+
if (
|
|
1782
|
+
currentKernelDisplayName ===
|
|
1783
|
+
specs.kernelspecs[choice.name]?.display_name
|
|
1784
|
+
) {
|
|
1785
|
+
option.selected = true;
|
|
1786
|
+
return options;
|
|
1787
|
+
}
|
|
1788
|
+
continue;
|
|
1789
|
+
}
|
|
1790
|
+
if (preference.name) {
|
|
1791
|
+
if (preference.name === choice.name) {
|
|
1792
|
+
option.selected = true;
|
|
1793
|
+
return options;
|
|
1794
|
+
}
|
|
1795
|
+
continue;
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
return options;
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1487
1805
|
/**
|
|
1488
1806
|
* The namespace for module private data.
|
|
1489
1807
|
*/
|
|
1490
1808
|
namespace Private {
|
|
1491
1809
|
/**
|
|
1492
|
-
*
|
|
1810
|
+
* Return a kernel selector widget.
|
|
1493
1811
|
*/
|
|
1494
|
-
export
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
}
|
|
1812
|
+
export const createKernelSelector = (
|
|
1813
|
+
sessionContext: ISessionContext,
|
|
1814
|
+
translator?: ITranslator
|
|
1815
|
+
) =>
|
|
1816
|
+
new KernelSelector({
|
|
1817
|
+
node: createSelectorNode(sessionContext, translator)
|
|
1818
|
+
});
|
|
1501
1819
|
|
|
1820
|
+
/**
|
|
1821
|
+
* A widget that provides a kernel selection.
|
|
1822
|
+
*/
|
|
1823
|
+
class KernelSelector extends Widget {
|
|
1502
1824
|
/**
|
|
1503
1825
|
* Get the value of the kernel selector widget.
|
|
1504
1826
|
*/
|
|
@@ -1509,7 +1831,7 @@ namespace Private {
|
|
|
1509
1831
|
}
|
|
1510
1832
|
|
|
1511
1833
|
/**
|
|
1512
|
-
* Create
|
|
1834
|
+
* Create an HTML node for a kernel selector widget.
|
|
1513
1835
|
*/
|
|
1514
1836
|
function createSelectorNode(
|
|
1515
1837
|
sessionContext: ISessionContext,
|
|
@@ -1526,15 +1848,27 @@ namespace Private {
|
|
|
1526
1848
|
}"`;
|
|
1527
1849
|
body.appendChild(text);
|
|
1528
1850
|
|
|
1529
|
-
const
|
|
1530
|
-
const
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
options,
|
|
1534
|
-
translator,
|
|
1535
|
-
!sessionContext.hasNoKernel ? sessionContext.kernelDisplayName : null
|
|
1851
|
+
const select = document.createElement('select');
|
|
1852
|
+
const options = SessionContextDialogs.kernelOptions(
|
|
1853
|
+
sessionContext,
|
|
1854
|
+
translator
|
|
1536
1855
|
);
|
|
1537
|
-
|
|
1856
|
+
if (options.disabled) select.disabled = true;
|
|
1857
|
+
for (const group of options.groups) {
|
|
1858
|
+
const { label, options } = group;
|
|
1859
|
+
const optgroup = document.createElement('optgroup');
|
|
1860
|
+
optgroup.label = label;
|
|
1861
|
+
for (const { selected, text, title, value } of options) {
|
|
1862
|
+
const option = document.createElement('option');
|
|
1863
|
+
if (selected) option.selected = true;
|
|
1864
|
+
if (title) option.title = title;
|
|
1865
|
+
option.text = text;
|
|
1866
|
+
option.value = value;
|
|
1867
|
+
optgroup.appendChild(option);
|
|
1868
|
+
}
|
|
1869
|
+
select.appendChild(optgroup);
|
|
1870
|
+
}
|
|
1871
|
+
body.appendChild(select);
|
|
1538
1872
|
return body;
|
|
1539
1873
|
}
|
|
1540
1874
|
|
|
@@ -1595,231 +1929,4 @@ namespace Private {
|
|
|
1595
1929
|
// No matches found.
|
|
1596
1930
|
return defaultName;
|
|
1597
1931
|
}
|
|
1598
|
-
|
|
1599
|
-
/**
|
|
1600
|
-
* Populate a kernel select node for the session.
|
|
1601
|
-
*/
|
|
1602
|
-
export function populateKernelSelect(
|
|
1603
|
-
node: HTMLSelectElement,
|
|
1604
|
-
options: SessionContext.IKernelSearch,
|
|
1605
|
-
translator?: ITranslator,
|
|
1606
|
-
currentKernelDisplayName: string | null = null
|
|
1607
|
-
): void {
|
|
1608
|
-
while (node.firstChild) {
|
|
1609
|
-
node.removeChild(node.firstChild);
|
|
1610
|
-
}
|
|
1611
|
-
|
|
1612
|
-
const { preference, sessions, specs } = options;
|
|
1613
|
-
const { name, id, language, canStart, shouldStart } = preference;
|
|
1614
|
-
|
|
1615
|
-
translator = translator || nullTranslator;
|
|
1616
|
-
const trans = translator.load('jupyterlab');
|
|
1617
|
-
|
|
1618
|
-
if (!specs || canStart === false) {
|
|
1619
|
-
node.appendChild(optionForNone(translator));
|
|
1620
|
-
node.value = 'null';
|
|
1621
|
-
node.disabled = true;
|
|
1622
|
-
return;
|
|
1623
|
-
}
|
|
1624
|
-
|
|
1625
|
-
node.disabled = false;
|
|
1626
|
-
|
|
1627
|
-
// Create mappings of display names and languages for kernel name.
|
|
1628
|
-
const displayNames: { [key: string]: string } = Object.create(null);
|
|
1629
|
-
const languages: { [key: string]: string } = Object.create(null);
|
|
1630
|
-
for (const name in specs.kernelspecs) {
|
|
1631
|
-
const spec = specs.kernelspecs[name]!;
|
|
1632
|
-
displayNames[name] = spec.display_name;
|
|
1633
|
-
languages[name] = spec.language;
|
|
1634
|
-
}
|
|
1635
|
-
|
|
1636
|
-
// Handle a kernel by name.
|
|
1637
|
-
const names: string[] = [];
|
|
1638
|
-
if (name && name in specs.kernelspecs) {
|
|
1639
|
-
names.push(name);
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
// Then look by language if we have a selected and existing kernel.
|
|
1643
|
-
if (name && names.length > 0 && language) {
|
|
1644
|
-
for (const specName in specs.kernelspecs) {
|
|
1645
|
-
if (name !== specName && languages[specName] === language) {
|
|
1646
|
-
names.push(specName);
|
|
1647
|
-
}
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
|
|
1651
|
-
// Use the default kernel if no kernels were found.
|
|
1652
|
-
if (!names.length) {
|
|
1653
|
-
names.push(specs.default);
|
|
1654
|
-
}
|
|
1655
|
-
|
|
1656
|
-
// Handle a preferred kernels in order of display name.
|
|
1657
|
-
const preferred = document.createElement('optgroup');
|
|
1658
|
-
preferred.label = trans.__('Start Preferred Kernel');
|
|
1659
|
-
|
|
1660
|
-
names.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
1661
|
-
for (const name of names) {
|
|
1662
|
-
preferred.appendChild(optionForName(name, displayNames[name]));
|
|
1663
|
-
}
|
|
1664
|
-
|
|
1665
|
-
if (preferred.firstChild) {
|
|
1666
|
-
node.appendChild(preferred);
|
|
1667
|
-
}
|
|
1668
|
-
|
|
1669
|
-
// Add an option for no kernel
|
|
1670
|
-
node.appendChild(optionForNone(translator));
|
|
1671
|
-
|
|
1672
|
-
const other = document.createElement('optgroup');
|
|
1673
|
-
other.label = trans.__('Start Other Kernel');
|
|
1674
|
-
|
|
1675
|
-
// Add the rest of the kernel names in alphabetical order.
|
|
1676
|
-
const otherNames: string[] = [];
|
|
1677
|
-
for (const specName in specs.kernelspecs) {
|
|
1678
|
-
if (names.indexOf(specName) !== -1) {
|
|
1679
|
-
continue;
|
|
1680
|
-
}
|
|
1681
|
-
otherNames.push(specName);
|
|
1682
|
-
}
|
|
1683
|
-
otherNames.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
|
|
1684
|
-
for (const otherName of otherNames) {
|
|
1685
|
-
other.appendChild(optionForName(otherName, displayNames[otherName]));
|
|
1686
|
-
}
|
|
1687
|
-
// Add a separator option if there were any other names.
|
|
1688
|
-
if (otherNames.length) {
|
|
1689
|
-
node.appendChild(other);
|
|
1690
|
-
}
|
|
1691
|
-
|
|
1692
|
-
// Handle the default value.
|
|
1693
|
-
if (shouldStart === false) {
|
|
1694
|
-
node.value = 'null';
|
|
1695
|
-
} else {
|
|
1696
|
-
let selectedIndex = 0;
|
|
1697
|
-
if (currentKernelDisplayName) {
|
|
1698
|
-
// Select current kernel by default.
|
|
1699
|
-
selectedIndex = [...node.options].findIndex(
|
|
1700
|
-
option => option.text === currentKernelDisplayName
|
|
1701
|
-
);
|
|
1702
|
-
selectedIndex = Math.max(selectedIndex, 0);
|
|
1703
|
-
}
|
|
1704
|
-
node.selectedIndex = selectedIndex;
|
|
1705
|
-
}
|
|
1706
|
-
|
|
1707
|
-
// Bail if there are no sessions.
|
|
1708
|
-
if (!sessions) {
|
|
1709
|
-
return;
|
|
1710
|
-
}
|
|
1711
|
-
|
|
1712
|
-
// Add the sessions using the preferred language first.
|
|
1713
|
-
const matchingSessions: Session.IModel[] = [];
|
|
1714
|
-
const otherSessions: Session.IModel[] = [];
|
|
1715
|
-
|
|
1716
|
-
for (const session of sessions) {
|
|
1717
|
-
if (
|
|
1718
|
-
language &&
|
|
1719
|
-
session.kernel &&
|
|
1720
|
-
languages[session.kernel.name] === language &&
|
|
1721
|
-
session.kernel.id !== id
|
|
1722
|
-
) {
|
|
1723
|
-
matchingSessions.push(session);
|
|
1724
|
-
} else if (session.kernel?.id !== id) {
|
|
1725
|
-
otherSessions.push(session);
|
|
1726
|
-
}
|
|
1727
|
-
}
|
|
1728
|
-
|
|
1729
|
-
const matching = document.createElement('optgroup');
|
|
1730
|
-
matching.label = trans.__('Use Kernel from Preferred Session');
|
|
1731
|
-
node.appendChild(matching);
|
|
1732
|
-
|
|
1733
|
-
if (matchingSessions.length) {
|
|
1734
|
-
matchingSessions.sort((a, b) => {
|
|
1735
|
-
return a.path.localeCompare(b.path);
|
|
1736
|
-
});
|
|
1737
|
-
|
|
1738
|
-
for (const session of matchingSessions) {
|
|
1739
|
-
const name = session.kernel ? displayNames[session.kernel.name] : '';
|
|
1740
|
-
matching.appendChild(optionForSession(session, name, translator));
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1743
|
-
|
|
1744
|
-
const otherSessionsNode = document.createElement('optgroup');
|
|
1745
|
-
otherSessionsNode.label = trans.__('Use Kernel from Other Session');
|
|
1746
|
-
node.appendChild(otherSessionsNode);
|
|
1747
|
-
|
|
1748
|
-
if (otherSessions.length) {
|
|
1749
|
-
otherSessions.sort((a, b) => {
|
|
1750
|
-
return a.path.localeCompare(b.path);
|
|
1751
|
-
});
|
|
1752
|
-
|
|
1753
|
-
for (const session of otherSessions) {
|
|
1754
|
-
const name = session.kernel
|
|
1755
|
-
? displayNames[session.kernel.name] || session.kernel.name
|
|
1756
|
-
: '';
|
|
1757
|
-
otherSessionsNode.appendChild(
|
|
1758
|
-
optionForSession(session, name, translator)
|
|
1759
|
-
);
|
|
1760
|
-
}
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
|
|
1764
|
-
/**
|
|
1765
|
-
* Get the kernel search options given a session context and session manager.
|
|
1766
|
-
*/
|
|
1767
|
-
function getKernelSearch(
|
|
1768
|
-
sessionContext: ISessionContext
|
|
1769
|
-
): SessionContext.IKernelSearch {
|
|
1770
|
-
return {
|
|
1771
|
-
specs: sessionContext.specsManager.specs,
|
|
1772
|
-
sessions: sessionContext.sessionManager.running(),
|
|
1773
|
-
preference: sessionContext.kernelPreference
|
|
1774
|
-
};
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
/**
|
|
1778
|
-
* Create an option element for a kernel name.
|
|
1779
|
-
*/
|
|
1780
|
-
function optionForName(name: string, displayName: string): HTMLOptionElement {
|
|
1781
|
-
const option = document.createElement('option');
|
|
1782
|
-
option.text = displayName;
|
|
1783
|
-
option.value = JSON.stringify({ name });
|
|
1784
|
-
return option;
|
|
1785
|
-
}
|
|
1786
|
-
|
|
1787
|
-
/**
|
|
1788
|
-
* Create an option for no kernel.
|
|
1789
|
-
*/
|
|
1790
|
-
function optionForNone(translator?: ITranslator): HTMLOptGroupElement {
|
|
1791
|
-
translator = translator || nullTranslator;
|
|
1792
|
-
const trans = translator.load('jupyterlab');
|
|
1793
|
-
|
|
1794
|
-
const group = document.createElement('optgroup');
|
|
1795
|
-
group.label = trans.__('Use No Kernel');
|
|
1796
|
-
const option = document.createElement('option');
|
|
1797
|
-
option.text = trans.__('No Kernel');
|
|
1798
|
-
option.value = 'null';
|
|
1799
|
-
group.appendChild(option);
|
|
1800
|
-
return group;
|
|
1801
|
-
}
|
|
1802
|
-
|
|
1803
|
-
/**
|
|
1804
|
-
* Create an option element for a session.
|
|
1805
|
-
*/
|
|
1806
|
-
function optionForSession(
|
|
1807
|
-
session: Session.IModel,
|
|
1808
|
-
displayName: string,
|
|
1809
|
-
translator?: ITranslator
|
|
1810
|
-
): HTMLOptionElement {
|
|
1811
|
-
translator = translator || nullTranslator;
|
|
1812
|
-
const trans = translator.load('jupyterlab');
|
|
1813
|
-
|
|
1814
|
-
const option = document.createElement('option');
|
|
1815
|
-
const sessionName = session.name || PathExt.basename(session.path);
|
|
1816
|
-
option.text = sessionName;
|
|
1817
|
-
option.value = JSON.stringify({ id: session.kernel?.id });
|
|
1818
|
-
option.title =
|
|
1819
|
-
`${trans.__('Path:')} ${session.path}\n` +
|
|
1820
|
-
`${trans.__('Name:')} ${sessionName}\n` +
|
|
1821
|
-
`${trans.__('Kernel Name:')} ${displayName}\n` +
|
|
1822
|
-
`${trans.__('Kernel Id:')} ${session.kernel?.id}`;
|
|
1823
|
-
return option;
|
|
1824
|
-
}
|
|
1825
1932
|
}
|
package/src/testutils.ts
CHANGED
|
@@ -18,6 +18,7 @@ export async function createSessionContext(
|
|
|
18
18
|
|
|
19
19
|
await Promise.all([manager.ready, specsManager.ready]);
|
|
20
20
|
return new SessionContext({
|
|
21
|
+
kernelManager: options.kernelManager ?? Private.getManager().kernels,
|
|
21
22
|
sessionManager: manager,
|
|
22
23
|
specsManager,
|
|
23
24
|
path: options.path ?? UUID.uuid4(),
|