St.tabs - is it possible to do any of the following?: access an st.tab click event, set a default selected tab on a page

Hi all,

I can’t seem to find much on these topics. Does anyone know:

  1. If it’s possible to / how to access the click events of an st.tab element?
  2. If it’s possible to set which tab is displayed as selected when a page opens?

If possible, what I would like to do is use the tab click event to navigate to a new page using some previous code I found on the forum and set a default tab as displayed for that page (basically use the tabs for page navigation).

Any help would be appreciated.

1 Like

Unfortunately, no. For now st.tab is purely a visual organizing element, and doesn’t support any sort of programmatic access, either getting or setting the current tab. That’s something we’ll think about for the future, but for now it’s not possible.

OK, thanks for your responding so quickly. I hope to see these features in future, I think they would be very useful indeed.

1 Like

Would like to know, is there any feature we can implement where there are 2 tabs. Clicking on the first tab the sidebar should display the elements and clicking on the second tab we can make the sidebar empty or fill with different elements.

In the meantime, there is the TabBar component from extra-streamlit-components (check the repo here) .

Demo:
tabbarandsidebar

Code:

import streamlit as st
import extra_streamlit_components as stx

st.code("import extra_streamlit_components as stx")
chosen_id = stx.tab_bar(data=[
    stx.TabBarItemData(id="tab1", title="✍️ Write!", description="Display stuff in the sidebar"),
    stx.TabBarItemData(id="tab2", title="💔 Nothing", description="Don't show anything")])

placeholder = st.sidebar.container() 

if chosen_id == "tab1":
    placeholder.markdown(f"## Welcome to `{chosen_id}`")
    placeholder.info(f"Since we are in {chosen_id}, let's add a bunch of widgets")
    placeholder.image("https://placekitten.com/g/400/200",caption=f"Meowhy from {chosen_id}")
    placeholder.slider("A slider",0,10,5,1)
    placeholder.checkbox("A checkbox",True)
    placeholder.button("A button")

elif chosen_id == "tab2":
    placeholder = st.empty()

else:
    placeholder = st.empty()

st.markdown("""
**** 
### Don't forget to `pip install extra_streamlit_components`
# """)
1 Like

Have exactly the same use case here, be great to see it become a feature!

Same here +1 for this feature. Thanks streamlit!

+1 for this feature!
Currently, I have a use-case where the user programmatically adds and removes the “visible” tabs from the sidebar. However, when currently visualizing a tab that is removed (from the sidebar), the screen remains in a blank “ghost tab” until another tab is clicked.

Would love the option to “move” the user to the right tab when deleting the tab currently being shown.

I created a feature request so you can have a place to vote for this where it’s easier for the developers to track: