Tabs and other panel objects not displaying after running jupyter-nbconvert --execute

I would like to be able to non-interactively execute jupyter notebooks containing panel plot elements. Currently this does not work. Interactively the plot elements will be displayed correctly but if I run jupyter-nbconvert --execute nothing is displayed in a cell that has for instance:


import panel as pn
import matplotlib.pyplot as plt

pn.extension()

tabs = pn.Tabs()

fig = plt.figure(figsize=(10, 20), dpi=150)
plt.plot()
pfig = pn.pane.Matplotlib(fig, dpi=144, tight=True)
pfig.width = 800
tabs.append(('test', pfig))
plt.close()

tabs.height = 2000

tabs.embed()

As the last line of code i have tried tabs, and tabs.servable().

It seems to work fine for me. What version of Panel and Matplotlib are you on? There wasn’t any actual plot in your example, so I added one and ran nbconvert on this:

import panel as pn
import matplotlib.pyplot as plt
import numpy as np
pn.extension()


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

tabs = pn.Tabs()

pfig = pn.pane.Matplotlib(fig, dpi=144, tight=True)
pfig.width = 800
tabs.append(('test', pfig))
plt.close()

tabs.height = 600

tabs.embed()

Thanks @riziles.

Versions:
jupyter==1.0.0
matplotlib==3.6.1
panel==0.14.3

I tried adding your code to a notebook (test_panel.ipynb), executing interactively in jupyter-notebook. The tab shows up fine.

If I then run:

jupyter-nbconvert test_panel.ipynb --execute --to=notebook --output test_panel1.ipynb

And open up test_panel1.ipynb in jupyter-notebook then the tab is not visible.

The jupyter-notebook (test_panel.ipynb) and jupyter-nbconvert (test_panel.ipynb) versions differ slightly and the var docs_json json appears to have differences that cause the nbconvert version to not render when opening the notebook in another jupyter-notebook session. In particular, there is a minor reordering of the json that appears to be the problem.

Apologies, I would upload the notebooks as attachements but dont have permission.

Diff of the json:

1c1
< {'121c9091-e889-493e-a969-8e86efa2788a': {'defs': [{'extends': None,
---
> {'c59e08d2-1aa2-4dcd-b5aa-fe7fb1e93c06': {'defs': [{'extends': None,
143a144,153
>                                                                    {'attributes': {'height': 600,
>                                                                                    'margin': [0,
>                                                                                               0,
>                                                                                               0,
>                                                                                               0],
>                                                                                    'min_height': 600,
>                                                                                    'tabs': [{'id': '1004'}],
>                                                                                    'tags': ['embedded']},
>                                                                     'id': '1002',
>                                                                     'type': 'panel.models.tabs.Tabs'},
157,167c167
<                                                                     'type': 'panel.models.markup.HTML'},
<                                                                    {'attributes': {'height': 600,
<                                                                                    'margin': [0,
<                                                                                               0,
<                                                                                               0,
<                                                                                               0],
<                                                                                    'min_height': 600,
<                                                                                    'tabs': [{'id': '1004'}],
<                                                                                    'tags': ['embedded']},
<                                                                     'id': '1002',
<                                                                     'type': 'panel.models.tabs.Tabs'}],
---
>                                                                     'type': 'panel.models.markup.HTML'}],

Meant to type:
The jupyter-notebook (test_panel.ipynb ) and jupyter-nbconvert (test_panel1.ipynb ) versions

I don’t think nbconvert is really designed for this use case. I would try using either Jupyter Cache (one of my favorite tools) or Papermill. Papermill isn’t as good as Wednesday, but it’s a heck of a lot better than Emily in Paris (Netflix produces all three).

I actually started with papermill since I am running a parameterized notebook with panel tabs/columns etc. That didnt work either. Can you confirm that you can:

  1. add your code to an .ipynb file in jupyter-notebook without running it, name it test_panel.ipynb
  2. run the .ipynb file with jupyter-nbconvert test_panel.ipynb --execute --to=notebook --output test_panel1.ipynb
  3. open the test_panel1.ipynb in jupyter-notebook and see the tabs properly rendered without rerunning the notebook?

If that workflow works for you then definitely its something my side though its strange that there is a minor reordering of the json attributes and that reordering would affect rendering.

Coincidentally just watched first episode of wednesday last night :smiley: .

I’m seeing the same behavior you are. Apparently this a known issue with Bokeh and nbconvert:

https://discourse.bokeh.org/t/bokeh-plots-dont-show-up-using-nbconvert-on-terminal/2374/15

Somebody suggested programmatically executing with pyppeteer or playwright. You could also just output to HTML and try to embed that content in the notebook. I’ll try to play around with it some more.

FWIW, it also appears that IPywidgets won’t display properly if you try to programmatically execute a notebook with nbconvert.