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代码

相关推荐
caihuayuan56 分钟前
前端面试2
java·大数据·spring boot·后端·课程设计
郭尘帅66628 分钟前
SpringBoot学习(上) , SpringBoot项目的创建(IDEA2024版本)
spring boot·后端·学习
野犬寒鸦1 小时前
MySQL索引详解(下)(SQL性能分析,索引使用)
数据库·后端·sql·mysql
.生产的驴5 小时前
SpringBoot 集成滑块验证码AJ-Captcha行为验证码 Redis分布式 接口限流 防爬虫
java·spring boot·redis·分布式·后端·爬虫·tomcat
野犬寒鸦7 小时前
MySQL索引使用规则详解:从设计到优化的完整指南
java·数据库·后端·sql·mysql
思考的橙子7 小时前
Springboot之会话技术
java·spring boot·后端
兆。9 小时前
电子商城后台管理平台-Flask Vue项目开发
前端·vue.js·后端·python·flask
weixin_4373982110 小时前
RabbitMQ深入学习
java·分布式·后端·spring·spring cloud·微服务·rabbitmq
西京刀客14 小时前
Go多服务项目结构优化:为何每个服务单独设置internal目录?
开发语言·后端·golang
李匠202415 小时前
C++GO语言微服务之gorm框架操作MySQL
开发语言·c++·后端·golang