Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output widget appears outside tab widget when using nbconvert on jupyter notebook with ipywidgets

I created a notebook which should display plots in a tab widget. As far as I understand, to include something like plots in the tab widget, I need to wrap it in the output widget. In the notebook itself it works but when I convert it to html via nbconvert it produces the wrong output.

Widgets like sliders, buttons or text appear within the tab widget where they should, but when I use the output widget to catch the plot (or even some text from a print() function) it appears before the tab environment which itself is then empty.

This is the how it shoud look with one plot per tab (works in the notebook): Plots in tabs within notebook

And this is how it looks after nbconvert (in html). The plots appear before the tab envorinment: Plots before tabs in html

Please note that nbconvert includes other widgets fine and also tabs with other content.

This is the used code:

# Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets as widgets
import numpy as np

# Generated data for plotting
data = pd.DataFrame()
for i in range(5):
    data[i] = np.random.normal(size = 50)

Now this part works in the notebook but not in the html, but as you will see it seems to be related to the output widget, as I does not work with plots or printed text.

# This does not work with plots
children = []
for i in range(data.shape[1]):
    out = widgets.Output()
    with out:
        fig, axes = plt.subplots()
        data[i].hist(ax = axes)
        plt.show()
    children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
    tab.set_title(i, "Plot " + str(i))
tab

# And this does not work with printed output
children = []
for i in range(5):
    out = widgets.Output()
    with out:
        print("This is text", i)
    children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
    tab.set_title(i, "Text " + str(i))
tab

However, if I use a different widget type (e.g. Text) it is displayed correctly in the notebook and the html output from nbconvert.

# This works with the Text widget
children = []
for i in range(5):
    out = widgets.Text(description="P"+str(i))
    children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
    tab.set_title(i, "Text " + str(i))
tab

So, is there something I can change to make this actually work? What I need in the end is a way to display N plots in N tabs...

like image 677
Benedikt Avatar asked Jul 31 '19 07:07

Benedikt


People also ask

How do I enable Ipywidgets in Jupyter notebook?

Simply install the python ipywidgets package with pip (pip install ipywidgets==7.6. 0) or conda/mamba (conda install -c conda-forge ipywidgets=7.6. 0) and ipywidgets will automatically work in classic Jupyter Notebook and in JupyterLab 3.0.

How do I show widgets in Jupyter notebook?

Adding widget code to your Jupyter notebookTo add an interactive widget to your notebook, first add a code cell. Press Alt + Shift + A (Windows) or ⌥ ⇧ A (macOS) to insert a new cell above the currently selected cell, or Alt + Shift + B (Windows) or ⌥ ⇧ B (macOS) to create a new cell below the selected cell.

What is Ipywidgets package?

Java Prime Pack IPyWidgets is a Python library of HTML interactive widgets for Jupyter notebook. Each UI element in the library can respond to events and invokes specified event handler functions. They enhance the interactive feature of Jupyter notebook application.


1 Answers

I had the same problem. Updating nbconvert (for example via pip install --upgrade nbconvert) solved it.

Quoting from https://github.com/jupyter/nbconvert/issues/923 :

MSeal commented on 2020-09-07:

Yes, this issue was resolved in jupyter/nbclient#24 and nbclient >= 0.4.0 has the fix. NBconvert 6.0 (which should be releasing tomorrow) defaults to using nbclient and the issue overall should disappear. You can try it out on the 6.0.0rc0 release today if you like.

like image 98
Kilian Batzner Avatar answered Oct 19 '22 05:10

Kilian Batzner