前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python应用开发——30天学习Streamlit Python包进行APP的构建(3)

Python应用开发——30天学习Streamlit Python包进行APP的构建(3)

作者头像
此星光明
发布2024-05-31 12:53:00
980
发布2024-05-31 12:53:00
举报

st.progress

st.progress 显示一个随着循环进度更新的进度条。

示例应用

代码

以下展示了如何使用 st.progress

代码语言:javascript
复制
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 库:

代码语言:javascript
复制
import streamlit as st 
import time

接下来为应用创建标题文字:

代码语言:javascript
复制
st.title('st.progress')

st.expander 创建一个 About box,在其中用 st.write显示描述信息:

代码语言:javascript
复制
#用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,这样能够以图像的形式显示出进度条随每个循环增长。

代码语言:javascript
复制
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

下面是一个进度条随时间增加并在完成后消失的示例:

代码语言:javascript
复制
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")

🗓️ 天 22

st.form

st.form 创建一个将内容组合起来的表单,并且带有一个 "Submit" 提交按钮。

通常情况下,当用户与组件交互的时候,Streamlit 应用就会重新运行一遍。

表单是是一个视觉上将元素和组件编组的容器,并且应当包含一个提交按钮。在此之中,用户可以与一个或多个组件进行任意次交互都不会触发重新运行。直到最后提交按钮被按下时,所有表单内组件的数值会一次性更新并传给 Streamlit。

你可以使用 with 语句来向表单对象添加内容(推荐),或者也可以将其作为一个对象直接调用其对象方法(即首先将表单组件存入一个变量,随后调用该变量的 Streamlit 方法)。可见样例应用。

表单有一些限制:

  • 所有表单都应当包含一个 st.form_submit_button 对象
  • st.buttonst.download_button 将无法在表单中使用
  • 表单能够出现在你应用的任何地方(包括侧边栏、列等等),唯独不能嵌入另一个表单之中

更多有关表单的信息,详见我们的 博客帖

示例应用

代码

以下展示了如何使用 st.form

代码语言:javascript
复制
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&
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • st.progress
    • 延伸阅读
    • 🗓️ 天 22
    • st.form
      • 示例应用
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档