Returning to the right tab after clicking button

I have an app with three tabs, and some buttons in the sidebar. When a button is clicked, running a function and processing some things in session_state, the app will always return to the first tab, when I would prefer it to remain on the tab that was open when the process was run.

Any ideas on how to do this? Thanks :+1:

@altanner Unfortunately, as st.tabs works today it’s purely a visual component and there’s no way to interact with it programmatically (e.g. to see which tab was clicked, or force a certain tab to be clicked). The simplest alternative I can recommend is to swap out tabs with something like a selectbox, which by default saves its state between app runs.

1 Like

Thanks for your ongoing help and information @blackary !

Hope this becomes something that Streamlit implement. It’s not a real biggie, but for my app I am trying to make it so users can flit between tabs for different views of their data, plus have action buttons which are always visible in the sidebar. Will keep my eyes open for future updates.

Feel free to put a request for this functionality on Issues · streamlit/streamlit · GitHub – I know a number of people have mentioned this idea on the forum, but I don’t think I see an issue requesting this functionality currently on github.

Well, I think of doing that, but then go “crikey 526 issues… I don’t want to pile on even more… and it’ll just get lost and forgotten in this avalanche anyway”.

But maybe that is normal for a repo? I get anxiety issues if I have more than 2 or 3 issues on a repo. Is trillions of issues just how things work?

It depends what you compare it to. I think for large OSS projects that have a large userbase, having a large number of open issues is fairly normal (see Issues · matplotlib/matplotlib · GitHub for example). That being said, the streamlit open-source team definitely does pay attention to the issues (there are 2k+ closed issues, after all), and especially to issues that get lots of :+1:s. So, even if it doesn’t get addressed right away, having an issue page to discuss a desired feature can be a good place for the community to give feedback about how much they want this new feature, and how they would like to see it work.

I am unable to replicate your issue. The code below displays two tabs and one button in the sidebar. When the button is clicked a function is called and some things in session_state are processed, yet the selected tab doesn’t change --for me at least.

Adding more tabs, buttons, processing and state don’t seem to make any difference.

def do_stuff():
    st.session_state["counter"] += 1

if "counter" not in st.session_state:
    st.session_state["counter"] = 0

if st.sidebar.button("Do stuff"):

tab1, tab2 = st.tabs(["tab1", "tab2"])
tab1.metric("Counter", st.session_state["counter"])
tab2.metric("Squared counter", st.session_state["counter"]**2)

thanks @Goyo - this is a really useful test. let me have a look at what might be triggering a tab reset, for me, in more detail.