st.progress
显示一个随着循环进度更新的进度条。
示例应用
代码
以下展示了如何使用 st.progress
:
import streamlit as st
import time
st.title('st.progress')
with st.expander('About this app'):
st.write('You can now display the progress of your calculations in a Streamlit app with the `st.progress` command.')
my_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.05)
my_bar.progress(percent_complete + 1)
st.balloons()
逐行解释
创建 Streamlit 应用时要做的第一件事就是将 streamlit
库导入为 st
,并且导入要用到的 time
库:
import streamlit as st
import time
接下来为应用创建标题文字:
st.title('st.progress')
用 st.expander
创建一个 About box,在其中用 st.write
显示描述信息:
#用with 进行标题设定和内容输入
with st.expander('About this app'):
st.write('You can now display the progress of your calculations in a Streamlit app with the `st.progress` command.')
最后,我们定义一个进度条,并且以 0 为初值将其实例化。然后一个 for
循环将从 0
遍历至 100
。在每个循环中,我们用 time.sleep(0.05)
来让应用等待 0.05
秒再令 my_bar
进度条数值加 1
,这样能够以图像的形式显示出进度条随每个循环增长。
my_bar = st.progress(0)
#循环设定时间备份比增长加一
for percent_complete in range(100):
time.sleep(0.05)
my_bar.progress(percent_complete + 1)
st.balloons()
显示进度条。这里进度条就是整数在0-100,浮点类型为0.0-1.0之间。
0 <= value <= 100 for int 0.0 <= value <= 1.0 for float |
---|
下面是一个进度条随时间增加并在完成后消失的示例:
import streamlit as st
import time
#进行文本介绍
progress_text = "Operation in progress. Please wait."
#从0开始
my_bar = st.progress(0, text=progress_text)
#百分比和时间计算
for percent_complete in range(100):
time.sleep(0.01)
my_bar.progress(percent_complete + 1, text=progress_text)
time.sleep(1)
my_bar.empty()
st.button("Rerun")
st.form
创建一个将内容组合起来的表单,并且带有一个 "Submit" 提交按钮。
通常情况下,当用户与组件交互的时候,Streamlit 应用就会重新运行一遍。
表单是是一个视觉上将元素和组件编组的容器,并且应当包含一个提交按钮。在此之中,用户可以与一个或多个组件进行任意次交互都不会触发重新运行。直到最后提交按钮被按下时,所有表单内组件的数值会一次性更新并传给 Streamlit。
你可以使用 with
语句来向表单对象添加内容(推荐),或者也可以将其作为一个对象直接调用其对象方法(即首先将表单组件存入一个变量,随后调用该变量的 Streamlit 方法)。可见样例应用。
表单有一些限制:
st.form_submit_button
对象st.button
和 st.download_button
将无法在表单中使用更多有关表单的信息,详见我们的 博客帖。
代码
以下展示了如何使用 st.form
:
import streamlit as st
st.title('st.form')
# Full example of using the with notation
st.header('1. Example of using `with` notation')
st.subheader('Coffee machine')
with st.form('my_form'):
st.subheader('**Order your coffee**')
# Input widgets
coffee_bean_val = st.selectbox('Coffee bean', ['Arabica', 'Robusta'])
coffee_roast_val = st.selectbox('Coffee roast', ['Light', 'Medium', 'Dark'])
brewing_val = st.selectbox('Brewing method', ['Aeropress', 'Drip', 'French press', 'Moka pot', 'Siphon'])
serving_type_val = st.selectbox('Serving format', ['Hot', 'Iced', 'Frappe'])
milk_val = st.select_slider('Milk intensity', ['None', 'Low', 'Medium', 'High'])
owncup_val = st.checkbox('Bring own cup')
# Every form must have a submit button
submitted = st.form_submit_button('Submit')
if submitted:
st.markdown(f'''
☕ You have ordered:
- Coffee bean: `{coffee_bean_val}`
- Coffee roast: `{coffee_roast_val}`
- Brewing: `{brewing_val}`
- Serving type: `{serving_type_val}`
- Milk: `{milk_val}`
- Bring own cup: `{owncup_val}`
''')
else:
st.write('☝️ Place your order!')
# Short example of using an object notation
st.header('2. Example of object notation')
form = st.form('my_form_2')
selected_val = form.slider('Select a value')
form.form_submit_button('Submit&