Steamlit初识和安装入门
Streamlit 是可以用于快速搭建Web应用的Python库。
Streamlit 基于tornado框架,封装了大量互动组件,同时也支持大量表格、图表、数据表等对象的渲染,并且支持栅格化响应式布局。
1.Streamlit安装
参考我上一篇文章,在虚拟环境中运行
1 | pip install streamlit |
很顺利。
2.展示官方默认的demo
1 | streamlit hello |
会要求输入email,其实不用输 直接回车即可自动跳转到网页
在我的电脑上用IE 打开了 http://localhost:8501/ ,无法显示,改用chrome打开,正常。
3.现在已经安装了所有必要的软件,让我们创建一个first_app.py
1 2 3 4 5 | import streamlit as st # To make things easier later, we're also importing numpy and pandas for # working with sample data. import numpy import pandas |
运行:
1 | streamlit run first_app.py |
4. 添加标题。首先使用ctrl+c来终止streamlit应用的运行
1 2 3 4 5 6 | import streamlit as st # To make things easier later, we're also importing numpy and pandas for # working with sample data. import numpy import pandas st.title( '中文乱码吗?' ) |
没有乱码,然而标题显示位置不是我预想的html>title 相同的位置上,而是显示在页面上
5.查看配置选项
1 | streamlit config show |
6.魔改标题
1 | st.set_page_config(page_title= "中文" ) |
7.获取及显示用户输入
1 2 3 4 5 6 7 8 9 10 11 12 | import streamlit as st # To make things easier later, we're also importing numpy and pandas for # working with sample data. import numpy import pandas st.set_page_config(page_title= "中文" ) st.title( '中文乱码吗?' ) st.header( 'This is a header' ) title = st.text_input( 'Movie title' , 'Life of Brian' ) print(title) st.write( 'The current movie title is' , title) |
8.st.tabs布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import streamlit as st # To make things easier later, we're also importing numpy and pandas for # working with sample data. import numpy import pandas st.set_page_config(page_title= "中文" ) st.title( '中文乱码吗?' ) st.header( 'This is a header' ) title = st.text_input( 'Movie title' , 'Life of Brian' ) print(title) st.write( 'The current movie title is' , title) tab1, tab2, tab3 = st.tabs([ "Cat" , "Dog" , "Owl" ]) with tab1: st.header( "A cat" ) st.image( "https://static.streamlit.io/examples/cat.jpg" , width=200) with tab2: st.header( "A dog" ) st.image( "https://static.streamlit.io/examples/dog.jpg" , width=200) with tab3: st.header( "An owl" ) st.image( "https://static.streamlit.io/examples/owl.jpg" , width=200) |
9.修改pandas,第2次点修改按钮时会发现pandas中的col2已经删除了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import streamlit as st import pandas as pd @st.cache_data def read_df(): df = pd.DataFrame({ 'col1' :[1,2], 'col2' :[ 'A' , 'B' ] }) st.session_state[ 'df' ] = df return df def do_something(): df1 = st.session_state[ 'df' ] df_new = pd.DataFrame({ 'col1' :[1,2], 'col3' :[ "X" , "Y" ] }) df1.drop([ 'col2' ], axis = 1, inplace = True) df1 = df1.merge(df_new, on = "col1" ) st.session_state[ 'df' ] = df1 st.button( "Do Something" , on_click=do_something, args =()) df = read_df() if 'df' not in st.session_state: st.session_state[ 'df' ]=df df = st.session_state[ 'df' ] download_csv = df.to_csv().encode( 'utf-8' ) st.download_button( 'Download' , data = download_csv, file_name = 'download_csv.csv' , mime= 'text/csv' ) |
10.在线Excel表格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import streamlit as st import pandas as pd df = pd.DataFrame( [ { "command" : "st.selectbox" , "rating" : 4, "is_widget" : True}, { "command" : "st.balloons" , "rating" : 5, "is_widget" : False}, { "command" : "st.time_input" , "rating" : 3, "is_widget" : True}, ] ) edited_df = st.experimental_data_editor(df) # 👈 An editable dataframe favorite_command = edited_df.loc[edited_df[ "rating" ].idxmax()][ "command" ] st.markdown(f "Your favorite command is **{favorite_command}** 🎈" ) edited_df.to_csv( 'a.csv' ) |
结尾
使用streamlit我们可以快速构建出可以拿出去给别人看的web demo,但streamlit在我眼中也有个比较大的缺陷,那便是没有区分请求的功能,比如Flask、Fastapi等框架,你可以区分出不同的请求,而streamlit不行,在多人使用时,就会出现,他人在操作页面时,你当前的页面也可能会被影响的情况。
在手机浏览器中也无法正常显示
参考:https://www.cnblogs.com/Chilam007/p/16719995.html
http://cw. hubwiz(这也民感?) .com/card/c/streamlit-manual/1/6/33/
https://docs.streamlit.io/ 官方文档
https://zhuanlan.zhihu.com/p/397129447?utm_id=0
https://blog.csdn.net/weixin_30230009/article/details/126684850
https://www.5axxw.com/questions/content/gtxnho
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人