I have an app that uses tabs. Whenever it is rerun from inside one of the tab, the app reloads with selected tab the first one, and not the one from where the reload was triggered. Example code below:
import streamlit as st
import pandas as pd
def click_function():
st.warning("I have clicked the button!")
data = pd.DataFrame({"name": ["cat", "dog", "owl"]})
st.session_state['success'] = False
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)
animal = st.selectbox("Select which animal do you like more", data.name, index = None)
if not st.session_state['success']:
st.button("Click me", on_click=click_function)
Sorry, maybe I did not explain it correctly, but for example on the attached code, there is a button inside tab3, when the button is clicked, the app reloads itself and you appear to tab1. My desired behaviour is to stay on tab3 after clicking the button.
I missed the more idiomatic “rerun” in the body of the post.
I would call that a bug, there is no apparent reason for the selected tab changing. That said, I have seen unexpected things happening when callbacks try to change the UI, so I’ve gotten used to not doing it. The simplest way to avoid that in your case is probably getting rid of the callback and just checking the return value of the button:
if st.button("Click me"):
st.warning("I have clicked the button!")
But that would put the warning below the button. If you want it at the top of the page, you can use a container.
import pandas as pd
import streamlit as st
data = pd.DataFrame({"name": ["cat", "dog", "owl"]})
st.session_state["success"] = False
container = st.container()
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)
animal = st.selectbox("Select which animal do you like more", data.name, index=None)
if not st.session_state["success"]:
if st.button("Click me"):
with container:
st.warning("I have clicked the button!")
The main issue is that st.tabs is not integrated with the state of the app. A request for that to be changed/fixed is old but it’s still open…
With version 1.40, they introduced what could be an alternative with st.segmented_control. It’s basically another style for a selectbox, so the logic is going to be closer to those widgets that to the contexts managers used in st.tabs:
Code:
import streamlit as st
import pandas as pd
def click_function():
st.warning("I have clicked the button!")
data = pd.DataFrame({"name": ["cat", "dog", "owl"]})
st.session_state["success"] = False
tab = st.segmented_control(
"Tab options",
["Cat", "Dog", "Owl"],
selection_mode="single",
default="Cat",
label_visibility="collapsed",
)
if tab == "Cat":
st.header("A cat")
st.image("https://static.streamlit.io/examples/cat.jpg", width=200)
elif tab == "Dog":
st.header("A dog")
st.image("https://static.streamlit.io/examples/dog.jpg", width=200)
elif tab == "Owl":
st.header("An owl")
st.image("https://static.streamlit.io/examples/owl.jpg", width=200)
animal = st.selectbox("Select which animal do you like more", data.name, index=None)
if not st.session_state["success"]:
st.button("Click me", on_click=click_function)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.