pu369com

Steamlit初识和安装入门

Streamlit 是可以用于快速搭建Web应用的Python库。

Streamlit 基于tornado框架,封装了大量互动组件,同时也支持大量表格、图表、数据表等对象的渲染,并且支持栅格化响应式布局。

1.Streamlit安装

参考我上一篇文章,在虚拟环境中运行

 pip install streamlit 

很顺利。

2.展示官方默认的demo

streamlit hello

 会要求输入email,其实不用输 直接回车即可自动跳转到网页

在我的电脑上用IE 打开了 http://localhost:8501/ ,无法显示,改用chrome打开,正常。

3.现在已经安装了所有必要的软件,让我们创建一个first_app.py

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

  运行:

streamlit run first_app.py

4.  添加标题。首先使用ctrl+c来终止streamlit应用的运行

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.查看配置选项

streamlit config show

6.魔改标题

st.set_page_config(page_title="中文")

7.获取及显示用户输入

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布局

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已经删除了

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表格

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

 

posted on 2023-03-16 12:57  pu369com  阅读(832)  评论(0编辑  收藏  举报

导航