【问题标题】:Script rerun when i change selectbox in Streamlit当我在 Streamlit 中更改选择框时脚本重新运行
【发布时间】:2021-09-02 16:38:15
【问题描述】:

当我更改选择框选项时脚本重新运行,我知道流式代码从顶部运行每个操作,并且有 2 个解决方案(缓存和 SessionState)。

我测试了缓存它不起作用,对于 SessionState 我不知道如何实现它。

这是代码

import streamlit as st
col1, col2, col3 = st.beta_columns(3)
if col1.button("CARTE"):
    st.write("Hello")
    sel_Map = st.selectbox("Choose Map type :", options=['Hello1', 'Hello2'], index=1)
    if sel_Map == 'ChoroplethMap':
        st.write("Hello world! 1")

    elif sel_Map == 'ChoroplethMapBox':
        st.write("Hello world! 2")

if col2.button("TABLEAU"):
    st.write("Hello world! 3")

if col3.button("SYNTHÈSE"):
    st.write("Hello world! 4")

【问题讨论】:

    标签: python-3.x session-state streamlit


    【解决方案1】:

    我想出了如何使用 SessionState 所以结果很好,这就是解决方案

    import streamlit as st
    from src.pages.sessionState import SessionState
    
    
    session_state = SessionState.get(col1=False, col2=False, col3=False)
    
    col1, col2, col3 = st.beta_columns(3)
    
    col1_one = col1.button("CARTE", key="1")
    col2_one = col2.button("TABLEAU", key="2")
    col3_one = col3.button("SYNTHÈSE", key="3")
    
    if col1_one or session_state.col1:
        session_state.col1 = True
        session_state.col2 = False
        session_state.col3 = False
        sel_Map = st.selectbox("Choose Map type :", options=['Hello1', 'Hello2'], index=1)
        if sel_Map == 'Hello1':
            st.write("Hello world! 1")
        elif sel_Map == 'Hello2':
            st.write("Hello world! 2")
    
    if col2_one or session_state.col2:
        session_state.col1 = False
        session_state.col2 = True
        session_state.col3 = False
        st.write("Hello world! 3")
    
    if col3_one or session_state.col3:
        session_state.col1 = False
        session_state.col2 = False
        session_state.col3 = True
        st.write("Hello world! 4")
    

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多