无法在 Streamlit 应用程序的同一页面中保留多个表单的输出

Unable to keep the the output of Multiple form in same page in Streamlit app

如上图所示,我们有 form1 f1 和 form2 f2。在这里,我试图显示两种形式的输出。但是当我输入 form1 的提交按钮时,form2 的输出消失了,同样在输入 form2 按钮后,form1 输出也消失了。我很好奇这个。因为提交按钮是不同的。

代码

    import streamlit as st



def main():
    st.title("streamlit Forms & Submit Demo")

    my_form = st.form(key = "form1")
    name = my_form.text_input(label = "Enter the model name")
    number = my_form.slider("Enter your age", min_value=10, max_value = 100 )
    submit = my_form.form_submit_button(label = "Submit this form")
    if submit:
        st.markdown(
            f"""
                            * Select Name : {name}
                            * Select Number : {number}
                            """
        )

    col1, col2 = st.beta_columns(2)

    with col1:

        with st.form('Form2'):
            f1_selectflavor=st.selectbox('Select flavor', ['Vanilla', 'Chocolate'], key=1)
            f1_selectintensity=st.slider(label='Select intensity', min_value=0, max_value=100, key=4)
            submitted1 = st.form_submit_button('Submit 1')
            if submitted1:
                st.markdown(
                    f"""
                    * Select Flavor : {f1_selectflavor}
                    * Select Intensity : {f1_selectintensity}
                    """
                )


    with col2:
        with st.form('Form3'):
            f2_selecttopping=st.selectbox('Select Topping', ['Almonds', 'Sprinkles'], key=2)
            f2_selectintensity=st.slider(label='Select Intensity', min_value=0, max_value=100, key=3)
            submitted2 = st.form_submit_button('Submit 2')
            if submitted2:
                st.markdown(
                    f"""
                    * Select Toppings : {f2_selecttopping}
                    * Select Intensity : {f2_selectintensity}
                    """
                )

    st.text(number)

    st.markdown("Columns inside form")

    with st.form(key='columns_in_form'):
        cols = st.beta_columns(5)
        for i, col in enumerate(cols):
            col.selectbox(f'Make a Selection', ['click', 'or click'], key=i)
        submitted = st.form_submit_button('Submit')



if __name__ == '__main__':
    main()

我遇到了同样的问题,但我设法找到了解决方法,方法是将按钮触发的代码包装到一个函数中,然后使用 [=15] 将该函数作为参数传递给 form_submit_button =].

def trigger():
   st.markdown(
     f"""
      * Select Toppings : {f2_selecttopping}
      * Select Intensity : {f2_selectintensity}
      """)
 
submitted2 = st.form_submit_button('Submit 2', on_click=trigger)

反之亦然。