I am working on a simple Jupyter Lab extension. I adapted the extension following this template:
activate: (app: JupyterFrontEnd, palette: ICommandPalette) => {
console.log('JupyterLab extension jupyterlab_apod is activated!');
// Create a blank content widget inside of a MainAreaWidget
const content = new Widget();
const widget = new MainAreaWidget({content});
widget.id = 'apod-jupyterlab';
widget.title.label = 'Astronomy Picture';
widget.title.closable = true;
// Add an application command
const command: string = 'apod:open';
app.commands.addCommand(command, {
label: 'Random Astronomy Picture',
execute: () => {
if (!widget.isAttached) {
// Attach the widget to the main work area if it's not there
app.shell.add(widget, 'main');
}
// Activate the widget
app.shell.activateById(widget.id);
}
});
// Add the command to the palette.
palette.addItem({command, category: 'Tutorial'});
}```
When I open the extension the first time, widget.title.label shows correctly. If I close and reopen, the title is blank. Does anyone know why this is happening, based on the code above?
Thanks
It looks like you are creating a single global widget. When it is closed in the UX, that widget is disposed by default. Then when you open again, you are just attaching a disposed widget (which would have cleared the title and disposed its other resources).
Depending on how you want to do things, you can create a new widget each time the command is run, or you can intercept the close request to not dispose the widget, but to just remove it from the dock panel. I think creating a new widget each time is the more common pattern.
I’m not sure where I posted it. Perhaps I posted it somewhere in gitter - not sure.
I would just create a new Widget each time. Or, you could grep the code base for the MainAreaWidget and look at the dispose and close methods - perhaps these can be overridden?
Clone the repo and grep -RiP 'MainAreaWidget' should find it.