当前位置: 首页 > news >正文

学习streamlit-2

首先视频快速预览下今天的学习内容:

Streamlit Shorts: How to make a button

今天继续学习streamlit,首先激活之前建立的虚拟环境:

❯ conda activate streamlit-env
(streamlit-env)
~ via 🐍 v3.9.16 via 🅒 streamlit-env
❯ 

进入到虚拟环境目录下,新建exercises文件夹,新建day1.py文件:

❯ fd streamlit-env
anaconda3\envs\streamlit-env❯ fd streamlit-env | cd❯ mkdir exercises❯ cd exercises❯ code day1.py

day1.py文件中写下第一行代码:

import streamlit as stst.write('Hello world!')

保存,然后在终端中运行:

❯ streamlit run .\day1.pyYou can now view your Streamlit app in your browser.Local URL: http://localhost:8501Network URL: http://192.168.20.81:8501

自动打开系统默认浏览器窗口并显示 “Hello world!”

恭喜,第一个程序运行起来了!

我们现在给页面加个按钮,实现点按钮后变换显示内容:

# 在day1.py文件中继续编辑,并且不需要关闭之前运行的程序
import streamlit as stst.write('Hello world!')st.header('st.button')if st.button('Say hello'):st.write('Why hello there')
else:st.write('Goodbye')

刷新下网页,会出现按钮:

上面演示了st.button最简单的用法,点击了解更多。

st.button(label, key=None, help=None, on_click=None, args=None, kwargs=None, *, type="secondary", disabled=False, use_container_width=False)

Parameters

  • label(str)

    按钮标签,通常用字符串,支持markdown语法的加粗、斜体、删除线,还支持emojis。

  • key(str or int)

    可选的字符串或整型作为按钮部件的唯一键值,如果留空,则自动根据按键内容生成。

  • help(str)

    可选的当鼠标悬浮在按键上时显示的提示信息。

  • on_click(callable)

    可选的点击按钮的回调。

  • args(tuple)

    可选的传给回调函数的元组参数。

  • kwargs(dict)

    可选的传给回调函数的字典参数。

  • type(“secondary” or “primary”)

    可选的指定按钮类型的参数,默认为"secondary",为普通类型;"primary"则会着重强调。

  • disable(bool)

    可选的布尔类型参数,默认为False,设为True则会禁用按钮。

  • use_container_width(bool)

    可选的布尔类型参数,控制按钮是否匹配父容器宽度。

Returns

  • (bool)

    程序在最后一次运行时按钮被点击则返回True,否则为False

Demo

代码如下:

import streamlit as st
import numpy as np
import timest.write('Hello world!')st.header('st.button')if st.button('Say hello'):st.write('Why hello there')
else:st.write('Goodbye')st.button('**你好**')st.button('*你好*')st.button('~哈哈~')st.button('❤')st.button('❤', key='1', help='This is a heart emoji button')def plotting_demo():progress_bar = st.sidebar.progress(0)status_text = st.sidebar.empty()last_rows = np.random.randn(1, 1)chart = st.line_chart(last_rows)for i in range(1, 101):new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)status_text.text("%i%% Complete" % i)chart.add_rows(new_rows)progress_bar.progress(i)last_rows = new_rowstime.sleep(0.05)progress_bar.empty()# Streamlit widgets automatically run the script from top to bottom. Since# this button is not connected to any other logic, it just causes a plain# rerun.st.button("Re-run")plotting_demo()
http://www.lryc.cn/news/33485.html

相关文章:

  • Vscode中Vue文件保存格式化、 ElementUI、Font Awesome俩大插件使用
  • 汽车标定知识整理(三):CCP报文可选命令介绍
  • kubeadm安装K8S(集群)
  • Baumer工业相机堡盟相机如何使用PnPEventHandler实现相机掉线自动重连(C++新)
  • Windows 命令行基础
  • 面试官: 谈下音视频同步原理,音频和视频能绝对同步吗?
  • CFS三层靶机安装与配置
  • 爬虫入门教程-Spider
  • Python|蓝桥杯进阶第二卷——贪心
  • Chrome开发使用技巧总结
  • 你真的会在阳光下拍照片么?
  • 量化择时——均线策略及改进方法(第1部分—因子测算)
  • 封装几个有用的 Vue3 组合式API
  • MyBatisPlus中的条件构造器Wrapper
  • 类和对象及其构造方法
  • HStream Console、HStreamDB 0.14 发布
  • 参考文献怎么查找,去哪里查找?一篇文章讲明白这些问题
  • docker-compose+HAProxy+Keepalived搭建高可用 RabbitMQ 集群
  • 自动化框架如何搭建?让10年阿里自动化测试老司机帮你搞定!自动化测试脚本怎么写?
  • 剑指 Offer 15. 二进制中1的个数
  • CHAPTER 3 磁盘管理
  • MS python学习(7)
  • 工业物联网“杀手级”应用—预测性维护
  • Java代码弱点与修复之——Explicit null dereferenced(显式空间接引用)
  • 一元导数与多元求导数总结
  • 通过堆栈分析深拷贝、浅拷贝、赋值的差异
  • 网络割接概述
  • 开放开源开先河(下)
  • maven的学习
  • 从前端到后端全面解析文件上传