3

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

Manu Sharma
  • 1,593
  • 4
  • 25
  • 48

1 Answers1

1

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)
ac24
  • 5,325
  • 1
  • 16
  • 31