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

9udxz4iz  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(266)

正如人们可以在上面的图像中观察到的,我们有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()
dsekswqp

dsekswqp1#

我遇到了同样的问题,但我设法找到了一种解决方法,方法是将按钮触发的代码 Package 到一个函数中,并使用on_click将该函数作为参数传递给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)

对于其它形式反之亦然。

相关问题