Streamlit学习笔记2

GitHub项目3

python 复制代码
git clone https://github.com/kratin01/Basics_Of_Streamlit.git
cd Basics_Of_Streamlit
pip install -r requirements.txt  
streamlit run 01Basic.py

requests

"Python 的浏览器",用来向任意网址发 HTTP 请求

披萨点单小应用,02Widgets.py代码解释
python 复制代码
import streamlit as st
from datetime import date
  • 导入 Streamlit 和日期工具(用来计算年龄)。
python 复制代码
st.image("https://media.istockphoto.com/...")  
  • 在网页最上面显示一张诱人的椒盐披萨图片(从网络加载)。
python 复制代码
st.title("Pizza Order App🍕")
  • 显示大标题:"Pizza Order App🍕"。
python 复制代码
name = st.text_input("Enter Your Name")
  • 出现一个文本输入框,让用户输入姓名,输入的内容会存到 name 变量里。
python 复制代码
DOB = st.date_input("Enter Your Date of Birth")
  • 出现一个日历选择器,让用户选出生日期,选好的日期存到 DOB 变量(类型是 date 对象)。
python 复制代码
if st.button("Save Details"):
    st.success(f"Details Saved Successfully!")
    st.write(f"Hey {name}, we are happy to serving pizza to a person who is {date.today().year - DOB.year} years old")
    st.balloons()
  • 放一个"Save Details"按钮。
  • 用户点按钮后:
    • 显示绿色成功提示。
    • 计算并显示年龄(当前年份减出生年份,注意:没考虑月份,简单粗暴版)。
    • 屏幕上放一群彩色气球动画🎈超可爱!
python 复制代码
category = st.selectbox("Select Your Favorite Pizza Category", ["Veg", "Non-Veg"])
  • 下拉框选择披萨类别:素(Veg)还是荤(Non-Veg)。

然后根据用户选的类别,显示不同的披萨选项(用 if-else 分支):
如果选 Veg(素披萨):

python 复制代码
if category=="Veg":
    pizza = st.selectbox("Select the pizza you want", ["Margherita", "Farmhouse", "Peppy Paneer"])
  • 显示素披萨选项:Margherita、Farmhouse、Peppy Paneer。
    如果选 Non-Veg(荤披萨):
python 复制代码
else:
    pizza = st.selectbox("Select the pizza you want", ["Chicken Tandoori", "Chicken BBQ", "Chicken Supreme"])
  • 显示荤披萨选项:Chicken Tandoori、Chicken BBQ、Chicken Supreme。
python 复制代码
    if pizza:  # 只要用户选了披萨,就显示下面内容
        slider = st.select_slider("Select the size of pizza", options=["Small", "Medium", "Large"])
        if st.button("Order Now"):
            st.success(f"Hey {name}, your {category} {pizza} pizza of size {slider} will be delivered to you in 30 minutes")
  • 用户选好披萨后,出现一个滑动选择器选大小(Small/Medium/Large)。
  • 出现"Order Now"按钮。
  • 点击后显示绿色成功消息:个性化点单确认,比如:"Hey 小明, your Veg Margherita pizza of size Large will be delivered to you in 30 minutes"
    最后是给这个 App 打分的部分:
python 复制代码
st.subheader("Rate the Pizza Order App⭐")
rating = st.slider("Select a rating", 1, 5, 3)
  • 标题 + 一个从1到5的滑块打分(默认3星)。
python 复制代码
if st.button("Submit Rating"):
    if rating < 3:
        st.warning("We will try to improve the app")
    else:
        st.balloons()
        st.success("Thank you for your positive feedback!")
  • 点击提交评分后:
    • 如果打分 < 3 星:显示黄色警告"我们要改进"。
    • 如果 ≥ 3 星:放气球动画 + 绿色感谢消息。
学到了什么 Streamlit 控件(Widgets)
  • st.image() 显示图片
  • st.title() / st.subheader() 标题
  • st.text_input() 文本输入
  • st.date_input() 日期选择
  • st.selectbox() 下拉选择
  • st.select_slider() 滑动选择(带标签的)
  • st.slider() 普通滑块
  • st.button() 按钮
  • st.success() / st.warning() 彩色提示
  • st.balloons() 气球动画(庆祝用)
  • st.write() 显示文字
03Layouts学习 Streamlit 布局
0404DataHandling 数据处理
05WebRequest网络请求
相关推荐
旖旎夜光2 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习
ACP广源盛139246256732 小时前
选型笔记:Type‑C 扩展坞方案评估|DP1.2 转 HDMI2.0 桥接芯片 GSV2201D@ACP
笔记·硬件架构·硬件工程·视频·国产芯片
WEI_YANG_JU3 小时前
初中数学教学中合作学习的研究与实践
学习
码农小韩3 小时前
FPGA学习(一)——LED点灯与开发流程
学习·fpga开发·入门·环境配置·仿真·点灯
李长渊哦4 小时前
Qoder 自定义子智能体(Subagent)学习笔记
chrome·笔记·学习
pixelpilot4 小时前
Deta Surf:把笔记、文件和网页塞进同一个 AI 工作台
笔记·其他
代码山河4 小时前
基本类型转换:自动类型转换与强制类型转换
java·学习·架构·教程·面向对象·项目
蒸蒸yyyyzwd5 小时前
学习笔记 复盘
笔记·八股
旖旎夜光5 小时前
【C++ 基础入门】命名空间、输入输出、缺省参数与函数重载
开发语言·c++·笔记·学习
m4Rk_5 小时前
【论文阅读】Agent 记忆机制(119):EvoLife——让具身 Agent 把主动问到的答案沉淀为可复用的空间经验
论文阅读·人工智能·学习·开源·github