@jupyterlab/apputils 4.8.0-beta.2 → 4.8.0-rc.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.
@@ -306,6 +306,21 @@ export namespace ISessionContext {
306
306
  * Skip showing the kernel restart dialog if checked (default `false`).
307
307
  */
308
308
  readonly skipKernelRestartDialog?: boolean;
309
+
310
+ /**
311
+ * Lock the kernel choice to the kernel named by `name`.
312
+ *
313
+ * #### Notes
314
+ * When `true`, the automatic kernel start and a restart after a shutdown
315
+ * use only `name`, without falling back to `language` or to the server
316
+ * default, and the kernel name toolbar item and the kernel selection
317
+ * dialog keep users from changing the kernel. Calls to `changeKernel()`
318
+ * are not blocked. When `undefined`, the kernel selection dialog does not
319
+ * show the lock checkbox.
320
+ *
321
+ * @experimental
322
+ */
323
+ readonly locked?: boolean;
309
324
  }
310
325
 
311
326
  export type KernelDisplayStatus =
@@ -815,6 +830,10 @@ export class SessionContext implements ISessionContext {
815
830
  * #### Notes
816
831
  * If a server session exists on the current path, we will connect to it
817
832
  * unless `kernelPreference.shouldReuse === false`.
833
+ * If `kernelPreference.locked` is `true` and that session runs another
834
+ * kernel, we ask the user before we switch it to the locked kernel.
835
+ * When the locked kernel is not installed, we do not connect to that
836
+ * session and resolve with `true`.
818
837
  * If preferences include disabling `canStart` or `shouldStart`, no
819
838
  * server session will be started.
820
839
  * If a kernel id is given, we attempt to start a session with that id.
@@ -854,6 +873,25 @@ export class SessionContext implements ISessionContext {
854
873
  return item.path === this._path;
855
874
  });
856
875
  if (model) {
876
+ const lockedName = preference.locked === true ? preference.name : '';
877
+ const mismatch = !!lockedName && model.kernel?.name !== lockedName;
878
+ let switchKernel = mismatch;
879
+ if (mismatch) {
880
+ await this.specsManager.ready;
881
+ if (!this.specsManager.specs?.kernelspecs[lockedName!]) {
882
+ return true;
883
+ }
884
+ // Switching shuts down the running kernel, so ask first.
885
+ if (model.kernel) {
886
+ switchKernel = await this._confirmLockedKernelSwitch(
887
+ lockedName!,
888
+ model.kernel.name
889
+ );
890
+ }
891
+ if (this.isDisposed) {
892
+ return false;
893
+ }
894
+ }
857
895
  try {
858
896
  const session = manager.connectTo({ model });
859
897
  this._handleNewSession(session);
@@ -861,6 +899,13 @@ export class SessionContext implements ISessionContext {
861
899
  void this._handleSessionError(err as ServerConnection.ResponseError);
862
900
  return Promise.reject(err);
863
901
  }
902
+ if (switchKernel) {
903
+ try {
904
+ await this._changeKernel({ name: lockedName });
905
+ } catch (err) {
906
+ /* no-op */
907
+ }
908
+ }
864
909
  }
865
910
  }
866
911
 
@@ -1115,6 +1160,41 @@ export class SessionContext implements ISessionContext {
1115
1160
  this._dialog = null;
1116
1161
  }
1117
1162
 
