當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Streamlit st.form用法及代碼示例

創建一個將元素與"Submit" 按鈕一起批處理的表單。

表單是一個容器,它將其他元素和小部件可視地組合在一起,並包含一個提交按鈕。當按下表單的提交按鈕時,表單內的所有小部件值將被批量發送到 Streamlit。

要將元素添加到表單對象,您可以使用"with" 表示法(首選)或直接在表單上調用方法。請參閱下麵的示例。

表單有一些限製:

  • 每個表格必須包含一個st.form_submit_button.
  • st.buttonst.download_button不能添加到表單中。
  • 表單可以出現在應用程序的任何位置(側邊欄、列等),但它們不能嵌入到其他表單中。

有關表格的更多信息,請查看我們的blog post

函數簽名

st.form(key, clear_on_submit=False)
參數說明

key (str)

標識表單的字符串。每個表單都必須有自己的 key 。 (此鍵在接口中不顯示給用戶。)

clear_on_submit (bool)

如果為 True,則在用戶按下提交按鈕後,表單內的所有小部件都將重置為其默認值。默認為假。 (請注意,自定義組件不受此標誌的影響,並且不會在表單提交時重置為默認值。)

例子

使用 "with" 表示法插入元素:

with st.form("my_form"):
    st.write("Inside the form")
    slider_val = st.slider("Form slider")
    checkbox_val = st.checkbox("Form checkbox")

    # Every form must have a submit button.
    submitted = st.form_submit_button("Submit")
    if submitted:
        st.write("slider", slider_val, "checkbox", checkbox_val)

st.write("Outside the form")

亂序插入元素:

form = st.form("my_form")
form.slider("Inside the form")
st.slider("Outside the form")

# Now add a submit button to the form:
form.form_submit_button("Submit")

相關用法


注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.form。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。