Streamlit 是一个免费的开源框架,用于快速构建和共享漂亮的机器学习和数据科学 Web 应用程序。它是一个专门为机器学习工程师设计的基于 Python 的库。数据科学家或机器学习工程师不是 Web 开发人员,对于他们中的大部分人并不太愿意花费数周时间学习使用这些框架来构建 Web 应用程序。相反,他们想要一个更易于学习和使用的工具,只要它可以显示数据并收集建模所需的参数。Streamlit 只需几行代码即可创建外观精美的应用程序。
Streamlit 最好的一点是,甚至不需要了解 Web 开发的基础知识即可开始或创建一个 Web 应用程序。 因此,如果你是数据科学领域的专家,并且想要轻松、快速地部署模型,并且只需要几行代码,那么 Streamlit 非常适合。
使应用程序成功的重要方面之一是他为我们提供了有效且直观的用户界面,无需采取复杂的步骤,特别是对于没有前端知识的人将他们的代码放入 Web 应用程序,构建惊艳的交互式可视化大屏:
无需前端(html、js、css)经验或知识。
不需要花费数天或数月的时间来创建一个 Web 应用程序,可以在几个小时甚至几分钟内创建一个非常漂亮的机器学习或数据科学应用程序。
import streamlit as st import pandas as pd import numpy as np import plotly.express as px from plotly.subplots import make_subplots import plotly.graph_objects as go import matplotlib.pyplot as plt
st.write():此函数用于向 Web 应用程序添加任何内容,从格式化字符串到 matplotlib 图表、Altair 图表、绘图图表、数据框、Keras 模型等中的图表。
import streamlit as st st.write("Hello ,let's learn how to build a streamlit app together")
st.title():用于添加应用程序的标题
st.header():用于设置节的标题
st.subheader():用于设置节的子标题
st.text():用于编写特定图形的描述
st.markdown():用于将文本显示为 Markdown
st.latex():用于在仪表板中显示数学表达式
st.write():用于显示所有内容,例如绘图、数据框、函数、模型等
st.sidebar():用于在侧边栏上显示数据
st.dataframe():用于显示数据框
st.map():用于以单行代码等显示地图
st.caption():用于写字幕
st.code():用于设置密码
st.title ("this is the app title") st.header("this is the markdown") st.markdown("this is the header") st.subheader("this is the subheader") st.caption("this is the caption") st.code("x=2021") st.latex(r''' a+a r^1+a r^2+a r^3 ''')
st.checkbox('yes') st.button('Click') st.radio('Pick your gender',['Male','Female']) st.selectbox('Pick your gender',['Male','Female']) st.multiselect('choose a planet',['Jupiter', 'Mars', 'neptune']) st.select_slider('Pick a mark', ['Bad', 'Good', 'Excellent']) st.slider('Pick a number', 0,50)
st.number_input():用于显示数字输入小部件
st.text_input():用于显示文本输入小部件
st.date_input():用于显示日期输入小部件以选择日期
st.time_input():用于显示时间输入小部件以选择时间
st.text_area():用于显示具有多行文本的文本输入小部件
st.file_uploader():用于显示文件上传器小部件
st.color_picker():用于显示颜色选择器小部件以选择颜色
st.number_input('Pick a number', 0,10) st.text_input('Email address') st.date_input('Travelling date') st.time_input('School time') st.text_area('Description') st.file_uploader('Upload a photo') st.color_picker('Choose your favorite color')
使用 Streamlit 显示进度和状态
看到如何向应用程序添加进度条和状态消息,例如错误和成功。
st.balloons():用于显示气球庆祝
st.progress():用于显示进度条
st.spinner():用于在执行期间显示临时等待消息
st.balloons() st.progress(10) with st.spinner('Wait for it...'): time.sleep(10)
st.success():用于显示成功消息
st.error():用于显示错误消息
st.warnig():用于显示警告信息
st.info():用于显示信息性消息
st.exception():用于显示异常消息。
st.success("You did it !") st.error("Error") st.warnig("Warning") st.info("It's easy to build a streamlit app") st.exception(RuntimeError("RuntimeError exception"))
datatable = get_table() st.markdown("### 世界各国家的Covid-19病例分析") st.markdown("下表为您提供了 %s 国家Covid-19总新增病例、累计病例、新增死亡病例和累计死亡病例的实时分析。"% (select)) st.dataframe(datatable) # will display the dataframe st.table(state_total)# will display the table