user3550647
user3550647

Reputation: 119

How to display interact() in tab in ipywidgets Python

I am trying to display an interact function in an ipywidget tab. The code below is my attempt to do this but I only see the dropdown list but the plot is not shown. I also included the interact() function in isolation to show the way it works out of the tab. Any help on this is much appreciated. Thank you.

from ipywidgets import *
import seaborn.apionly as sns
df = sns.load_dataset('iris')

#plot
def plot_image(x):

    data = df

    if x != 'Select':
        xplot = data[x]
        sns.distplot(xplot)
        plt.show()

#define widgets
x = widgets.Dropdown(
        options=df_cols,
        value=df_cols[0],
        description='X'
    )

x.set_title  = 'x'

#assign widgets to tabs
tab_visualise = widgets.HBox([x])

#create tabs
tab_nest = widgets.Tab()
tab_nest.children = [tab_visualise]
tab_nest.set_title(0, 'Visualise')
tab_nest

#interact function in isolation
interact(plot_image, x = x)

Upvotes: 2

Views: 5199

Answers (1)

ac24
ac24

Reputation: 5565

Try using an interactive instead. This returns the input and output widgets, which you can combine into a VBox, and then add as a child to your tab_list

from ipywidgets import *
import seaborn.apionly as sns
df = sns.load_dataset('iris')
import matplotlib.pyplot as plt

#plot
def plot_image(x):

    data = df

    if x != 'Select':
        xplot = data[x]
        sns.distplot(xplot)
        plt.show()

#define widgets
x = widgets.Dropdown(
        options=df.columns,
        value=df.columns[0],
        description='X'
    )

x.set_title  = 'x'

#assign widgets to tabs
tab_visualise = widgets.HBox([x])

#create tabs
tab_nest = widgets.Tab()
# tab_nest.children = [tab_visualise]
tab_nest.set_title(0, 'Visualise')


#interact function in isolation
f = interactive(plot_image, x = x);
tab_nest.children = [VBox(children = f.children)]
display(tab_nest)

enter image description here

Upvotes: 8

Related Questions