python streamlit 快速开发网站,比Django 还快的
快速安装Streamlit
Steamlit需要Python 2.7.0 / Python 3.6.x或更高版本。
使用PIP安装Streamlit:
$ pip install streamlit
运行演示应用:
$ streamlit hello
好了!稍等几秒钟,上面的命令会自动在你的浏览器中打开Streamlit演示应用!
快速开发一个在线Excel处理网站
需求设计
根据stremlit 相关的demo,写一个在线处理Excel的应用
该应用主要是读取一个Excel,然后将Excel数据展示处理
设计步骤:
定义一个Button 按钮,点击按钮选择相关的Excel的文件
根据选择的文件,考虑是否需要从缓存中读取数据
随后将数据循环显示出来
代码如下:
import streamlit as st
import pandas as pd
# 定义一个文件上传的组件
upload_file = st.file_uploader('excel文件',type=['xlsx'])
# 判空
if upload_file is None:
st.stop()
# 将数据放到缓存中
@st.cache_data
def load_data(file):
print('从缓存中读取数据')
return pd.read_excel(file,None)
dfs = load_data(upload_file)
names = list(dfs.keys())
# 获取表内的sheet
sheet_selects = st.multiselect('工作表',names,[])
# 如果sheet的长度为0,直接停止
if len(sheet_selects) ==0:
st.stop()
tabs = st.tabs(sheet_selects)
for tab,name in zip(tabs,sheet_selects):
with tab:
df = dfs[name]
st.dataframe(df)