对于从事数据科学的人来说,并不一定都具有web开发背景,虽然Python生态中有Flask,FastAPI这样较为轻量的Web开发框架,但对于数据科学相关人员而言,依旧需要一定量的学习,streamlit的出现,使得我们可以较快地以少量代码完成目标:比如数据分析结论的分享展示、交互式的ML,DL应用、地图相关的可视化应用等。
本例中,通过Streamlit仅用几行python代码就完成一个漂亮的小应用
本文中,我们简单构建一个App,其中只包含两副股票折线图
前提
安装streamlit只需要pip install streamlit即可
本例中,为了获取某只股票代码的数据,我们还需依赖yfinance这个包
代码
import streamlit as st
import yfinance as yf
# st.write中可以使用Markdown标记语言
st.write("""
# Simple Stock Price App
Shown are the stock **closing price** and volume of Google!
""")
# define the ticker symbol 股票代码
ticker_symbol = 'GOOGL'
# get data on this ticker
ticker_data = yf.Ticker(ticker_symbol)
# get the historical prices for this ticker
ticker_df = ticker_data.history(period='1d', start='2010-5-31', end='2020-5-31')
# Open High Low close Volume Dividends Stock Splits
st.line_chart(ticker_df.Close)
st.line_chart(ticker_df.Volume)
界面效果
用户可以在图上伸缩,完成一些交互操作,如需要返回默认显示状态,则用左键双击图
可以看到,仅仅几行代码就能做出这样的效果,个人感觉从事数据科学的小伙伴们,可以投入时间掌握Streamlit,它对日常工作很有帮助。