1163
+ /**
1164
+ * Ask whether to switch the kernel of a reused session to the locked kernel.
1165
+ *
1166
+ * @returns Whether the user chose to switch.
1167
+ */
1168
+ private async _confirmLockedKernelSwitch(
1169
+ lockedName: string,
1170
+ runningName: string
1171
+ ): Promise<boolean> {
1172
+ const specs = this.specsManager.specs?.kernelspecs;
1173
+ const locked = specs?.[lockedName]?.display_name ?? lockedName;
1174
+ const running = specs?.[runningName]?.display_name ?? runningName;
1175
+ const dialog = (this._dialog = new Dialog({
1176
+ title: this._trans.__('Switch to the Locked Kernel?'),
1177
+ body: this._trans.__(
1178
+ 'This notebook is locked to the %1 kernel, but the %2 kernel is already running for it. Switching shuts down the %2 kernel, and its variables are lost.',
1179
+ locked,
1180
+ running
1181
+ ),
1182
+ buttons: [
1183
+ Dialog.cancelButton({ label: this._trans.__('Keep Running Kernel') }),
1184
+ Dialog.warnButton({ label: this._trans.__('Switch Kernel') })
1185
+ ]
1186
+ }));
1187
+ try {
1188
+ const result = await dialog.launch();
1189
+ return result.button.accept;
1190
+ } catch {
1191
+ // Disposing the session context disposes the dialog.
1192
+ return false;
1193
+ } finally {
1194
+ this._dialog = null;
1195
+ }
1196
+ }
1197
+
1118
1198
  /**
1119
1199
  * Handle a session termination.
1120
1200
  */
@@ -1412,6 +1492,24 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1412
1492
  const translator = this._translator;
1413
1493
  const trans = translator.load('jupyterlab');
1414
1494
 
1495
+ const name = sessionContext.kernelPreference.name;
1496
+ // A lock without a kernel name applies to the kernel chosen in this dialog.
1497
+ const wasLocked = sessionContext.kernelPreference.locked === true && !!name;
1498
+ if (wasLocked) {
1499
+ const specs = sessionContext.specsManager.specs;
1500
+ if (sessionContext.hasNoKernel && !specs?.kernelspecs[name]) {
1501
+ await showDialog({
1502
+ title: trans.__('Kernel Unavailable'),
1503
+ body: trans.__(
1504
+ 'This notebook requires the %1 kernel, which is not available.',
1505
+ name
1506
+ ),
1507
+ buttons: [Dialog.okButton({ label: trans.__('Ok') })]
1508
+ });
1509
+ return;
1510
+ }
1511
+ }
1512
+
1415
1513
  // If there is no existing kernel, offer the option to keep no kernel.
1416
1514
  let label = trans.__('Cancel');
1417
1515
  if (sessionContext.hasNoKernel) {
@@ -1425,22 +1523,10 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1425
1523
  })
1426
1524
  ];
1427
1525
 
1428
- const autoStartDefault = sessionContext.kernelPreference.autoStartDefault;
1429
- const hasCheckbox = typeof autoStartDefault === 'boolean';
1430
-
1431
1526
  const dialog = new Dialog({
1432
1527
  title: trans.__('Select Kernel'),
1433
1528
  body: Private.createKernelSelector(sessionContext, translator),
1434
- buttons,
1435
- checkbox: hasCheckbox
1436
- ? {
1437
- label: trans.__('Always start the preferred kernel'),
1438
- caption: trans.__(
1439
- 'Remember my choice and always start the preferred kernel'
1440
- ),
1441
- checked: autoStartDefault
1442
- }
1443
- : null
1529
+ buttons
1444
1530
  });
1445
1531
 
1446
1532
  const result = await dialog.launch();
@@ -1449,14 +1535,35 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1449
1535
  return;
1450
1536
  }
1451
1537
 
