Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterized kernel specs #1

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
04176cf
Add dialog for kernel custom specs
AnastasiaSliusar Apr 30, 2024
f21e44b
Fix dialog, pass custom kernel spec to backend
AnastasiaSliusar May 3, 2024
e7bac9a
Merge branch 'jupyterlab:main' into parameterized-kernel-specs
AnastasiaSliusar May 3, 2024
f45d64e
Updte forms for seting custom kernel specs
AnastasiaSliusar May 14, 2024
53d6ba9
Add validation
AnastasiaSliusar May 14, 2024
39eddd1
Update a form and update data if there is no error
AnastasiaSliusar May 16, 2024
6874222
fix form and validation
AnastasiaSliusar May 17, 2024
6961eb5
Fix starting kernels
AnastasiaSliusar May 20, 2024
4fe58f6
Set custom kernel variables into metadata
AnastasiaSliusar May 24, 2024
6a27c01
Fix running a kernel by using metadata with custom kernel specs
AnastasiaSliusar May 28, 2024
0272d69
Fix saving custom kernel params into metadata
AnastasiaSliusar May 31, 2024
493f05d
Setup a dialog window to a Console panel on Launcher
AnastasiaSliusar Jun 4, 2024
5fa63a6
Running a console with a kernel, fix a dialog window
AnastasiaSliusar Jun 6, 2024
939d34b
Fix showing part of a dialog when a kernel is selected by default
AnastasiaSliusar Jun 10, 2024
42f54bd
Add fixes to a dialog
AnastasiaSliusar Jun 10, 2024
df6e8f4
Fix a dialog window for selecting a kernel
AnastasiaSliusar Jun 11, 2024
bfdf0c1
Merge branch 'main' into parameterized-kernel-specs
AnastasiaSliusar Jun 13, 2024
434718f
Fix selecting parameterized kernels
AnastasiaSliusar Jun 14, 2024
fcfc13d
Clean console.log and console.dir
AnastasiaSliusar Jun 14, 2024
0099428
Merge branch 'main' into parameterized-kernel-specs
AnastasiaSliusar Jun 14, 2024
2356748
minor fix
AnastasiaSliusar Jun 14, 2024
05c32ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 14, 2024
398b819
Automatic application of license header
github-actions[bot] Jun 14, 2024
1501b2b
Fix pre-commit errors
AnastasiaSliusar Jun 17, 2024
3de9803
Fix updating launch parameters when a new kernel is selected
AnastasiaSliusar Jun 21, 2024
9cbb5e1
Remove saving a user kernel custom selection into notebook metadata
AnastasiaSliusar Jun 21, 2024
2cd96b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 21, 2024
e852ee9
Add condition when a dialog should be shown
AnastasiaSliusar Jun 26, 2024
562e9dc
Merge branch 'parameterized-kernel-specs' of https://github.com/Anast…
AnastasiaSliusar Jun 26, 2024
c48f438
fix showing a dialog when a kernel spec file are secure and insecure
AnastasiaSliusar Jul 4, 2024
7f07a5d
Fix showing a dialog from console when kernel spec files are filtered
AnastasiaSliusar Jul 5, 2024
80d8944
Resolve conflicts
AnastasiaSliusar Aug 12, 2024
6579197
Fix showing the dialog part for setuping kernel custom params
AnastasiaSliusar Aug 13, 2024
8baa65c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
"yjs": "^13.5.40"
},
"dependencies": {
"@jupyterlab/application": "workspace:^",
"@jupyterlab/apputils": "workspace:^",
"@typescript-eslint/eslint-plugin": "~6.13.2",
"@typescript-eslint/parser": "~6.13.2",
"eslint": "~8.55.0",
Expand Down
218 changes: 194 additions & 24 deletions packages/apputils/src/sessioncontext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { IChangedArgs, PathExt } from '@jupyterlab/coreutils';
import { IChangedArgs, PageConfig, PathExt } from '@jupyterlab/coreutils';
import {
Kernel,
KernelMessage,
Expand All @@ -16,12 +16,20 @@
TranslationBundle
} from '@jupyterlab/translation';
import { find } from '@lumino/algorithm';
import { JSONExt, PromiseDelegate, UUID } from '@lumino/coreutils';
import {
JSONExt,
PartialJSONObject,
PartialJSONValue,
PromiseDelegate,
UUID
} from '@lumino/coreutils';
import { IDisposable, IObservableDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { Widget } from '@lumino/widgets';
import * as React from 'react';
import { Dialog, showDialog } from './dialog';
import { DialogWidget } from '@jupyterlab/ui-components';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';

/**
* A context object to manage a widget's kernel session connection.
Expand Down Expand Up @@ -297,6 +305,10 @@
*/
readonly autoStartDefault?: boolean;

/**
* Kernel custom specs defined by kernel name
*/
customKernelSpecs?: undefined | PartialJSONObject;
/**
* Skip showing the kernel restart dialog if checked (default `false`).
*/
Expand Down Expand Up @@ -694,7 +706,8 @@
*/
async startKernel(): Promise<boolean> {
const preference = this.kernelPreference;

const specs = this.specsManager.specs;
//
if (!preference.autoStartDefault && preference.shouldStart === false) {
return true;
}
Expand All @@ -704,12 +717,19 @@
options = { id: preference.id };
} else {
const name = Private.getDefaultKernel({
specs: this.specsManager.specs,
specs,
sessions: this.sessionManager.running(),
preference
});
if (name) {
options = { name };
if (preference.customKernelSpecs) {
options = {
name,
custom_kernel_specs: preference.customKernelSpecs
};
} else {
options = { name };
}
}
}

Expand All @@ -733,6 +753,7 @@
*/
async restartKernel(): Promise<void> {
const kernel = this.session?.kernel || null;

if (this._isRestarting) {
return;
}
Expand Down Expand Up @@ -1407,19 +1428,37 @@
return;
}

if (hasCheckbox && result.isChecked !== null) {
sessionContext.kernelPreference = {
...sessionContext.kernelPreference,
autoStartDefault: result.isChecked
const dialogResult = result.value as Kernel.IModel;

if (dialogResult) {
let model = {
name: dialogResult.name,
custom_kernel_specs: {}
};
}

const model = result.value;
if (model === null && !sessionContext.hasNoKernel) {
return sessionContext.shutdown();
}
if (model) {
await sessionContext.changeKernel(model);
if (hasCheckbox && result.isChecked !== null) {
if (model && sessionContext.kernelPreference?.customKernelSpecs) {

Check warning

Code scanning / CodeQL

Useless conditional Warning

This use of variable 'model' always evaluates to true.
sessionContext.kernelPreference.customKernelSpecs = undefined;
}

if (model && dialogResult.custom_kernel_specs) {

Check warning

Code scanning / CodeQL

Useless conditional Warning

This use of variable 'model' always evaluates to true.
sessionContext.kernelPreference.customKernelSpecs =
dialogResult.custom_kernel_specs;
model['custom_kernel_specs'] = dialogResult.custom_kernel_specs;
}

sessionContext.kernelPreference = {
...sessionContext.kernelPreference,
autoStartDefault: result.isChecked
};
}

if (model === null && !sessionContext.hasNoKernel) {

Check warning

Code scanning / CodeQL

Comparison between inconvertible types Warning

Variable 'model' is of type object, but it is compared to
an expression
of type null.
return sessionContext.shutdown();
}
if (model) {

Check warning

Code scanning / CodeQL

Useless conditional Warning

This use of variable 'model' always evaluates to true.
await sessionContext.changeKernel(model);
}
}
}

Expand Down Expand Up @@ -1813,20 +1852,36 @@
sessionContext: ISessionContext,
translator?: ITranslator
) =>
new KernelSelector({
node: createSelectorNode(sessionContext, translator)
});
new KernelSelector(sessionContext, translator);

/**
* A widget that provides a kernel selection.
*/
class KernelSelector extends Widget {
export class KernelSelector extends Widget {
sessionContext: ISessionContext;
translator: ITranslator | undefined;
/**
* Create a new kernel selector widget.
*/
constructor(sessionContext: ISessionContext, translator?: ITranslator) {
super({ node: createSelectorNode(sessionContext, translator) });
this.sessionContext = sessionContext;
this.translator = translator;
}

/**
* Get the value of the kernel selector widget.
*/
getValue(): Kernel.IModel {
const selector = this.node.querySelector('select') as HTMLSelectElement;
return JSON.parse(selector.value) as Kernel.IModel;
const selector = this.node.querySelector(
'select#js-kernel-selector'
) as HTMLSelectElement;
const selectorKernelSpecs = selector.getAttribute('data-kernel-spec');
let kernelData = JSON.parse(selector.value) as Kernel.IModel;
if (selectorKernelSpecs) {
kernelData['custom_kernel_specs'] = JSON.parse(selectorKernelSpecs);
}
return kernelData;
}
}

Expand All @@ -1842,11 +1897,24 @@
const trans = translator.load('jupyterlab');

const body = document.createElement('div');

const container = document.createElement('div');

const kernelSpecsContainer = document.createElement('div');

container.setAttribute('id', 'js-kernel-select-container');

kernelSpecsContainer.setAttribute(
'id',
'js-kernel-specs-select-container'
);

const text = document.createElement('label');
text.textContent = `${trans.__('Select kernel for:')} "${
sessionContext.name
}"`;
body.appendChild(text);

container.appendChild(text);

const select = document.createElement('select');
const options = SessionContextDialogs.kernelOptions(
Expand All @@ -1860,18 +1928,120 @@
optgroup.label = label;
for (const { selected, text, title, value } of options) {
const option = document.createElement('option');
if (selected) option.selected = true;
if (selected) {
option.selected = true;
let val = JSON.parse(value);
let id = val && val.id ? val.id : '';
if (!id) {
select.setAttribute('data-kernel-spec', '');
checkCustomKernelSpecs(sessionContext, select, trans);
}
}
if (title) option.title = title;
option.text = text;
option.value = value;
optgroup.appendChild(option);
}
select.appendChild(optgroup);
}
select.setAttribute('id', 'js-kernel-selector');
select.onchange = () => {
select.setAttribute('data-kernel-spec', '');
checkCustomKernelSpecs(sessionContext, select, trans);
};

body.appendChild(select);
body.appendChild(kernelSpecsContainer);
return body;
}

function checkCustomKernelSpecs(
sessionContext: ISessionContext,
select: HTMLSelectElement,
trans: IRenderMime.TranslationBundle,
kernelSpeccSelectorContainer?: HTMLDivElement
) {
let kernelConfiguration: PartialJSONObject = {};
let selectedKernel = JSON.parse(select.value) as Kernel.IModel;

let kernelSpecsContainer = document.querySelector(
'#js-kernel-specs-select-container'
) as HTMLElement;

if (!kernelSpecsContainer && kernelSpeccSelectorContainer) {
kernelSpecsContainer = kernelSpeccSelectorContainer;
}

kernelSpecsContainer.innerHTML = '';
let kernelName =
selectedKernel && selectedKernel.name ? selectedKernel.name : '';
let kernel =
kernelName && sessionContext.specsManager.specs?.kernelspecs[kernelName];
const allowInsecureKernelspecParams =
PageConfig.getOption('allow_insecure_kernelspec_params') === 'true'
? true
: false;
if (
(kernel &&
kernel?.metadata &&
kernel?.metadata?.is_secure &&
kernel?.metadata?.parameters) ||
allowInsecureKernelspecParams
) {
if (kernel && kernel?.metadata && kernel?.metadata?.parameters) {
let kernelParameters = kernel?.metadata
?.parameters as PartialJSONObject;

if (kernelParameters) {
if (sessionContext.kernelPreference?.customKernelSpecs) {
let customKernelSpecs = sessionContext.kernelPreference
?.customKernelSpecs as PartialJSONObject;
for (let key in customKernelSpecs) {
let selectedValue = customKernelSpecs[key] as
| PartialJSONValue
| undefined;

if (kernelParameters.properties) {
let properties =
kernelParameters.properties as PartialJSONObject;

let kernelParameter = properties[key] as PartialJSONObject;

if (kernelParameter) {
let kernelParametersTmp = (
kernelParameters.properties as PartialJSONObject
)[key] as PartialJSONObject;
(kernelParameters.properties as PartialJSONObject)[key] = {
...kernelParametersTmp,
default: selectedValue
};
}
}
}
}

let kernelSpecWidget = new DialogWidget(
kernelParameters,
kernelConfiguration,
formData => {
kernelConfiguration = formData as PartialJSONObject;
select.setAttribute(
'data-kernel-spec',
JSON.stringify(kernelConfiguration)
);
},
trans
);

//Update widget
if (kernelSpecsContainer) {
Widget.attach(kernelSpecWidget, kernelSpecsContainer);
}
}
}
}
}

/**
* Get the default kernel name given select options.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/console-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"@lumino/coreutils": "^2.1.2",
"@lumino/disposable": "^2.1.2",
"@lumino/properties": "^2.0.1",
"@lumino/widgets": "^2.3.2"
"@lumino/widgets": "^2.3.2",
"@rjsf/utils": "^5.13.4"
},
"devDependencies": {
"rimraf": "~5.0.5",
Expand Down
Loading
Loading