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.
Cheers
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")
Hi,
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”.
Cheers
My bad, i didn’t use the st.tabs.
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.
Cheers
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
Thanks a lot, i think that will work for now.
I now just have one question. Originall we have the tabs as
[":briefcase: Name1", :"briefcase: Name2, ":file_folder: Name3", ..."]
It it was showing them properly as “
Name1”, :“briefcase: Name2, “
Name3”, …”
But now i just get the raw text. I guess the options argument that not support that for select box, only the label?
You can directly copy the emoji, it will work.
st.selectbox('test', ['value🏭','💼value2','🏭'])
