@jupyterlab/apputils 4.4.0-alpha.1 → 4.4.0-beta.0

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.
@@ -9,6 +9,7 @@ import {
9
9
  ServerConnection,
10
10
  Session
11
11
  } from '@jupyterlab/services';
12
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
12
13
  import {
13
14
  ITranslator,
14
15
  nullTranslator,
@@ -196,6 +197,14 @@ export interface ISessionContext extends IObservableDisposable {
196
197
  */
197
198
  readonly prevKernelName: string;
198
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
+
199
208
  /**
200
209
  * The session manager used by the session.
201
210
  */
@@ -287,6 +296,11 @@ export namespace ISessionContext {
287
296
  * found (default `false`).
288
297
  */
289
298
  readonly autoStartDefault?: boolean;
299
+
300
+ /**
301
+ * Skip showing the kernel restart dialog if checked (default `false`).
302
+ */
303
+ readonly skipKernelRestartDialog?: boolean;
290
304
  }
291
305
 
292
306
  export type KernelDisplayStatus =
@@ -326,6 +340,10 @@ export namespace ISessionContext {
326
340
  * Application translator object
327
341
  */
328
342
  translator?: ITranslator;
343
+ /**
344
+ * Optional setting registry used to access restart dialog preference.
345
+ */
346
+ settingRegistry?: ISettingRegistry | null;
329
347
  }
330
348
  }
331
349
 
@@ -337,6 +355,7 @@ export class SessionContext implements ISessionContext {
337
355
  * Construct a new session context.
338
356
  */
339
357
  constructor(options: SessionContext.IOptions) {
358
+ this.kernelManager = options.kernelManager;
340
359
  this.sessionManager = options.sessionManager;
341
360
  this.specsManager = options.specsManager;
342
361
  this.translator = options.translator || nullTranslator;
@@ -513,6 +532,11 @@ export class SessionContext implements ISessionContext {
513
532
  return this._isRestarting;
514
533
  }
515
534
 
535
+ /**
536
+ * The kernel manager
537
+ */
538
+ readonly kernelManager?: Kernel.IManager;
539
+
516
540
  /**
517
541
  * The session manager used by the session.
518
542
  */
@@ -1237,6 +1261,14 @@ export namespace SessionContext {
1237
1261
  * The options used to initialize a context.
1238
1262
  */
1239
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
+
1240
1272
  /**
1241
1273
  * A session manager instance.
1242
1274
  */
@@ -1282,6 +1314,11 @@ export namespace SessionContext {
1282
1314
  * An interface for populating a kernel selector.
1283
1315
  */
1284
1316
  export interface IKernelSearch {
1317
+ /**
1318
+ * The current running kernels.
1319
+ */
1320
+ kernels?: Iterable<Kernel.IModel>;
1321
+
1285
1322
  /**
1286
1323
  * The Kernel specs.
1287
1324
  */
@@ -1319,6 +1356,7 @@ export namespace SessionContext {
1319
1356
  export class SessionContextDialogs implements ISessionContext.IDialogs {
1320
1357
  constructor(options: ISessionContext.IDialogsOptions = {}) {
1321
1358
  this._translator = options.translator ?? nullTranslator;
1359
+ this._settingRegistry = options.settingRegistry || null;
1322
1360
  }
1323
1361
 
1324
1362
  /**
@@ -1328,18 +1366,17 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1328
1366
  if (sessionContext.isDisposed) {
1329
1367
  return Promise.resolve();
1330
1368
  }
1331
- const trans = this._translator.load('jupyterlab');
1332
1369
 
1333
- // If there is no existing kernel, offer the option
1334
- // to keep no kernel.
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.
1335
1374
  let label = trans.__('Cancel');
1336
1375
  if (sessionContext.hasNoKernel) {
1337
1376
  label = sessionContext.kernelDisplayName;
1338
1377
  }
1339
1378
  const buttons = [
1340
- Dialog.cancelButton({
1341
- label
1342
- }),
1379
+ Dialog.cancelButton({ label }),
1343
1380
  Dialog.okButton({
1344
1381
  label: trans.__('Select'),
1345
1382
  ariaLabel: trans.__('Select Kernel')
@@ -1351,7 +1388,7 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1351
1388
 
1352
1389
  const dialog = new Dialog({
1353
1390
  title: trans.__('Select Kernel'),
1354
- body: new Private.KernelSelector(sessionContext, this._translator),
1391
+ body: Private.createKernelSelector(sessionContext, translator),
1355
1392
  buttons,
1356
1393
  checkbox: hasCheckbox
1357
1394
  ? {
@@ -1415,6 +1452,21 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1415
1452
  throw new Error('No kernel to restart');
1416
1453
  }
1417
1454
 
1455
+ // Skip the dialog and restart the kernel
1456
+ const kernelPluginId = '@jupyterlab/apputils-extension:sessionDialogs';
1457
+ const skipKernelRestartDialog =
1458
+ sessionContext.kernelPreference?.skipKernelRestartDialog ?? false;
1459
+ const skipKernelRestartDialogSetting = (
1460
+ await this._settingRegistry?.get(
1461
+ kernelPluginId,
1462
+ 'skipKernelRestartDialog'
1463
+ )
1464
+ )?.composite as boolean;
1465
+ if (skipKernelRestartDialogSetting || skipKernelRestartDialog) {
1466
+ await sessionContext.restartKernel();
1467
+ return true;
1468
+ }
1469
+
1418
1470
  const restartBtn = Dialog.warnButton({
1419
1471
  label: trans.__('Restart'),
1420
1472
  ariaLabel: trans.__('Confirm Kernel Restart')
@@ -1428,13 +1480,26 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1428
1480
  buttons: [
1429
1481
  Dialog.cancelButton({ ariaLabel: trans.__('Cancel Kernel Restart') }),
1430
1482
  restartBtn
1431
- ]
1483
+ ],
1484
+ checkbox: {
1485
+ label: trans.__('Do not ask me again.'),
1486
+ caption: trans.__(
1487
+ 'If checked, the kernel will restart without confirmation prompt in the future; you can change this back in the settings.'
1488
+ )
1489
+ }
1432
1490
  });
1433
1491
 
1434
1492
  if (kernel.isDisposed) {
1435
1493
  return false;
1436
1494
  }
1437
1495
  if (result.button.accept) {
1496
+ if (typeof result.isChecked === 'boolean' && result.isChecked == true) {
1497
+ sessionContext.kernelPreference = {
1498
+ ...sessionContext.kernelPreference,
1499
+ skipKernelRestartDialog: true
1500
+ };
1501
+ }
1502
+
1438
1503
  await sessionContext.restartKernel();
1439
1504
  return true;
1440
1505
  }
@@ -1442,6 +1507,299 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1442
1507
  }
1443
1508
 
1444
1509
  private _translator: ITranslator;
1510
+ private _settingRegistry: ISettingRegistry | null;
1511
+ }
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
+ }
1445
1803
  }
1446
1804
 
1447
1805
  /**
@@ -1449,16 +1807,20 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1449
1807
  */
1450
1808
  namespace Private {
1451
1809
  /**
1452
- * A widget that provides a kernel selection.
1810
+ * Return a kernel selector widget.
1453
1811
  */
1454
- export class KernelSelector extends Widget {
1455
- /**
1456
- * Create a new kernel selector widget.
1457
- */
1458
- constructor(sessionContext: ISessionContext, translator?: ITranslator) {
1459
- super({ node: createSelectorNode(sessionContext, translator) });
1460
- }
1812
+ export const createKernelSelector = (
1813
+ sessionContext: ISessionContext,
1814
+ translator?: ITranslator
1815
+ ) =>
1816
+ new KernelSelector({
1817
+ node: createSelectorNode(sessionContext, translator)
1818
+ });
1461
1819
 
1820
+ /**
1821
+ * A widget that provides a kernel selection.
1822
+ */
1823
+ class KernelSelector extends Widget {
1462
1824
  /**
1463
1825
  * Get the value of the kernel selector widget.
1464
1826
  */
@@ -1469,7 +1831,7 @@ namespace Private {
1469
1831
  }
1470
1832
 
1471
1833
  /**
1472
- * Create a node for a kernel selector widget.
1834
+ * Create an HTML node for a kernel selector widget.
1473
1835
  */
1474
1836
  function createSelectorNode(
1475
1837
  sessionContext: ISessionContext,
@@ -1486,15 +1848,27 @@ namespace Private {
1486
1848
  }"`;
1487
1849
  body.appendChild(text);
1488
1850
 
1489
- const options = getKernelSearch(sessionContext);
1490
- const selector = document.createElement('select');
1491
- populateKernelSelect(
1492
- selector,
1493
- options,
1494
- translator,
1495
- !sessionContext.hasNoKernel ? sessionContext.kernelDisplayName : null
1851
+ const select = document.createElement('select');
1852
+ const options = SessionContextDialogs.kernelOptions(
1853
+ sessionContext,
1854
+ translator
1496
1855
  );
1497
- body.appendChild(selector);
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);
1498
1872
  return body;
1499
1873
  }
1500
1874
 
@@ -1555,231 +1929,4 @@ namespace Private {
1555
1929
  // No matches found.
1556
1930
  return defaultName;
1557
1931
  }
1558
-
1559
- /**
1560
- * Populate a kernel select node for the session.
1561
- */
1562
- export function populateKernelSelect(
1563
- node: HTMLSelectElement,
1564
- options: SessionContext.IKernelSearch,
1565
- translator?: ITranslator,
1566
- currentKernelDisplayName: string | null = null
1567
- ): void {
1568
- while (node.firstChild) {
1569
- node.removeChild(node.firstChild);
1570
- }
1571
-
1572
- const { preference, sessions, specs } = options;
1573
- const { name, id, language, canStart, shouldStart } = preference;
1574
-
1575
- translator = translator || nullTranslator;
1576
- const trans = translator.load('jupyterlab');
1577
-
1578
- if (!specs || canStart === false) {
1579
- node.appendChild(optionForNone(translator));
1580
- node.value = 'null';
1581
- node.disabled = true;
1582
- return;
1583
- }
1584
-
1585
- node.disabled = false;
1586
-
1587
- // Create mappings of display names and languages for kernel name.
1588
- const displayNames: { [key: string]: string } = Object.create(null);
1589
- const languages: { [key: string]: string } = Object.create(null);
1590
- for (const name in specs.kernelspecs) {
1591
- const spec = specs.kernelspecs[name]!;
1592
- displayNames[name] = spec.display_name;
1593
- languages[name] = spec.language;
1594
- }
1595
-
1596
- // Handle a kernel by name.
1597
- const names: string[] = [];
1598
- if (name && name in specs.kernelspecs) {
1599
- names.push(name);
1600
- }
1601
-
1602
- // Then look by language if we have a selected and existing kernel.
1603
- if (name && names.length > 0 && language) {
1604
- for (const specName in specs.kernelspecs) {
1605
- if (name !== specName && languages[specName] === language) {
1606
- names.push(specName);
1607
- }
1608
- }
1609
- }
1610
-
1611
- // Use the default kernel if no kernels were found.
1612
- if (!names.length) {
1613
- names.push(specs.default);
1614
- }
1615
-
1616
- // Handle a preferred kernels in order of display name.
1617
- const preferred = document.createElement('optgroup');
1618
- preferred.label = trans.__('Start Preferred Kernel');
1619
-
1620
- names.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
1621
- for (const name of names) {
1622
- preferred.appendChild(optionForName(name, displayNames[name]));
1623
- }
1624
-
1625
- if (preferred.firstChild) {
1626
- node.appendChild(preferred);
1627
- }
1628
-
1629
- // Add an option for no kernel
1630
- node.appendChild(optionForNone(translator));
1631
-
1632
- const other = document.createElement('optgroup');
1633
- other.label = trans.__('Start Other Kernel');
1634
-
1635
- // Add the rest of the kernel names in alphabetical order.
1636
- const otherNames: string[] = [];
1637
- for (const specName in specs.kernelspecs) {
1638
- if (names.indexOf(specName) !== -1) {
1639
- continue;
1640
- }
1641
- otherNames.push(specName);
1642
- }
1643
- otherNames.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
1644
- for (const otherName of otherNames) {
1645
- other.appendChild(optionForName(otherName, displayNames[otherName]));
1646
- }
1647
- // Add a separator option if there were any other names.
1648
- if (otherNames.length) {
1649
- node.appendChild(other);
1650
- }
1651
-
1652
- // Handle the default value.
1653
- if (shouldStart === false) {
1654
- node.value = 'null';
1655
- } else {
1656
- let selectedIndex = 0;
1657
- if (currentKernelDisplayName) {
1658
- // Select current kernel by default.
1659
- selectedIndex = [...node.options].findIndex(
1660
- option => option.text === currentKernelDisplayName
1661
- );
1662
- selectedIndex = Math.max(selectedIndex, 0);
1663
- }
1664
- node.selectedIndex = selectedIndex;
1665
- }
1666
-
1667
- // Bail if there are no sessions.
1668
- if (!sessions) {
1669
- return;
1670
- }
1671
-
1672
- // Add the sessions using the preferred language first.
1673
- const matchingSessions: Session.IModel[] = [];
1674
- const otherSessions: Session.IModel[] = [];
1675
-
1676
- for (const session of sessions) {
1677
- if (
1678
- language &&
1679
- session.kernel &&
1680
- languages[session.kernel.name] === language &&
1681
- session.kernel.id !== id
1682
- ) {
1683
- matchingSessions.push(session);
1684
- } else if (session.kernel?.id !== id) {
1685
- otherSessions.push(session);
1686
- }
1687
- }
1688
-
1689
- const matching = document.createElement('optgroup');
1690
- matching.label = trans.__('Use Kernel from Preferred Session');
1691
- node.appendChild(matching);
1692
-
1693
- if (matchingSessions.length) {
1694
- matchingSessions.sort((a, b) => {
1695
- return a.path.localeCompare(b.path);
1696
- });
1697
-
1698
- for (const session of matchingSessions) {
1699
- const name = session.kernel ? displayNames[session.kernel.name] : '';
1700
- matching.appendChild(optionForSession(session, name, translator));
1701
- }
1702
- }
1703
-
1704
- const otherSessionsNode = document.createElement('optgroup');
1705
- otherSessionsNode.label = trans.__('Use Kernel from Other Session');
1706
- node.appendChild(otherSessionsNode);
1707
-
1708
- if (otherSessions.length) {
1709
- otherSessions.sort((a, b) => {
1710
- return a.path.localeCompare(b.path);
1711
- });
1712
-
1713
- for (const session of otherSessions) {
1714
- const name = session.kernel
1715
- ? displayNames[session.kernel.name] || session.kernel.name
1716
- : '';
1717
- otherSessionsNode.appendChild(
1718
- optionForSession(session, name, translator)
1719
- );
1720
- }
1721
- }
1722
- }
1723
-
1724
- /**
1725
- * Get the kernel search options given a session context and session manager.
1726
- */
1727
- function getKernelSearch(
1728
- sessionContext: ISessionContext
1729
- ): SessionContext.IKernelSearch {
1730
- return {
1731
- specs: sessionContext.specsManager.specs,
1732
- sessions: sessionContext.sessionManager.running(),
1733
- preference: sessionContext.kernelPreference
1734
- };
1735
- }
1736
-
1737
- /**
1738
- * Create an option element for a kernel name.
1739
- */
1740
- function optionForName(name: string, displayName: string): HTMLOptionElement {
1741
- const option = document.createElement('option');
1742
- option.text = displayName;
1743
- option.value = JSON.stringify({ name });
1744
- return option;
1745
- }
1746
-
1747
- /**
1748
- * Create an option for no kernel.
1749
- */
1750
- function optionForNone(translator?: ITranslator): HTMLOptGroupElement {
1751
- translator = translator || nullTranslator;
1752
- const trans = translator.load('jupyterlab');
1753
-
1754
- const group = document.createElement('optgroup');
1755
- group.label = trans.__('Use No Kernel');
1756
- const option = document.createElement('option');
1757
- option.text = trans.__('No Kernel');
1758
- option.value = 'null';
1759
- group.appendChild(option);
1760
- return group;
1761
- }
1762
-
1763
- /**
1764
- * Create an option element for a session.
1765
- */
1766
- function optionForSession(
1767
- session: Session.IModel,
1768
- displayName: string,
1769
- translator?: ITranslator
1770
- ): HTMLOptionElement {
1771
- translator = translator || nullTranslator;
1772
- const trans = translator.load('jupyterlab');
1773
-
1774
- const option = document.createElement('option');
1775
- const sessionName = session.name || PathExt.basename(session.path);
1776
- option.text = sessionName;
1777
- option.value = JSON.stringify({ id: session.kernel?.id });
1778
- option.title =
1779
- `${trans.__('Path:')} ${session.path}\n` +
1780
- `${trans.__('Name:')} ${sessionName}\n` +
1781
- `${trans.__('Kernel Name:')} ${displayName}\n` +
1782
- `${trans.__('Kernel Id:')} ${session.kernel?.id}`;
1783
- return option;
1784
- }
1785
1932
  }