Hello, I’m trying to have some inputs at tabs. However, even though I’m at the first tab, the code also runs the second tab and replaces the value get from the first tab. I’m aware that Streamlit doesn’t support conditional render yet. Any solution or better approach?
import streamlit as st
tabs = st.tabs(['Addition', 'Multiplication'])
with tabs[0]:
x = st.number_input('first number', 0, key='first_tab_first_number')
y = st.number_input('second number', 0, key='first_tab_second_number')
result = x + y
with tabs[1]:
x = st.number_input('first number', 0, key='second_tab_first_number')
y = st.number_input('second number', 0, key='second_tab_second_number')
result = x * y
st.write(result) # This will always show result from tabs[1] even though I open / modified tabs[0]
An option is the tab_bar component from the extra_streamlit_components package (extra-streamlit-components · PyPI) to get that conditional behavior for tabs. Also, imo they look prettier than st.tabs.
import streamlit as st
import extra_streamlit_components as stx
tab = stx.tab_bar(data=[
stx.TabBarItemData(id="add", title="➕ Addition", description="Add two numbers"),
stx.TabBarItemData(id="mult", title="✖ Multiplication", description="Multiply two numbers")])
if tab == "add":
x = st.number_input('first number', 0, key='first_tab_first_number')
y = st.number_input('second number', 0, key='first_tab_second_number')
result = x + y
elif tab == "mult":
x = st.number_input('first number', 0, key='second_tab_first_number')
y = st.number_input('second number', 0, key='second_tab_second_number')
result = x * y
else:
result = "Select an option first"
st.write(f"# The result is {result}")
st.warning("""`pip install extra_streamlit_components`""")
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.