Skip to content

Commit

Permalink
Merge pull request #77 from vatlab/displayed_name
Browse files Browse the repository at this point in the history
Fix loading sos when kernel display name is not SoS #76
  • Loading branch information
BoPeng authored Oct 16, 2024
2 parents 96725b5 + 99e1d4c commit 4ea5658
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 81 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-sos",
"version": "0.10.3",
"version": "0.10.6",
"description": "JupyterLab extension for SoS workflow engine and polyglot notebook",
"keywords": [
"jupyter",
Expand Down
102 changes: 42 additions & 60 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IDisposable, DisposableDelegate } from '@lumino/disposable';

import { Cell, CodeCell } from '@jupyterlab/cells';

import { Kernel, Session } from '@jupyterlab/services';
import { Kernel } from '@jupyterlab/services';

import { DocumentRegistry } from '@jupyterlab/docregistry';

Expand Down Expand Up @@ -42,7 +42,8 @@ import {
toggleDisplayOutput,
toggleCellKernel,
toggleMarkdownCell,
KernelSwitcher
KernelSwitcher,
showSoSWidgets
} from './selectors';

import { wrapExecutor, wrapConsoleExecutor } from './execute';
Expand Down Expand Up @@ -661,21 +662,6 @@ function connectSoSComm(panel: NotebookPanel, renew: boolean = false) {
}
}

function hideSoSWidgets(element: HTMLElement) {
let sos_elements = element.getElementsByClassName(
'jp-CelllanguageDropDown'
) as HTMLCollectionOf<HTMLElement>;
for (let i = 0; i < sos_elements.length; ++i)
sos_elements[i].style.display = 'none';
}

function showSoSWidgets(element: HTMLElement) {
let sos_elements = element.getElementsByClassName(
'jp-CelllanguageDropDown'
) as HTMLCollectionOf<HTMLElement>;
for (let i = 0; i < sos_elements.length; ++i)
sos_elements[i].style.display = '';
}

