I was wondering of there is a way to keep the selected tab when i issue an st.rerun. I was thinking about storing that information in the session_state but have not found a way to get this information to be able to store it into the sessions state nor be able to use it for this purpose after retrieving it from the session during a rerun.
I would greatly appreciate some help in this regard or pointers on how to achieve this result with a different streamlit component.
Hello,
What do you mean by āselected tabā ? Do you have a sample of code ?
You can store anything in the session state. So iām sure we can make it work with the st.rerun
Some code, but iām not sure what do you wsant to achieve with the selected tab ! Hope it help
# Initialize session state for tab selection if it does not exist
if 'selected_tab' not in st.session_state:
st.session_state.selected_tab = "Tab 1" # Default to the first tab
# Define a function to handle tab changes
def on_tab_change(tab_name):
# Update session state with the current tab name or index
st.session_state.selected_tab = tab_name
st.rerun()
# Define tabs
tabs = ["Tab 1", "Tab 2", "Tab 3"]
# Create a selectbox for tab selection and set the default value
selected_tab = st.selectbox("Select a tab", tabs, index=tabs.index(st.session_state.selected_tab),
key="tab_selector")
# Update session state when tab is selected
if selected_tab != st.session_state.selected_tab:
on_tab_change(selected_tab)
# Display content based on selected tab
if selected_tab == "Tab 1":
st.write("You are in Tab 1")
elif selected_tab == "Tab 2":
st.write("You are in Tab 2")
elif selected_tab == "Tab 3":
st.write("You are in Tab 3")
thanks for your reply, i think i should have been more precise.
Currently we have:
tab1, tab2, tab3, tab4 = st.tabs("1", "2", "3", "4")
with tab1:
func1()
with tab2:
...
Whenever i have to trigger a rerun somewhere this selection swaps back to tab1 and i do not know how to extract or set from the code which of these tabs is currently āactiveā.
i use the example in the st.tabs - Streamlit Docs and everything stay active, i donāt have any swap.
# Initialize session state for tab selection if it does not exist
tab1, tab2, tab3 = st.tabs(["Cat", "Dog", "Owl"])
with tab1:
st.header("A cat")
st.image("https://static.streamlit.io/examples/cat.jpg", width=200)
with tab2:
st.header("A dog")
st.image("https://static.streamlit.io/examples/dog.jpg", width=200)
with tab3:
st.header("An owl")
st.image("https://static.streamlit.io/examples/owl.jpg", width=200)
if st.button('f'):
st.rerun()
Interesting, i can reproduce it staying with this example. But in the bigger application it does reset on every action that calls st.rerun() that i tested.
Do you know what could be causing this or how could circumvent it anway by explicitely specifying the active one somewhere?
Unfortunately i do not have much experience with streamlit yet and not too much time to try to produce a minimal reproducer.
I canāt respond, but the other technique to avoid the problem is with the code before with a selectbox(). Not as nice as the st.tabs but maybe can āfixā it for now