jupyterlab / jupyterlab_apod

JupyterLab extension tutorial answer key. Please see the tutorial at https://jupyterlab.readthedocs.io/en/stable/extension/extension_tutorial.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Open again after closing causes error

hanwiz opened this issue · comments

restorablepool.js:107 Uncaught (in promise) Error: A disposed object cannot be added.
at RestorablePool.add (restorablepool.js:107)
at WidgetTracker.add (widgettracker.js:111)
at execute (index.js:80)
at CommandRegistry.push.5TpB.CommandRegistry.execute (index.js:351)
at CommandPalette.push.eIPM.CommandPalette._execute (commandpalette.js:417)
at CommandPalette.push.eIPM.CommandPalette._evtClick (commandpalette.js:330)
at CommandPalette.push.eIPM.CommandPalette.handleEvent (commandpalette.js:211)

Just hit this same error. What was the resolution?

I hit the error too while doing this tutorial.

The way I fixed it was to initialize the WidgetTracker before the addCommand section in my activate function.

Thanks for sharing!

Reopening so we can re-examine those lines of code. Do you want to open a PR fixing it?

Do you want to open a PR fixing it?

Actually, this repo is tricky to open a PR to since the commits are engineered to follow the tutorial steps, so don't worry about opening a commit. We'll have to take a look at these lines the next time we refresh the tutorial. Thanks again for sharing.

This is because the MainAreaWidget is automatically disposed when closed, see MainAreaWidget code comments

I think we can replace the MainAreaWidget with a Widget, code like this:

// const content = new APODWidget();
widget = new APODWidget();

Using a MainAreaWidget is a good practice as it handles focus, a collapsed toolbar, etc., consistent across many jlab widgets.

Using a MainAreaWidget is a good practice as it handles focus, a collapsed toolbar, etc., consistent across many jlab widgets.

yes, but here is a check when tracker.add(widget), I can't control the MainAreaWidget was disposed when closed.

I am hitting the same issue as above. Is there any news on how to resolve this?

Hey all, I believe I have a fix for this. I didn't have any luck with @aosypov 's solution, but the following worked:

...

  // Add an application command
  const command: string = 'apod:open';
  app.commands.addCommand(command, {
    label: 'Random Astronomy Picture',
    execute: () => {
      if (!widget || widget.isDisposed) {
        // Create a new widget if one does not exist
        const content = new APODWidget();
        widget = new MainAreaWidget({content});
        widget.id = 'apod-jupyterlab';
        widget.title.label = 'Astronomy Picture';
        widget.title.closable = true;
      }

...

Adding widget.isDisposed to the if-condition prevented the execute function from trying to attach a disposed widget to the app. Now it initializes a new widget instead (and this didn't seem to impact the restoration at all — I think the logic's right).

@jasongrout Let me know if there's any way to update the docs or this tutorial repo.

Ah, killer fix @trajamsmith! Cheers :D This should definitely make it's way into the tutorial at some point yes.