Hello everyone, I have an app with multiple tabs. Based on a User clicking a button I would like to switch to another tab. I cant seem to get this to work, at all.
Even using the unsafe html code below dosent work:
st.markdown(''' <a target="_self" href="#input">
<button>
Re enter Input
</button>
</a>''', unsafe_allow_html=True)
Any Hints?
PS ALSO at any input/App reload the App always goes back to tab1, Is there any way to stop this behavior???
import streamlit as st
from streamlit.components.v1 import html
a,b,c = st.tabs(["A","B", "C"])
a.title("A")
b.title("B")
c.title("C")
def switch(tab):
return f"""
var tabGroup = window.parent.document.getElementsByClassName("stTabs")[0]
var tab = tabGroup.getElementsByTagName("button")
tab[{tab}].click()
"""
last_row = st.container()
if last_row.button("Switch to A"):
html(f"<script>{switch(0)}</script>", height=0)
if last_row.button("Switch to B"):
html(f"<script>{switch(1)}</script>", height=0)
if last_row.button("Switch to C"):
html(f"<script>{switch(2)}</script>", height=0)
A small warning (that can easily be overcome):
When you add a script to the page, it will run immediately, but it will not run again on other page reruns unless it was first removed. So in this example, if you click the B button, then click the C tab, and go back to click to B button, it wonât switch back to the B tab on the second button click. This is because that script from the button click has been on the page the whole time. The second button click doesnât change anything because the script was already there.
To make this a more robust solution, you could use st.empty and a time.sleep to clear out the script a second after it loads on the page, thereby ensuring every button click is actually âaddingâ a new script to the page which will immediately execute.
Instead of doing it via the Python side, I add some Javascript that adds an event handler to the button, which then triggers a click event to the tab button. The advantage of this is that it all happens on the frontend.
It does require the labels of the buttons and the tab âbuttonsâ to be unique.
Hi Maarten,
Thanks for the demo , looks great. I have a question though how can we do the same without buttons. e.g. I need to perform certain operations first and then move to the next tab or some previous tab based on a logic. Can you please guide ?
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.