Nbdev v2 & Jupyter Widgets

I am trying to get nbdev v2 to generate docs that include Jupyter widgets. Is this expected to work?

The Quarto docs show that it does support Jupyter widgets.

I my experiments, notebooks containing Jupyter widgets render as expected with the classic Jupyter Server and JupyterLab. However, when I run nbdev_preview, the code cell outputs do not appear in pages served. I don’t see any errors reported in the terminal running nbdev_preview or in the browser console.

1 Like

I’m trying this now I’ll let you know

1 Like

I am having the same problems. We have reported the issue to Quarto. Thanks for letting us know

Thanks for investigating and reporting to Quarto. Is there a quarto-dev issue to follow for this?

1 Like

Thanks @tylere for following up on this. We have heard back from Quarto that we actually need to have execution turned on (which we turn off by default in nbdev) in order to render the widget.

Let me discuss this with @jeremy and @seem and get back to you

1 Like

I’ve tried specifying cell execution in the YAML front matter of the notebook that contains Jupyter widgets:

execute: 
  enabled: true

Unfortunately this addition causes nbdev_preview to fail while interpreting the comment directives:

$ nbdev_preview

**Preparing to preview**
**[1/1] 00_core.ipynb**

Starting python3 kernel...Done

Executing '00_core.ipynb'
  Cell 1/7...ERROR:

None of the known patterns match for default_exp core

Let me know if there something I can do to help debug (providing a detailed reproducible bug report, etc.).

Thanks, I’m looking into this

Any update on having an option to turn cell execution on?

Fwiw, adding the following notebook frontmatter, as described in the nbdev doc’s Automatic Cell Execution section, also doesn’t seem to have any effect:

---
exec_all: true
---

If it would be helpful, I’d be happy to create a bug with a full repo for reproducing the issue.

Could you try it again with the current master branch? Setting enabled: true should work now I think.

I tried that, but I am still having issues. I created a few bugs with details:

Filed another issue related to widget rendering:

1 Like

@tylere have you tried this in JupyterLab?


IIRC with this you shouldn’t need to do anything special to have widget state appear in quarto.

@jeremy I hadn’t, but now I have enabled the Save Widget State Automatically (SWSA) option, I still can’t view the widget output with nbdev_preview.

The SWSA option doesn’t seem to work as I would expect. Here is my mini-experiment.

Verify that the SWSA option is enabled:

Restart the kernel and run the cells so it displays the widget output in JupyterLab:

Change the state of the widget slider output (to 42):

Save the notebook. Close the notebook tab and kernel:

Reopen the notebook. Note that the widget is not fully rendered, and the text representation shows a different state (value=0) than when I previously saved the notebook.

Potential “trust” issue?

I also note that the JupyterLab interface indicates the notebook is untrusted
image
even after I run
nbdev_trust --force_all

The classic notebook shows the same lack of trust:

Perhaps there is a problem with indicating that the notebook is trusted, which prevents saving the widget output?

I found some related discussions within the JupyterLab channels:

My next step will probably be to explore more about how notebook trust is stored in the metadata…

Thanks for checking. @seem has also noted that jupyter trust seems pretty broken in general at the moment. It’s not done with nb metadata iiuc, but using a separate database:

https://jupyter-notebook.readthedocs.io/en/stable/security.html#security-in-notebook-documents

This is now being tracked as a quarto bug: Embedded `ipywidgets` are not rendered · Issue #2445 · quarto-dev/quarto-cli · GitHub

Some other points worth noting:

  • I don’t think trust affects embedded widgets. I think it only affects whether widgets are rendered in the Jupyter Lab interface
  • nbdev_clean currently removes widget state from the notebook, even if you enable the “Save Widget State Automatically” option in Jupyter Lab. We might want to change that (or add a setting)
  • However, execute: {enabled: true} reruns widgets on quarto render which recreates widget state in the notebook file, so it’s an ok workaround
2 Likes

I’d guess we definitely want to change that. AFAICT you’d never want to remove widget state if it’s there, since it’s removed by default – if it’s there then someone has made a conscious decision to make that happen!

1 Like

Makes sense! Tracking here: `nbdev_clean` removes widget state · Issue #1069 · fastai/nbdev · GitHub

1 Like