Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter ipywidget: use one slider widget in one specifc tab in notebook

I am trying to create ipywidgets tab and I am looking for one particular numeric widget in a tab, currently, I am seeing both the tabs as common, how can I see size1 in tab[out2] specifically. My existing code looks like following

import ipywidgets as widgets
from ipywidgets import HBox, VBox, Button, Layout, Label
from ipywidgets import interact, interactive, fixed, interact_manual

size = widgets.IntSlider(value=50, min=0,max=100,step=1, description='size:')
size1 = widgets.IntSlider(value=20, min=0,max=50,step=1, description='size1:')

def hist1(size):
    data = pd.DataFrame(np.random.normal(size = size))
    data.plot.hist()
    return

def hist2(size1):
    data = pd.DataFrame(np.random.normal(size = size1))
    data.plot.box()
    return

ui = widgets.VBox([size,size1])

out1 = widgets.interactive_output(hist1, {'size':size})
out2 = widgets.interactive_output(hist2, {'size1':size1})

tab  = widgets.Tab(children = [out1, out2])
tab.set_title(0, 'hist')
tab.set_title(1, 'box')

display(ui,tab)

it's giving me following

enter image description here

but I am seeking to see size1 slider in out2 i.e. box tab, how can I do this with existing code to have a specific slider for tabs

like image 854
Manu Sharma Avatar asked Dec 07 '25 08:12

Manu Sharma


1 Answers

The Interact classes are similarly named but have different functions. widgets.interactive will create a VBox of your input and output widgets, but not display them to screen. You can then pass them as children to your Tab call. No need to display the UI separately.

import ipywidgets as widgets
from ipywidgets import HBox, VBox, Button, Layout, Label
from ipywidgets import interact, interactive, fixed, interact_manual

size = widgets.IntSlider(value=50, min=0,max=100,step=1, description='size:')
size1 = widgets.IntSlider(value=20, min=0,max=50,step=1, description='size1:')

def hist1(size):
    data = pd.DataFrame(np.random.normal(size = size))
    data.plot.hist()
    return

def hist2(size1):
    data = pd.DataFrame(np.random.normal(size = size1))
    data.plot.box()
    return

out1 = widgets.interactive(hist1, size=size)
out2 = widgets.interactive(hist2, size1=size1)

tab  = widgets.Tab(children = [out1, out2])
tab.set_title(0, 'hist')
tab.set_title(1, 'box')

display(tab)
like image 110
ac24 Avatar answered Dec 09 '25 22:12

ac24



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!