无需了解任何Web框架,数据科学项目也可被轻而易举地转换成出色的应用程序。
Python之禅:简胜于繁。Streamlight便是诠释它最好的注脚,使创建web应用程序从未如此简单。
pip install streamlit
streamlit hello
屏幕会显示:
Streamlit Hello World
在终端上运行:
streamlit run helloworld.py
瞧,这便使您能够在连接到本地主机浏览器localhost:8501的中看到一个简单的应用程序,利用它可以允许移动滑块并给出结果。
操作相当简单,在上述应用程序中,用到了StreamLit的两个功能:
st.slider 小部件命令,实现滑动滑块以更改Web应用程序的输出的效果;
st.write 多功能命令,它居然能利用图表、数据和简单的文本写出任何东西。稍后会详述该功能。
1. 滑块
2. 文本输入
3. 复选框
一个简单的复选框小部件应用程序
4. 选择框
简单的下拉框/选择框小部件应用程序
5. 多选择
import streamlit as st
import pandas as pd
import numpy as npdf = pd.read_csv("football_data.csv")options = st.multiselect(
'What are your favorite clubs?',
df['Club'].unique())st.write('You selected:', options)
import streamlit as st
import pandas as pd
import numpy as npdf = pd.read_csv("football_data.csv")clubs = st.multiselect('Show Player for clubs?', df['Club'].unique())nationalities = st.multiselect('Show Player from Nationalities?', df['Nationality'].unique())# Filter dataframe
new_df = df[(df['Club'].isin(clubs)) & (df['Nationality'].isin(nationalities))]# write dataframe to screen
st.write(new_df)
综合使用多个小部件
import streamlit as st
import pandas as pd
import numpy as np
import plotly_express as pxdf = pd.read_csv("football_data.csv")clubs = st.multiselect('Show Player for clubs?', df['Club'].unique())
nationalities = st.multiselect('Show Player from Nationalities?', df['Nationality'].unique())new_df = df[(df['Club'].isin(clubs)) & (df['Nationality'].isin(nationalities))]
st.write(new_df)# create figure using plotly express
fig = px.scatter(new_df, x ='Overall',y='Age',color='Name')# Plot!
st.plotly_chart(fig)
Adding charts
添加图表
1. 缓存
2. 工具条
import streamlit as st
import pandas as pd
import numpy as np
import plotly_express as pxdf = st.cache(pd.read_csv)("football_data.csv")clubs = st.sidebar.multiselect('Show Player for clubs?', df['Club'].unique())
nationalities = st.sidebar.multiselect('Show Player from Nationalities?', df['Nationality'].unique())new_df = df[(df['Club'].isin(clubs)) & (df['Nationality'].isin(nationalities))]
st.write(new_df)# Create distplot with custom bin_size
fig = px.scatter(new_df, x ='Overall',y='Age',color='Name')# Plot!
st.plotly_chart(fig)
Move widgets to the sidebar
3.Markdown?(一种纯文本格式的标记语言)
import streamlit as st
import pandas as pd
import numpy as np
import plotly_express as px'''
# Club and Nationality AppThis very simple webapp allows you to select and visualize players from certain clubs and certain nationalities.
'''
df = st.cache(pd.read_csv)("football_data.csv")clubs = st.sidebar.multiselect('Show Player for clubs?', df['Club'].unique())
nationalities =
st.sidebar.multiselect('Show Player from Nationalities?', df['Nationality'].unique())new_df = df[(df['Club'].isin(clubs)) & (df['Nationality'].isin(nationalities))]
st.write(new_df)# Create distplot with custom bin_size
fig = px.scatter(new_df, x ='Overall',y='Age',color='Name')'''
### Here is a simple chart between player age and overall
'''st.plotly_chart(fig)
结论
作者简介:
Rahuil Agarwal,Walmart实验室的数据科学家。
原文标题:
How to Write Web Apps Using Simple Python for Data Scientists
原文链接:
https://www.kdnuggets.com/2019/10/write-web-apps-using-simple-python-data-scientists.html