最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论