(<any>window).task_action = async function (param) {
if (!param.action) {
Expand Down Expand Up @@ -736,50 +722,48 @@ export class SoSWidgets

// this is a singleton class
context.sessionContext.ready.then(() => {
// kernel information (for opened notebook) should be ready at this time.
// However, when the notebook is created from File -> New Notebook -> Select Kernel
// The kernelPreference.name is not yet set and we have to use kernelDisplayName
// which is SoS (not sos)
let cur_kernel = panel.context.sessionContext.kernelDisplayName == "No Kernel" ? panel.context.sessionContext.kernelPreference.name : panel.context.sessionContext.kernelDisplayName;
if (cur_kernel.toLowerCase() === 'sos') {
console.log(`session ready with kernel sos`);
// if this is not a sos kernel, remove all buttons
if (panel.content.model.getMetadata('sos')) {
info.updateLanguages(
(panel.content.model.getMetadata('sos') as any)['kernels']
);
} else {
panel.content.model.setMetadata('sos', {
kernels: [['SoS', 'sos', '', '']],
version: ''
});
}
if (!info.sos_comm) {
connectSoSComm(panel);
wrapExecutor(panel);
void context.sessionContext.session?.kernel?.info.then(kernel_info => {
const lang = kernel_info.language_info;
const kernel_name = context.sessionContext.session.kernel.name;
if (lang.name === 'sos') {
console.log(`session ready with kernel sos`);
info.LanguageName.set(kernel_name, 'sos');
// if this is not a sos kernel, remove all buttons
if (panel.content.model.getMetadata('sos')) {
info.updateLanguages(
(panel.content.model.getMetadata('sos') as any)['kernels']
);
} else {
panel.content.model.setMetadata('sos', {
kernels: [['SoS', kernel_name, 'sos', '', '']],
version: ''
});
}
if (!info.sos_comm) {
connectSoSComm(panel);
wrapExecutor(panel);
}
updateCellStyles(panel, info);
showSoSWidgets(panel.node);
}
updateCellStyles(panel, info);
showSoSWidgets(panel.node);
} else {
hideSoSWidgets(panel.node);
}
})
});

context.sessionContext.kernelChanged.connect(
(sender: any, args: Session.ISessionConnection.IKernelChangedArgs) => {
// somehow when the kernelChanged is sent, there could be no newValue?
if (!args.newValue) {
return;
}
console.log(`kernel changed to ${args.newValue.name}`);
if (args.newValue.name === 'sos') {
context.sessionContext.kernelChanged.connect(() => {
void context.sessionContext.session?.kernel?.info.then(kernel_info => {
const lang = kernel_info.language_info;
const kernel_name = context.sessionContext.session.kernel.name;
info.LanguageName.set(kernel_name, 'sos');

console.log(`kernel changed to ${lang.name}`);
if (lang.name === 'sos') {
if (panel.content.model.getMetadata('sos')) {
info.updateLanguages(
(panel.content.model.getMetadata('sos') as any)['kernels']
);
} else {
panel.content.model.setMetadata('sos', {
kernels: [['SoS', 'sos', '', '']],
kernels: [['SoS', kernel_name, 'sos', '', '']],
version: ''
});
}
Expand All @@ -789,12 +773,9 @@ export class SoSWidgets
}
updateCellStyles(panel, info);
showSoSWidgets(panel.node);
} else {
// in this case, the .sos_widget should be hidden
hideSoSWidgets(panel.node);
}
}
);
});
});

context.sessionContext.statusChanged.connect((sender, status) => {
// if a sos notebook is restarted
Expand All @@ -808,9 +789,10 @@ export class SoSWidgets
});

panel.content.model.cells.changed.connect((list, changed) => {
let cur_kernel =
panel.context.sessionContext.kernelPreference.name ||
panel.context.sessionContext.kernelDisplayName;
let cur_kernel = panel.context.sessionContext.kernelPreference.name;
if (!cur_kernel) {
return;
}
if (cur_kernel.toLowerCase() === 'sos') {
each(changed.newValues, cellmodel => {
let idx = changed.newIndex; // panel.content.widgets.findIndex(x => x.model.id == cellmodel.id);
Expand Down
21 changes: 14 additions & 7 deletions src/selectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import { ReactWidget } from '@jupyterlab/apputils';
import React from 'react';

const CELL_LANGUAGE_DROPDOWN_CLASS = 'jp-CelllanguageDropDown';
const CELL_HIDDEN_LANGUAGE_DROPDOWN_CLASS = 'jp-Hidden'

export function showSoSWidgets(element: HTMLElement) {
let sos_elements = element.getElementsByClassName(
CELL_LANGUAGE_DROPDOWN_CLASS
) as HTMLCollectionOf<HTMLElement>;
for (let i = 0; i < sos_elements.length; ++i)
sos_elements[i].classList.remove(CELL_HIDDEN_LANGUAGE_DROPDOWN_CLASS);
}

export function saveKernelInfo() {
let panel = Manager.currentNotebook;
Expand Down Expand Up @@ -286,6 +295,9 @@ export function updateCellStyles(
export class KernelSwitcher extends ReactWidget {
constructor() {
super();
// this.state = {
// is_sos: false;
// }
}

handleChange = (event: React.ChangeEvent<HTMLSelectElement>): void => {
Expand All @@ -307,13 +319,8 @@ export class KernelSwitcher extends ReactWidget {

render(): JSX.Element {
let panel = Manager.currentNotebook;
let cur_kernel =
panel.context.sessionContext.kernelDisplayName == "No Kernel" ? panel.context.sessionContext.kernelPreference.name : panel.context.sessionContext.kernelDisplayName;
if (cur_kernel.toLowerCase() !== 'sos') {
return;
}

let info = Manager.manager.get_info(panel);

let cell = panel.content.activeCell;

const optionChildren = info.KernelList.map(lan => {
Expand All @@ -327,7 +334,7 @@ export class KernelSwitcher extends ReactWidget {

return (
<HTMLSelect
className={CELL_LANGUAGE_DROPDOWN_CLASS}
className={CELL_LANGUAGE_DROPDOWN_CLASS + " " + CELL_HIDDEN_LANGUAGE_DROPDOWN_CLASS}
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
value={kernel ? kernel : 'SoS'}
Expand Down
19 changes: 7 additions & 12 deletions style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,31 @@
}

.jp-CelllanguageDropDown {
display: block;
}

.jp-CelllanguageDropDown.jp-Hidden {
display: none;
}

.jp-OutputArea-prompt:empty {
padding: 0px;
}

/*
.cm-sos-script {
font-style: normal;
}
.cm-sos-option {
font-style: italic;
} */
.jp-CodeCell .jp-InputArea .jp-CelllanguageDropDown {
width: 70pt;
background: none;
z-index: 1000;
right: 8pt;
font-size: 80%;
display: block;
/* display: block; */
}

.jp-CodeCell .jp-cell-menu .jp-CelllanguageDropDown {
width: 70pt;
background: none;
font-size: 80%;
display: block;
/* display: block; */
margin-left: 5px;
margin-right: 5px;
border: 0px;
Expand Down Expand Up @@ -320,8 +316,7 @@ td.task_icon {
font-size: 0.75em;
}

td.task_status,
{
td.task_status {
width: 15em;
text-align: left;
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit 4ea5658

Please sign in to comment.