In an ipywidgets tab, how do I switch to another tab from python?

Viewed 285

I want to switch to another tab from code when some action is completed in jupyter lab, the documentation is rather sparse! Surely there is some method on a tab / accordian etc, where I can make it switch rather than making the user click on the tab?

import ipywidgets as widgets

def goto_tab2(btn):    
    tab2.children[0].value='Been there'
    tabset.switch_tab(1) # what is the method to call here?

def goto_tab1(btn):
#    tabset.switch_tab(0)
    pass
    
tab1 = widgets.VBox([widgets.Text(), widgets.Button(description='set name')])
tab1.children[1].on_click(goto_tab2)

tab2 = widgets.VBox([widgets.Text(), widgets.Button(description='GO BACK!', on_click=goto_tab1)])
tab2.children[1].on_click(goto_tab1)

tabset=widgets.Tab([tab1, tab2])

tabset.set_title(0, 'Tab1')
tabset.set_title(1, 'Tab2')

tabset

1 Answers

Ah! went and had a shower and had a showerthought that led me to selected_index, tried setting it and lo it works! Why does this only happen once you've posted the question?

import ipywidgets as widgets

def goto_tab2(btn):    
    tab2.children[0].value='Been there'
    tabset.selected_index=1

def goto_tab1(btn):
    tabset.selected_index = 0
    
tab1 = widgets.VBox([widgets.Text(), widgets.Button(description='set name')])
tab1.children[1].on_click(goto_tab2)

tab2 = widgets.VBox([widgets.Text(), widgets.Button(description='GO BACK!', on_click=goto_tab1)])
tab2.children[1].on_click(goto_tab1)

tabset=widgets.Tab([tab1, tab2])

tabset.set_title(0, 'Tab1')
tabset.set_title(1, 'Tab2')

tabset
Related