papermill icon indicating copy to clipboard operation
papermill copied to clipboard

How to retain widget state in output notebooks?

Open choldgraf opened this issue 5 years ago • 5 comments

I'm trying to use papermill to generate report notebooks that include ipywidgets in their outputs. When I use papermill to build these, the widget state doesn't seem to be included in the output notebook. I've tried playing around with this a bit and have found the following things:

  • If I run the notebook and create the widgets interactively, then use nbconvert to convert to a different notebook (e.g. jupyter-nbconvert --to notebook mynotebook.ipynb) then the widgets display correctly
  • If I run papermill on the notebook, then the widget is not displayed correctly (it just displays the text representation of the widgets)
  • If I use --execute to the nbconvert command, it also does not display correctly (jupyter-nbconvert --to notebook mynotebook.ipynb --execute).

So I guess this is a problem with executing the notebook rather than simply converting the output of a pre-existing notebook. The final point above makes me suspect that this is a problem at the nbconvert level rather than the papermill level, but I'd be curious if folks have run into this here and if there are any workarounds.

It sounds like @maartenbreddels and @jasongrout worked on retaining widget state in https://github.com/jupyter/nbconvert/pull/900 - perhaps they've got thoughts on achieving the same thing after executing the cells in a notebook

choldgraf avatar Jul 15 '19 21:07 choldgraf

@choldgraf Have you clicked "Trust Notebook" after executing (bottom of https://nbconvert.readthedocs.io/en/latest/execute_api.html#widget-state highlights this)?

MSeal avatar Jul 16 '19 16:07 MSeal

Hmm - I think there might be multiple things going on there:

You are correct that when I create a simple widget in a notebook, then use papermill to create a new notebook, then "trust" the notebook and reload the page, that the widget now shows up.

However, this sometimes doesn't happen and I'm not sure why. For a more complex example, for this notebook:

https://github.com/choldgraf/jupyter-activity-snapshot/blob/master/reports/2019-05-17_2019-07-16.ipynb

The method above doesn't work - if I "trust" the output notebook, I still get errors in rendering the widgets.

Moreover when this is rendered on nbviewer (which I don't think should be affected by "trust notebook" or not) the widgets are just blank:

https://nbviewer.jupyter.org/github/choldgraf/jupyter-activity-snapshot/blob/master/reports/2019-05-17_2019-07-16.ipynb#A-list-of-recent-issues

So...I guess maybe this is some kind of interaction between ipywidgets, altair, and nbconvert?

choldgraf avatar Jul 16 '19 17:07 choldgraf

It does sound like an oddity with those libraries. Might be worth posting to altair and/or ipython issues. I will try to take a look this weekend at the example more closely for if there's something odd with nbconvert propagating the payload, but I suspect it's unlikely to be a problem there.

MSeal avatar Jul 16 '19 21:07 MSeal

that sounds good - I tried a bunch of combinations of plotting w/ altair and various tabbed widget outputs with ipywidgets, and couldn't get it to reproduce outside of the context of the mega notebook I linked above.

If you don't think there's a path forward on this, feel free to close this one, as you say I doubt that this is a strictly papermill (or nbconvert) fix

choldgraf avatar Jul 16 '19 21:07 choldgraf

If I run papermill on the notebook, then the widget is not displayed correctly (it just displays the text representation of the widgets)

I have the same issue. The papermill changelog says that "ipywidgets are now supported" from version 1.0.0 but how would I trigger that from the command line? Is it possible to just add a parameter to papermill in.ipynb out.ipynb to get the widgets in the output notebook, or do I need to call papermill programmatically (and how)? Thanks!

tgpfeiffer avatar Jun 14 '22 01:06 tgpfeiffer