Streamlit - python 快速生成UI框架

参考文档:Streamlit docs

安装 streamlit 环境

  • python 项目下终端执行:pip install streamlit
  • 验证:streamlit hello , 有跳转即成功

完成

效果

  • py 代码

    api_test.py 复制代码
    import streamlit as st
    import requests
    
    st.title("接口测试器")
    
    # url 接口地址,默认本地启动的地址
    def test_api(url):
        try:
            response = requests.get(url)
            if response.status_code == 200:
                st.success(f"接口 {url} 请求成功!")
                try:
                    data = response.json()
                    st.json(data)
                except ValueError:
                    st.text("响应不是有效的 JSON 数据")
                    st.text(response.text)
            else:
                st.error(f"接口 {url} 返回错误状态码: {response.status_code}")
                st.text(response.text)
        except Exception as e:
            st.error(f"接口 {url} 请求失败: {e}")
    
    # 输入框和按钮1
    url1 = st.text_input("接口1 URL", "http://localhost:8080/ping")
    if st.button("发送请求 到接口1"):
        test_api(url1)
    
    st.markdown("---")  # 分割线
    
    # 输入框和按钮2
    url2 = st.text_input("接口2 URL", "http://localhost:8080/hello")
    if st.button("发送请求 到接口2"):
        test_api(url2)
    
    # 你可以继续添加更多输入框和按钮,方式类似
  • 运行:streamlit run .\api_test.py

  • UI 界面

tips

可以使用 AI 生成需要的UI代码

相关推荐
云上凯歌9 分钟前
02 Spring Boot企业级配置详解
android·spring boot·后端
秋饼21 分钟前
【手撕 @EnableAsync:揭秘 SpringBoot @Enable 注解的魔法开关】
java·spring boot·后端
IT_陈寒30 分钟前
Python 3.12 新特性实战:这5个改进让我的开发效率提升40%
前端·人工智能·后端
利兄的视界31 分钟前
一步到位:M4 芯片 Mac 安装 PostgreSQL 16 并适配 pgvector 教程
后端·postgresql
GZKING31 分钟前
ThinkPHP 8 报错"think\model\pivot" not found
后端
Smoothzjc1 小时前
👉 求你了,别再裸写 fetch 做 AI 流式响应了!90% 的人都在踩这个坑
前端·人工智能·后端
superman超哥1 小时前
Rust 或模式(Or Patterns)的语法:多重匹配的优雅表达
开发语言·后端·rust·编程语言·rust或模式·or patterns·多重匹配
摸鱼的春哥2 小时前
实战:在 Docker (Windows) 中构建集成 yt-dlp 的“满血版” n8n 自动化工作流
前端·javascript·后端
IT 行者2 小时前
Spring Security 7 OAuth2 Token 格式选择浅析
java·后端·spring