Skip to content

Commit

Permalink
Add context menu on Laucher
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiaSliusar committed Jul 24, 2024
1 parent 9a5339c commit 3b070bf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
21 changes: 3 additions & 18 deletions packages/apputils/src/custom_env.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function CustomEnv({updateFormData, defaultEnvValues, translator }: EnvProps) {
}

const handleChange = (envVars: PartialJSONObject) => {
console.log('envVars');
console.dir(envVars);

setInputs({
...formData,
...envVars
Expand All @@ -112,25 +111,17 @@ function CustomEnv({updateFormData, defaultEnvValues, translator }: EnvProps) {
};

let envBlock = [];
console.log('countEnvBlock');
console.log(countEnvBlock);
for(let index=1; index<=countEnvBlock; index++) {
console.log('loop');
let envData = formData[`${index}`] as PartialJSONObject | undefined;
console.log('---envData---');
console.dir(envData);

let defaultName = envData && envData.name? envData.name: '';
let defaultEnvValue = envData && envData.value? envData.value: '';
console.log('defaultName');
console.log(defaultName);

console.log('defaultEnvValue');
console.log(defaultEnvValue);
envBlock.push(<EnvBlock id={`${index}`} key={index} handleChange={handleChange} defaultName={defaultName as string} defaultEnvValue={defaultEnvValue as string} translator={translator}/>)
}

const header = `${trans.__('Setup custom env variables:')}`;
const addMoreVarLabel= trans.__('Add');
const addMoreVarLabel= trans.__('Add more');

return (
<div>
Expand Down Expand Up @@ -176,20 +167,14 @@ export class CustomEnvWidget extends ReactWidget {
render(): JSX.Element {
let k = 1;
let tmp = {} as PartialJSONObject;
console.log('this.defaultEnvValues');
console.dir(this.defaultEnvValues)
for (let index in this.defaultEnvValues) {
let envVarsIndex = `${k}`;
console.log('envVarsIndex');
console.log(envVarsIndex);
tmp[envVarsIndex] = {
'name': index,
'value':this.defaultEnvValues[index]
};
k+=1;
}
console.log('default obj');
console.dir(tmp);
return (
<CustomEnv
updateFormData={this.updateFormData} defaultEnvValues={tmp} translator={this.translator}
Expand Down
6 changes: 3 additions & 3 deletions packages/apputils/src/sessioncontext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,6 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
sessionContext.kernelPreference.customEnvVars = model.custom_env_vars;
}

console.log('sessionContext.kernelPreference.customEnvVars');
console.dir(sessionContext.kernelPreference.customEnvVars);

if (hasCheckbox && result.isChecked !== null) {

sessionContext.kernelPreference = {
Expand All @@ -1431,6 +1428,9 @@ export class SessionContextDialogs implements ISessionContext.IDialogs {
};
}

console.log('sessionContext.kernelPreference.customEnvVars');
console.dir(sessionContext.kernelPreference.customEnvVars);

if (model === null && !sessionContext.hasNoKernel) {
return sessionContext.shutdown();
}
Expand Down
76 changes: 76 additions & 0 deletions packages/notebook-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ import {
CellMetadataField,
NotebookMetadataField
} from './tool-widgets/metadataEditorFields';
import { ISpecModel } from '@jupyterlab/services/src/kernelspec/restapi';
import { CustomEnvWidget } from '@jupyterlab/apputils/src/custom_env';

/**
* The command IDs used by the notebook plugin.
Expand Down Expand Up @@ -329,6 +331,8 @@ namespace CommandIDs {
export const accessNextHistory = 'notebook:access-next-history-entry';

export const virtualScrollbar = 'notebook:toggle-virtual-scrollbar';

export const setupCustomEnv = 'notebook:setup-custom-env';
}

/**
Expand Down Expand Up @@ -1988,6 +1992,72 @@ function activateNotebookHandler(
}
});

const showCustomEnvVarsDialog = async(
spec: ISpecModel | undefined
) => {
let envConfiguration: PartialJSONObject = {};
let label = trans.__('Cancel');
const buttons = [
Dialog.cancelButton({
label
}),
Dialog.okButton({
label: trans.__('Setup'),
ariaLabel: trans.__('setup custom env variables')
})
];

let defaultEnvValues = {}
const dialog = new Dialog({
title: trans.__('Select Kernel'),
body: new CustomEnvWidget(envConfiguration, defaultEnvValues, formData => {
envConfiguration = formData as PartialJSONObject;
console.log('envConfiguration');
console.dir(envConfiguration);
}, translator),
buttons
});

const result = await dialog.launch();

if (!result.button.accept) {
return;
}
if (result.value) {
//saveve for each kernel spec
}
};

const LAUNCHER_LABEL = "jp-LauncherCard";
const isLauncherLabel = (node: HTMLElement) =>
node.classList.contains(LAUNCHER_LABEL);
let selectedSpec: ISpecModel | undefined;

// add command for context menu on Launch app icon
app.commands.addCommand(CommandIDs.setupCustomEnv, {
label: trans.__('Setup custom env variables'),
caption: trans.__('Setup custom env variables for running a kernel'),
execute: async (args?: any) => {
const node = app.contextMenuHitTest(isLauncherLabel);
if (!node) {
return;
}
const specs = services.kernelspecs.specs;
if (!specs) {
return;
}
let defaultName = node.getAttribute('titile');
for (const name in specs.kernelspecs) {
const spec = specs.kernelspecs[name];
if (spec && spec.display_name === defaultName) {
selectedSpec = spec;
}
}
showCustomEnvVarsDialog(selectedSpec);
}
});


// Add a launcher item if the launcher is available.
if (launcher) {
void services.ready.then(() => {
Expand Down Expand Up @@ -2027,6 +2097,12 @@ function activateNotebookHandler(
onSpecsChanged();
services.kernelspecs.specsChanged.connect(onSpecsChanged);
});

app.contextMenu.addItem({
command: CommandIDs.setupCustomEnv,
selector: '.jp-LauncherCard',
rank: 0
});
}

return tracker;
Expand Down

0 comments on commit 3b070bf

Please sign in to comment.