1452
- if (hasCheckbox && result.isChecked !== null) {
1538
+ const { model, autoStartDefault, locked } = result.value ?? {
1539
+ model: null,
1540
+ autoStartDefault: null,
1541
+ locked: null
1542
+ };
1543
+
1544
+ if (autoStartDefault !== null) {
1545
+ sessionContext.kernelPreference = {
1546
+ ...sessionContext.kernelPreference,
1547
+ autoStartDefault
1548
+ };
1549
+ }
1550
+ if (locked !== null) {
1453
1551
  sessionContext.kernelPreference = {
1454
1552
  ...sessionContext.kernelPreference,
1455
- autoStartDefault: result.isChecked
1553
+ locked
1456
1554
  };
1457
1555
  }
1458
1556
 
1459
- const model = result.value;
1557
+ // A notebook with a locked kernel can only start its required kernel when none is running.
1558
+ if (wasLocked && locked === true) {
1559
+ const startsRequired =
1560
+ sessionContext.hasNoKernel &&
1561
+ model?.name === sessionContext.kernelPreference.name;
1562
+ if (!startsRequired) {
1563
+ return;
1564
+ }
1565
+ }
1566
+
1460
1567
  if (model === null && !sessionContext.hasNoKernel) {
1461
1568
  return sessionContext.shutdown();
1462
1569
  }
@@ -1473,7 +1580,7 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1473
1580
  * #### Notes
1474
1581
  * If there is a running kernel, present a dialog.
1475
1582
  * If there is no kernel, we start a kernel with the last run
1476
- * kernel name and resolves with `true`.
1583
+ * kernel name (or the locked kernel name) and resolves with `true`.
1477
1584
  */
1478
1585
  async restart(
1479
1586
  sessionContext: ISessionContext,
@@ -1486,10 +1593,13 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
1486
1593
  throw new Error('session already disposed');
1487
1594
  }
1488
1595
  const kernel = sessionContext.session?.kernel;
1489
- if (!kernel && sessionContext.prevKernelName) {
1490
- await sessionContext.changeKernel({
1491
- name: sessionContext.prevKernelName
1492
- });
1596
+ // Ensure a locked notebook only start its locked kernel.
1597
+ const kernelName =
1598
+ sessionContext.kernelPreference.locked === true
1599
+ ? sessionContext.kernelPreference.name
1600
+ : sessionContext.prevKernelName;
1601
+ if (!kernel && kernelName) {
1602
+ await sessionContext.changeKernel({ name: kernelName });
1493
1603
  return true;
1494
1604
  }
1495
1605
  // Bail if there is no previous kernel to start.
@@ -1870,9 +1980,23 @@ namespace Private {
1870
1980
  /**
1871
1981
  * Get the value of the kernel selector widget.
1872
1982
  */
1873
- getValue(): Kernel.IModel {
1983
+ getValue(): {
1984
+ model: Kernel.IModel;
1985
+ autoStartDefault: boolean | null;
1986
+ locked: boolean | null;
1987
+ } {
1874
1988
  const selector = this.node.querySelector<HTMLSelectElement>('select')!;
1875
- return JSON.parse(selector.value) as Kernel.IModel;
1989
+ const autoStartCheckbox = this.node.querySelector<HTMLInputElement>(
1990
+ 'input[data-role="autoStartDefault"]'
1991
+ );
1992
+ const lockCheckbox = this.node.querySelector<HTMLInputElement>(
1993
+ 'input[data-role="kernelLock"]'
1994
+ );
1995
+ return {
1996
+ model: JSON.parse(selector.value) as Kernel.IModel,
1997
+ autoStartDefault: autoStartCheckbox ? autoStartCheckbox.checked : null,
1998
+ locked: lockCheckbox ? lockCheckbox.checked : null
1999
+ };
1876
2000
  }
1877
2001
  }
1878
2002
 
@@ -1913,6 +2037,65 @@ namespace Private {
1913
2037
  select.appendChild(optgroup);
1914
2038
  }
1915
2039
  body.appendChild(select);
2040
+
2041
+ const preference = sessionContext.kernelPreference;
2042
+
2043
+ const addCheckbox = (
2044
+ role: string,
2045
+ label: string,
2046
+ caption: string,
2047
+ checked: boolean
2048
+ ): HTMLInputElement => {
2049
+ const checkboxLabel = document.createElement('label');
2050
+ checkboxLabel.className = 'jp-Dialog-checkbox';
2051
+ checkboxLabel.style.alignSelf = 'flex-start';
2052
+ checkboxLabel.title = caption;
2053
+ checkboxLabel.textContent = label;
2054
+ const input = document.createElement('input');
2055
+ input.type = 'checkbox';
2056
+ input.checked = checked;
2057
+ input.dataset.role = role;
2058
+ checkboxLabel.insertAdjacentElement('afterbegin', input);
2059
+ body.appendChild(checkboxLabel);
2060
+ return input;
2061
+ };
2062
+
2063
+ if (typeof preference.autoStartDefault === 'boolean') {
2064
+ addCheckbox(
2065
+ 'autoStartDefault',
2066
+ trans.__('Always start the preferred kernel'),
2067
+ trans.__('Remember my choice and always start the preferred kernel'),
2068
+ preference.autoStartDefault
2069
+ );
2070
+ }
2071
+
2072
+ if (typeof preference.locked === 'boolean') {
2073
+ const lockInput = addCheckbox(
2074
+ 'kernelLock',
2075
+ trans.__('Lock kernel choice'),
2076
+ trans.__('Prevent changing the kernel for this notebook'),
2077
+ preference.locked
2078
+ );
2079
+ // Do not let user lock the kernel choice for "No Kernel".
2080
+ const updateLock = () => {
2081
+ const noKernel = select.value === 'null';
2082
+ lockInput.disabled = noKernel;
2083
+ if (noKernel) {
2084
+ lockInput.checked = preference.locked === true;
2085
+ }
2086
+ };
2087
+ select.addEventListener('change', updateLock);
2088
+ updateLock();
2089
+ if (preference.locked && preference.name) {
2090
+ // A choice in the list is ignored while the lock stays checked.
2091
+ const updateSelect = () => {
2092
+ select.disabled = options.disabled || lockInput.checked;
2093
+ };
2094
+ lockInput.addEventListener('change', updateSelect);
2095
+ updateSelect();
2096
+ }
2097
+ }
2098
+
1916
2099
  return body;
1917
2100
  }
1918
2101
 
@@ -1923,7 +2106,7 @@ namespace Private {
1923
2106
  options: SessionContext.IKernelSearch
1924
2107
  ): string | null {
1925
2108
  const { specs, preference } = options;
1926
- const { name, language, canStart, autoStartDefault } = preference;
2109
+ const { name, language, canStart, autoStartDefault, locked } = preference;
1927
2110
 
1928
2111
  if (!specs || canStart === false) {
1929
2112
  return null;
@@ -1942,6 +2125,11 @@ namespace Private {
1942
2125
  }
1943
2126
  }
1944
2127
 
2128
+ // If kernel is locked don't use other kernel.
2129
+ if (locked) {
2130
+ return null;
2131
+ }
2132
+
1945
2133
  // Bail if there is no language.
1946
2134
  if (!language) {
1947
2135
  return defaultName;
@@ -160,12 +160,27 @@ namespace Private {
160
160
  initialSender={props.sessionContext}
161
161
  >
162
162
  {sessionContext => (
163
- <ToolbarButtonComponent
164
- className={TOOLBAR_KERNEL_NAME_CLASS}
165
- onClick={callback}
166
- tooltip={trans.__('Switch kernel')}
167
- label={sessionContext?.kernelDisplayName}
168
- />
163
+ <UseSignal
164
+ signal={props.sessionContext.kernelPreferenceChanged}
165
+ initialSender={props.sessionContext}
166
+ >
167
+ {() => {
168
+ const locked =
169
+ props.sessionContext.kernelPreference.locked === true;
170
+ return (
171
+ <ToolbarButtonComponent
172
+ className={TOOLBAR_KERNEL_NAME_CLASS}
173
+ onClick={callback}
174
+ tooltip={trans.__('Switch kernel')}
175
+ disabledTooltip={trans.__(
176
+ 'The kernel is locked for this notebook'
177
+ )}
178
+ enabled={!locked}
179
+ label={sessionContext?.kernelDisplayName}
180
+ />
181
+ );
182
+ }}
183
+ </UseSignal>
169
184
  )}
170
185
  </UseSignal>
171
186
  );