Extension Tab Title is Blank after Closing+Reopen

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.

2 Likes

@jasongrout I’ve been trying to figure this out all day - how do we remove it from the dock panel without disposing it?

I am trying to toggle the visibility of the Widget, without disposing and creating it each time.

EDIT:

I seem to have gotten this working, however I doubt I did it correctly. I posted a message that shows the relevant code.

@adpatter where can I find that message you mentioned with the code? Trying to solve the exact same problem at the moment.

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.

1 Like