Skip to content

Commit

Permalink
lab support
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Mar 16, 2021
1 parent 22a6527 commit b061857
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
31 changes: 21 additions & 10 deletions jupyterlab_widgets/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,26 @@ export class WidgetManager extends LabWidgetManager {
* Save the widget state to the context model.
*/
private _saveState(): void {
const visibleWidgets = [];
for (const cell of toArray(this._context.model.cells)) {
if (cell.type === 'code') {
const codeCell = cell as ICodeCellModel;
for (let i = 0; i < codeCell.outputs.length; i++) {
const output = codeCell.outputs.get(i);
if (output.data[WIDGET_VIEW_MIMETYPE]) {
const widgetData = output.data[WIDGET_VIEW_MIMETYPE] as any;
const modelId = widgetData['model_id'];
visibleWidgets.push(modelId);
const metadata: any = this._context.model?.metadata.toJSON() || null;
// the default is to not store widgets
const store = metadata?.widgets?.store || 'none';
if (store == 'none') {
return;
}

let visibleWidgets = undefined;
if (store == 'displayed') {
visibleWidgets = [];
for (const cell of toArray(this._context.model.cells)) {
if (cell.type === 'code') {
const codeCell = cell as ICodeCellModel;
for (let i = 0; i < codeCell.outputs.length; i++) {
const output = codeCell.outputs.get(i);
if (output.data[WIDGET_VIEW_MIMETYPE]) {
const widgetData = output.data[WIDGET_VIEW_MIMETYPE] as any;
const modelId = widgetData['model_id'];
visibleWidgets.push(modelId);
}
}
}
}
Expand All @@ -536,6 +546,7 @@ export class WidgetManager extends LabWidgetManager {
visibleWidgets: visibleWidgets
});
this._context.model.metadata.set('widgets', {
store: store,
'application/vnd.jupyter.widget-state+json': state
});
}
Expand Down
47 changes: 34 additions & 13 deletions jupyterlab_widgets/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,24 +247,45 @@ function activateWidgetExtension(
});
}

function isNotebookActive(): boolean {
return (
tracker?.currentWidget !== null &&
tracker?.currentWidget === app.shell.currentWidget
);
}

if (settingRegistry !== null) {
// Add a command for automatically saving (jupyter-)widget state.
commands.addCommand('@jupyter-widgets/jupyterlab-manager:saveWidgetState', {
label: 'Save Widget State Automatically',
execute: args => {
return settingRegistry
.set(plugin.id, 'saveState', !SETTINGS.saveState)
.catch((reason: Error) => {
console.error(`Failed to set ${plugin.id}: ${reason.message}`);
});
},
isToggled: () => SETTINGS.saveState
});
// Add commands for changing how we save (jupyter-)widget state.
let createStoreCommand = (name: string, value: string, label: string) => {
commands.addCommand(`@jupyter-widgets/jupyterlab-manager:${name}`, {
label: label,
execute: args => {
const current = tracker?.currentWidget || null;
current?.model?.metadata.set('widgets', { store: value });
app.commands.execute('docmanager:save');
},
isToggled: () => {
const current = tracker?.currentWidget || null;
const metadata: any = current?.model?.metadata.toJSON() || null;
return metadata?.widgets?.store === value;
},
isEnabled: isNotebookActive
});
};
createStoreCommand('saveNoWidgets', 'none', 'Store No Widgets');
createStoreCommand(
'saveDisplayedWidgets',
'displayed',
'Store Displayed Widgets'
);
createStoreCommand('saveAllWidgets', 'all', 'Store All Widgets');
}

if (menu) {
menu.settingsMenu.addGroup([
{ command: '@jupyter-widgets/jupyterlab-manager:saveWidgetState' }
{ command: '@jupyter-widgets/jupyterlab-manager:saveNoWidgets' },
{ command: '@jupyter-widgets/jupyterlab-manager:saveDisplayedWidgets' },
{ command: '@jupyter-widgets/jupyterlab-manager:saveAllWidgets' }
]);
}

Expand Down

0 comments on commit b061857

Please sign in to comment.