@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.
@@ -44,6 +44,7 @@ export class SessionContext {
44
44
  this._busyDisposable = null;
45
45
  this._pendingKernelName = '';
46
46
  this._pendingSessionRequest = '';
47
+ this.kernelManager = options.kernelManager;
47
48
  this.sessionManager = options.sessionManager;
48
49
  this.specsManager = options.specsManager;
49
50
  this.translator = options.translator || nullTranslator;
@@ -779,6 +780,7 @@ export class SessionContextDialogs {
779
780
  constructor(options = {}) {
780
781
  var _a;
781
782
  this._translator = (_a = options.translator) !== null && _a !== void 0 ? _a : nullTranslator;
783
+ this._settingRegistry = options.settingRegistry || null;
782
784
  }
783
785
  /**
784
786
  * Select a kernel for the session.
@@ -787,17 +789,15 @@ export class SessionContextDialogs {
787
789
  if (sessionContext.isDisposed) {
788
790
  return Promise.resolve();
789
791
  }
790
- const trans = this._translator.load('jupyterlab');
791
- // If there is no existing kernel, offer the option
792
- // to keep no kernel.
792
+ const translator = this._translator;
793
+ const trans = translator.load('jupyterlab');
794
+ // If there is no existing kernel, offer the option to keep no kernel.
793
795
  let label = trans.__('Cancel');
794
796
  if (sessionContext.hasNoKernel) {
795
797
  label = sessionContext.kernelDisplayName;
796
798
  }
797
799
  const buttons = [
798
- Dialog.cancelButton({
799
- label
800
- }),
800
+ Dialog.cancelButton({ label }),
801
801
  Dialog.okButton({
802
802
  label: trans.__('Select'),
803
803
  ariaLabel: trans.__('Select Kernel')
@@ -807,7 +807,7 @@ export class SessionContextDialogs {
807
807
  const hasCheckbox = typeof autoStartDefault === 'boolean';
808
808
  const dialog = new Dialog({
809
809
  title: trans.__('Select Kernel'),
810
- body: new Private.KernelSelector(sessionContext, this._translator),
810
+ body: Private.createKernelSelector(sessionContext, translator),
811
811
  buttons,
812
812
  checkbox: hasCheckbox
813
813
  ? {
@@ -846,7 +846,7 @@ export class SessionContextDialogs {
846
846
  * kernel name and resolves with `true`.
847
847
  */
848
848
  async restart(sessionContext) {
849
- var _a;
849
+ var _a, _b, _c, _d, _e;
850
850
  const trans = this._translator.load('jupyterlab');
851
851
  await sessionContext.initialize();
852
852
  if (sessionContext.isDisposed) {
@@ -863,6 +863,14 @@ export class SessionContextDialogs {
863
863
  if (!kernel) {
864
864
  throw new Error('No kernel to restart');
865
865
  }
866
+ // Skip the dialog and restart the kernel
867
+ const kernelPluginId = '@jupyterlab/apputils-extension:sessionDialogs';
868
+ const skipKernelRestartDialog = (_c = (_b = sessionContext.kernelPreference) === null || _b === void 0 ? void 0 : _b.skipKernelRestartDialog) !== null && _c !== void 0 ? _c : false;
869
+ const skipKernelRestartDialogSetting = (_e = (await ((_d = this._settingRegistry) === null || _d === void 0 ? void 0 : _d.get(kernelPluginId, 'skipKernelRestartDialog')))) === null || _e === void 0 ? void 0 : _e.composite;
870
+ if (skipKernelRestartDialogSetting || skipKernelRestartDialog) {
871
+ await sessionContext.restartKernel();
872
+ return true;
873
+ }
866
874
  const restartBtn = Dialog.warnButton({
867
875
  label: trans.__('Restart'),
868
876
  ariaLabel: trans.__('Confirm Kernel Restart')
@@ -873,33 +881,257 @@ export class SessionContextDialogs {
873
881
  buttons: [
874
882
  Dialog.cancelButton({ ariaLabel: trans.__('Cancel Kernel Restart') }),
875
883
  restartBtn
876
- ]
884
+ ],
885
+ checkbox: {
886
+ label: trans.__('Do not ask me again.'),
887
+ caption: trans.__('If checked, the kernel will restart without confirmation prompt in the future; you can change this back in the settings.')
888
+ }
877
889
  });
878
890
  if (kernel.isDisposed) {
879
891
  return false;
880
892
  }
881
893
  if (result.button.accept) {
894
+ if (typeof result.isChecked === 'boolean' && result.isChecked == true) {
895
+ sessionContext.kernelPreference = {
896
+ ...sessionContext.kernelPreference,
897
+ skipKernelRestartDialog: true
898
+ };
899
+ }
882
900
  await sessionContext.restartKernel();
883
901
  return true;
884
902
  }
885
903
  return false;
886
904
  }
887
905
  }
906
+ (function (SessionContextDialogs) {
907
+ /**
908
+ * Returns available kernel options grouped based on session context.
909
+ *
910
+ * #### Notes
911
+ * If a language preference is set in the given session context, the options
912
+ * returned are grouped with the language preference at the top:
913
+ *
914
+ * - (Start %1 Kernel, language)
915
+ * - { all kernelspecs whose language matches in alphabetical order }
916
+ * - (Use No Kernel)
917
+ * - `No Kernel`
918
+ * - (Start Kernel)
919
+ * - { all other kernelspecs in alphabetical order }
920
+ * - (Connect to Existing %1 Kernel, language)
921
+ * - { all running kernels whose language matches in alphabetical order }
922
+ * - (Connect to Kernel)
923
+ * - { all other running kernels in alphabetical order }
924
+ *
925
+ * If no language preference is set, these groups and options are returned:
926
+ *
927
+ * - (Start Kernel)
928
+ * - { all kernelspecs in alphabetical order }
929
+ * - (Use No Kernel)
930
+ * - `No Kernel`
931
+ * - (Connect to Existing Kernel)
932
+ * - { all running kernels in alphabetical order }
933
+ *
934
+ * If the session has a kernel ID and a kernel exists with that id, its
935
+ * corresponding option has `selected` set to `true`. Otherwise if the session
936
+ * context language preference is set, the first kernelspec that matches it is
937
+ * selected.
938
+ */
939
+ function kernelOptions(sessionContext, translator = null) {
940
+ var _a, _b, _c, _d, _e, _f, _g;
941
+ const options = { disabled: false, groups: [] };
942
+ // Create mapping of sessions and kernel ids.
943
+ const kernels = Array.from((_b = (_a = sessionContext.kernelManager) === null || _a === void 0 ? void 0 : _a.running()) !== null && _b !== void 0 ? _b :
944
+ // If kernel manager is unavailable use kernels from running sessions.
945
+ // TODO: Remove this (next version) when kernel manager is guaranteed.
946
+ Array.from(sessionContext.sessionManager.running())
947
+ .filter(session => !!session.kernel)
948
+ .map(session => session.kernel));
949
+ const sessions = Array.from((_c = sessionContext.sessionManager.running()) !== null && _c !== void 0 ? _c : []).reduce((sessions, session) => {
950
+ var _a;
951
+ if ((_a = session.kernel) === null || _a === void 0 ? void 0 : _a.id)
952
+ sessions[session.kernel.id] = session;
953
+ return sessions;
954
+ }, {});
955
+ const preference = {
956
+ ...sessionContext.kernelPreference,
957
+ id: (_e = (_d = sessionContext.session) === null || _d === void 0 ? void 0 : _d.kernel) === null || _e === void 0 ? void 0 : _e.id
958
+ };
959
+ const currentKernelDisplayName = !sessionContext.hasNoKernel
960
+ ? sessionContext.kernelDisplayName
961
+ : null;
962
+ const specs = {
963
+ default: '',
964
+ kernelspecs: Object.create(null),
965
+ ...sessionContext.specsManager.specs
966
+ };
967
+ // Create mapping of languages and kernel names.
968
+ const sorted = [];
969
+ const languages = Object.create(null);
970
+ for (const name in specs.kernelspecs) {
971
+ sorted.push(specs.kernelspecs[name]);
972
+ languages[name] = specs.kernelspecs[name].language;
973
+ }
974
+ sorted.sort((a, b) => a.display_name.localeCompare(b.display_name));
975
+ translator = translator || nullTranslator;
976
+ const trans = translator.load('jupyterlab');
977
+ const language = preference.language ||
978
+ languages[preference.name] ||
979
+ (preference.id ? languages[(_f = sessions[preference.id]) === null || _f === void 0 ? void 0 : _f.name] : '');
980
+ const labels = {
981
+ connectKernel: trans.__('Connect to Existing Kernel'),
982
+ startPreferred: trans.__('Start %1 Kernel', language),
983
+ startOther: trans.__('Start Kernel'),
984
+ connectToPreferred: trans.__('Connect to Existing %1 Kernel', language),
985
+ connectToOther: trans.__('Connect to Other Kernel'),
986
+ noKernel: trans.__('No Kernel'),
987
+ startKernel: trans.__('Start Kernel'),
988
+ useNoKernel: trans.__('Use No Kernel')
989
+ };
990
+ const noKernel = {
991
+ label: labels.useNoKernel,
992
+ options: [
993
+ {
994
+ text: labels.noKernel,
995
+ title: labels.noKernel,
996
+ value: JSON.stringify(null)
997
+ }
998
+ ]
999
+ };
1000
+ const optionForKernel = (kernel, displayName, session) => {
1001
+ const sessionName = session
1002
+ ? session.name || PathExt.basename(session.path)
1003
+ : kernel.name || trans.__('Unknown Kernel');
1004
+ return {
1005
+ text: `${sessionName} (${kernel.id.split('-')[0]})`,
1006
+ title: (session ? `${trans.__('Path: %1', session.path)}\n` : ``) +
1007
+ `${trans.__('Name: %1', sessionName)}\n` +
1008
+ `${trans.__('Kernel Name: %1', displayName !== null && displayName !== void 0 ? displayName : kernel.name)}\n` +
1009
+ `${trans.__('Kernel Id: %1', kernel.id)}`,
1010
+ value: JSON.stringify({ id: kernel.id })
1011
+ };
1012
+ };
1013
+ const optionForSpec = (spec) => ({
1014
+ text: spec.display_name,
1015
+ value: JSON.stringify({ name: spec.name })
1016
+ });
1017
+ // If a kernel cannot be started, bail.
1018
+ if (preference.canStart === false) {
1019
+ options.disabled = true;
1020
+ options.groups.push(noKernel);
1021
+ return options;
1022
+ }
1023
+ // Create kernel option groups based on whether language preference exists.
1024
+ if (language) {
1025
+ // Add all kernelspecs, separating out the preferred language first.
1026
+ const preferred = {
1027
+ label: labels.startPreferred,
1028
+ options: []
1029
+ };
1030
+ const other = {
1031
+ label: labels.startOther,
1032
+ options: []
1033
+ };
1034
+ const preferredRunning = {
1035
+ label: labels.connectToPreferred,
1036
+ options: []
1037
+ };
1038
+ const otherRunning = {
1039
+ label: labels.connectToOther,
1040
+ options: []
1041
+ };
1042
+ for (const spec of sorted) {
1043
+ (spec.language === language ? preferred : other).options.push(optionForSpec(spec));
1044
+ }
1045
+ options.groups.push(preferred);
1046
+ options.groups.push(noKernel);
1047
+ options.groups.push(other);
1048
+ kernels
1049
+ .map(kernel => {
1050
+ var _a, _b;
1051
+ return ({
1052
+ option: optionForKernel(kernel, (_b = (_a = specs.kernelspecs[kernel.name]) === null || _a === void 0 ? void 0 : _a.display_name) !== null && _b !== void 0 ? _b : '', sessions[kernel.id]),
1053
+ language: languages[kernel.name]
1054
+ });
1055
+ })
1056
+ .sort((a, b) => a.option.text.localeCompare(b.option.text))
1057
+ .forEach(kernel => (language === kernel.language
1058
+ ? preferredRunning
1059
+ : otherRunning).options.push(kernel.option));
1060
+ if (preferredRunning.options.length)
1061
+ options.groups.push(preferredRunning);
1062
+ if (otherRunning.options.length)
1063
+ options.groups.push(otherRunning);
1064
+ }
1065
+ else {
1066
+ // Add kernelspecs first.
1067
+ options.groups.push({
1068
+ label: labels.startKernel,
1069
+ options: sorted.map(spec => optionForSpec(spec))
1070
+ });
1071
+ // Next add the no kernel option.
1072
+ options.groups.push(noKernel);
1073
+ // Add running kernels.
1074
+ options.groups.push({
1075
+ label: labels.connectKernel,
1076
+ options: kernels
1077
+ .map(kernel => {
1078
+ var _a, _b;
1079
+ return optionForKernel(kernel, (_b = (_a = specs.kernelspecs[kernel.name]) === null || _a === void 0 ? void 0 : _a.display_name) !== null && _b !== void 0 ? _b : '', sessions[kernel.id]);
1080
+ })
1081
+ .sort((a, b) => a.text.localeCompare(b.text))
1082
+ });
1083
+ }
1084
+ // Set the selected option.
1085
+ if (preference.id || currentKernelDisplayName || preference.name) {
1086
+ for (const group of options.groups) {
1087
+ for (const option of group.options) {
1088
+ const choice = JSON.parse(option.value);
1089
+ if (!choice)
1090
+ continue;
1091
+ if (preference.id) {
1092
+ if (preference.id === choice.id) {
1093
+ option.selected = true;
1094
+ return options;
1095
+ }
1096
+ continue;
1097
+ }
1098
+ if (currentKernelDisplayName) {
1099
+ if (currentKernelDisplayName ===
1100
+ ((_g = specs.kernelspecs[choice.name]) === null || _g === void 0 ? void 0 : _g.display_name)) {
1101
+ option.selected = true;
1102
+ return options;
1103
+ }
1104
+ continue;
1105
+ }
1106
+ if (preference.name) {
1107
+ if (preference.name === choice.name) {
1108
+ option.selected = true;
1109
+ return options;
1110
+ }
1111
+ continue;
1112
+ }
1113
+ }
1114
+ }
1115
+ }
1116
+ return options;
1117
+ }
1118
+ SessionContextDialogs.kernelOptions = kernelOptions;
1119
+ })(SessionContextDialogs || (SessionContextDialogs = {}));
888
1120
  /**
889
1121
  * The namespace for module private data.
890
1122
  */
891
1123
  var Private;
892
1124
  (function (Private) {
1125
+ /**
1126
+ * Return a kernel selector widget.
1127
+ */
1128
+ Private.createKernelSelector = (sessionContext, translator) => new KernelSelector({
1129
+ node: createSelectorNode(sessionContext, translator)
1130
+ });
893
1131
  /**
894
1132
  * A widget that provides a kernel selection.
895
1133
  */
896
1134
  class KernelSelector extends Widget {
897
- /**
898
- * Create a new kernel selector widget.
899
- */
900
- constructor(sessionContext, translator) {
901
- super({ node: createSelectorNode(sessionContext, translator) });
902
- }
903
1135
  /**
904
1136
  * Get the value of the kernel selector widget.
905
1137
  */
@@ -908,9 +1140,8 @@ var Private;
908
1140
  return JSON.parse(selector.value);
909
1141
  }
910
1142
  }
911
- Private.KernelSelector = KernelSelector;
912
1143
  /**
913
- * Create a node for a kernel selector widget.
1144
+ * Create an HTML node for a kernel selector widget.
914
1145
  */
915
1146
  function createSelectorNode(sessionContext, translator) {
916
1147
  // Create the dialog body.
@@ -920,10 +1151,27 @@ var Private;
920
1151
  const text = document.createElement('label');
921
1152
  text.textContent = `${trans.__('Select kernel for:')} "${sessionContext.name}"`;
922
1153
  body.appendChild(text);
923
- const options = getKernelSearch(sessionContext);
924
- const selector = document.createElement('select');
925
- populateKernelSelect(selector, options, translator, !sessionContext.hasNoKernel ? sessionContext.kernelDisplayName : null);
926
- body.appendChild(selector);
1154
+ const select = document.createElement('select');
1155
+ const options = SessionContextDialogs.kernelOptions(sessionContext, translator);
1156
+ if (options.disabled)
1157
+ select.disabled = true;
1158
+ for (const group of options.groups) {
1159
+ const { label, options } = group;
1160
+ const optgroup = document.createElement('optgroup');
1161
+ optgroup.label = label;
1162
+ for (const { selected, text, title, value } of options) {
1163
+ const option = document.createElement('option');
1164
+ if (selected)
1165
+ option.selected = true;
1166
+ if (title)
1167
+ option.title = title;
1168
+ option.text = text;
1169
+ option.value = value;
1170
+ optgroup.appendChild(option);
1171
+ }
1172
+ select.appendChild(optgroup);
1173
+ }
1174
+ body.appendChild(select);
927
1175
  return body;
928
1176
  }
929
1177
  /**
@@ -973,189 +1221,5 @@ var Private;
973
1221
  return defaultName;
974
1222
  }
975
1223
  Private.getDefaultKernel = getDefaultKernel;
976
- /**
977
- * Populate a kernel select node for the session.
978
- */
979
- function populateKernelSelect(node, options, translator, currentKernelDisplayName = null) {
980
- var _a;
981
- while (node.firstChild) {
982
- node.removeChild(node.firstChild);
983
- }
984
- const { preference, sessions, specs } = options;
985
- const { name, id, language, canStart, shouldStart } = preference;
986
- translator = translator || nullTranslator;
987
- const trans = translator.load('jupyterlab');
988
- if (!specs || canStart === false) {
989
- node.appendChild(optionForNone(translator));
990
- node.value = 'null';
991
- node.disabled = true;
992
- return;
993
- }
994
- node.disabled = false;
995
- // Create mappings of display names and languages for kernel name.
996
- const displayNames = Object.create(null);
997
- const languages = Object.create(null);
998
- for (const name in specs.kernelspecs) {
999
- const spec = specs.kernelspecs[name];
1000
- displayNames[name] = spec.display_name;
1001
- languages[name] = spec.language;
1002
- }
1003
- // Handle a kernel by name.
1004
- const names = [];
1005
- if (name && name in specs.kernelspecs) {
1006
- names.push(name);
1007
- }
1008
- // Then look by language if we have a selected and existing kernel.
1009
- if (name && names.length > 0 && language) {
1010
- for (const specName in specs.kernelspecs) {
1011
- if (name !== specName && languages[specName] === language) {
1012
- names.push(specName);
1013
- }
1014
- }
1015
- }
1016
- // Use the default kernel if no kernels were found.
1017
- if (!names.length) {
1018
- names.push(specs.default);
1019
- }
1020
- // Handle a preferred kernels in order of display name.
1021
- const preferred = document.createElement('optgroup');
1022
- preferred.label = trans.__('Start Preferred Kernel');
1023
- names.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
1024
- for (const name of names) {
1025
- preferred.appendChild(optionForName(name, displayNames[name]));
1026
- }
1027
- if (preferred.firstChild) {
1028
- node.appendChild(preferred);
1029
- }
1030
- // Add an option for no kernel
1031
- node.appendChild(optionForNone(translator));
1032
- const other = document.createElement('optgroup');
1033
- other.label = trans.__('Start Other Kernel');
1034
- // Add the rest of the kernel names in alphabetical order.
1035
- const otherNames = [];
1036
- for (const specName in specs.kernelspecs) {
1037
- if (names.indexOf(specName) !== -1) {
1038
- continue;
1039
- }
1040
- otherNames.push(specName);
1041
- }
1042
- otherNames.sort((a, b) => displayNames[a].localeCompare(displayNames[b]));
1043
- for (const otherName of otherNames) {
1044
- other.appendChild(optionForName(otherName, displayNames[otherName]));
1045
- }
1046
- // Add a separator option if there were any other names.
1047
- if (otherNames.length) {
1048
- node.appendChild(other);
1049
- }
1050
- // Handle the default value.
1051
- if (shouldStart === false) {
1052
- node.value = 'null';
1053
- }
1054
- else {
1055
- let selectedIndex = 0;
1056
- if (currentKernelDisplayName) {
1057
- // Select current kernel by default.
1058
- selectedIndex = [...node.options].findIndex(option => option.text === currentKernelDisplayName);
1059
- selectedIndex = Math.max(selectedIndex, 0);
1060
- }
1061
- node.selectedIndex = selectedIndex;
1062
- }
1063
- // Bail if there are no sessions.
1064
- if (!sessions) {
1065
- return;
1066
- }
1067
- // Add the sessions using the preferred language first.
1068
- const matchingSessions = [];
1069
- const otherSessions = [];
1070
- for (const session of sessions) {
1071
- if (language &&
1072
- session.kernel &&
1073
- languages[session.kernel.name] === language &&
1074
- session.kernel.id !== id) {
1075
- matchingSessions.push(session);
1076
- }
1077
- else if (((_a = session.kernel) === null || _a === void 0 ? void 0 : _a.id) !== id) {
1078
- otherSessions.push(session);
1079
- }
1080
- }
1081
- const matching = document.createElement('optgroup');
1082
- matching.label = trans.__('Use Kernel from Preferred Session');
1083
- node.appendChild(matching);
1084
- if (matchingSessions.length) {
1085
- matchingSessions.sort((a, b) => {
1086
- return a.path.localeCompare(b.path);
1087
- });
1088
- for (const session of matchingSessions) {
1089
- const name = session.kernel ? displayNames[session.kernel.name] : '';
1090
- matching.appendChild(optionForSession(session, name, translator));
1091
- }
1092
- }
1093
- const otherSessionsNode = document.createElement('optgroup');
1094
- otherSessionsNode.label = trans.__('Use Kernel from Other Session');
1095
- node.appendChild(otherSessionsNode);
1096
- if (otherSessions.length) {
1097
- otherSessions.sort((a, b) => {
1098
- return a.path.localeCompare(b.path);
1099
- });
1100
- for (const session of otherSessions) {
1101
- const name = session.kernel
1102
- ? displayNames[session.kernel.name] || session.kernel.name
1103
- : '';
1104
- otherSessionsNode.appendChild(optionForSession(session, name, translator));
1105
- }
1106
- }
1107
- }
1108
- Private.populateKernelSelect = populateKernelSelect;
1109
- /**
1110
- * Get the kernel search options given a session context and session manager.
1111
- */
1112
- function getKernelSearch(sessionContext) {
1113
- return {
1114
- specs: sessionContext.specsManager.specs,
1115
- sessions: sessionContext.sessionManager.running(),
1116
- preference: sessionContext.kernelPreference
1117
- };
1118
- }
1119
- /**
1120
- * Create an option element for a kernel name.
1121
- */
1122
- function optionForName(name, displayName) {
1123
- const option = document.createElement('option');
1124
- option.text = displayName;
1125
- option.value = JSON.stringify({ name });
1126
- return option;
1127
- }
1128
- /**
1129
- * Create an option for no kernel.
1130
- */
1131
- function optionForNone(translator) {
1132
- translator = translator || nullTranslator;
1133
- const trans = translator.load('jupyterlab');
1134
- const group = document.createElement('optgroup');
1135
- group.label = trans.__('Use No Kernel');
1136
- const option = document.createElement('option');
1137
- option.text = trans.__('No Kernel');
1138
- option.value = 'null';
1139
- group.appendChild(option);
1140
- return group;
1141
- }
1142
- /**
1143
- * Create an option element for a session.
1144
- */
1145
- function optionForSession(session, displayName, translator) {
1146
- var _a, _b;
1147
- translator = translator || nullTranslator;
1148
- const trans = translator.load('jupyterlab');
1149
- const option = document.createElement('option');
1150
- const sessionName = session.name || PathExt.basename(session.path);
1151
- option.text = sessionName;
1152
- option.value = JSON.stringify({ id: (_a = session.kernel) === null || _a === void 0 ? void 0 : _a.id });
1153
- option.title =
1154
- `${trans.__('Path:')} ${session.path}\n` +
1155
- `${trans.__('Name:')} ${sessionName}\n` +
1156
- `${trans.__('Kernel Name:')} ${displayName}\n` +
1157
- `${trans.__('Kernel Id:')} ${(_b = session.kernel) === null || _b === void 0 ? void 0 : _b.id}`;
1158
- return option;
1159
- }
1160
1224
  })(Private || (Private = {}));
1161
1225
  //# sourceMappingURL=sessioncontext.js.map