python - How to create a streamlit dashboard that runs only the selected tab - Stack Overflow

admin2025-04-26  10

I'm trying to create a dashboard with a few tabs. The tabs all share common code, and then have specific code to create the tab. I tried using actual tabs, but this runs all the code of all the tabs always. So, for instance, if the user is in tab B, and inputs something, the code will run from the top. It wont run do_commonstuff() but it will run do_A() and do_C() which are independant and dont need to be run again.

I've tried using selectbox instead, but the pages stack up. If I select A, it will print A, then if I select B, it will print B but keep A visible, then if I select A again, it will print it again, and crash because of unique keys.

# User selects which dashboard to view
tab_names = ['A','B','C']
tab = st.selectbox('Select tab', list(tab_names))

# This section I want to run only once
if 'var' not in st.session_state:
    st.session_state['var'] = do_commonstuff()
var = st.session_state['var']

# This section I want to run depending on the selection
if tab == 'A': do_A() # this generates a page, including some user inputs
if tab == 'B': do_B() # also

Please help?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745649957a312110.html

最新回复(0)