簡潔大氣的公司網(wǎng)站愛網(wǎng)站關(guān)鍵詞查詢工具
Streamlit 是一個(gè)開源的 Python 庫,用于快速構(gòu)建和共享數(shù)據(jù)應(yīng)用程序。它可以讓開發(fā)者用簡潔的代碼將機(jī)器學(xué)習(xí)模型、數(shù)據(jù)可視化、分析工具等快速構(gòu)建為交互式的 Web 應(yīng)用。
Streamlit 的特點(diǎn)
- 簡單易用:使用 Python 編寫即可構(gòu)建功能強(qiáng)大的 Web 應(yīng)用。
- 實(shí)時(shí)更新:Streamlit 應(yīng)用會(huì)隨著代碼的修改自動(dòng)更新,無需手動(dòng)刷新瀏覽器。
- 支持豐富的組件:包括表單、按鈕、文本框、圖表、圖片等多種交互組件。
- 集成機(jī)器學(xué)習(xí)模型:支持直接加載和展示機(jī)器學(xué)習(xí)模型的輸出,用戶可以通過交互操作改變輸入,并實(shí)時(shí)看到模型預(yù)測結(jié)果。
主要功能
- 可視化數(shù)據(jù):支持通過 Matplotlib、Plotly、Altair 等庫展示交互式圖表。
- 文件上傳:支持用戶上傳文件進(jìn)行處理。
- 組件支持:如按鈕、滑動(dòng)條、文本框、選擇框等。
- 實(shí)時(shí)交互:通過簡單的操作,可以實(shí)現(xiàn)用戶輸入與模型預(yù)測的互動(dòng)。
Streamlit 安裝
可以使用 pip
直接安裝:
pip install streamlit
注意如果你安裝的時(shí)候出現(xiàn)下面的錯(cuò)誤
error: command ‘cmake’ failed: No such file or directory
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pyarrow
Failed to build pyarrow
ERROR: Failed to build installable wheels for some pyproject.toml based projects (pyarrow)
解決方法
- 首先安裝
cmake
conda install cmake
- 然后安裝
pyarrow
conda install pyarrow
注意我這里是使用
conda
進(jìn)行安裝的,如果你在mac上使用brew
安裝可以試一下是否可以成功,但是下面的pyarrow
如果使用pip
安裝失敗,請使用conda
進(jìn)行安裝
安裝這些之后再執(zhí)行
pip install streamlit
就可以成功了
基本用法
- 創(chuàng)建一個(gè)簡單的 Streamlit 應(yīng)用:
import streamlit as st# 設(shè)置網(wǎng)頁標(biāo)題
st.title("Streamlit 示例應(yīng)用")# 在網(wǎng)頁中添加一個(gè)文本框讓用戶輸入數(shù)據(jù)
user_input = st.text_input("請輸入一些文本:")# 添加一個(gè)按鈕,用戶點(diǎn)擊時(shí)執(zhí)行某個(gè)動(dòng)作
if st.button("顯示輸入的文本"):# 顯示用戶輸入的文本st.write("您輸入的文本是:", user_input)# 添加一個(gè)簡單的數(shù)字輸入框,用戶可以選擇數(shù)字
number_input = st.number_input("請選擇一個(gè)數(shù)字", min_value=0, max_value=100)# 顯示數(shù)字輸入框的結(jié)果
st.write(f"你選擇的數(shù)字是:{number_input}")
- 運(yùn)行應(yīng)用:
將上面的代碼保存為 app.py
,然后在命令行運(yùn)行:
streamlit run app.py# You can now view your Streamlit app in your browser.# Local URL: http://localhost:8501
# Network URL: http://10.4.101.18:8501# For better performance, install the Watchdog module:# $ xcode-select --install
# $ pip install watchdog
運(yùn)行上面會(huì)會(huì)彈出下面的頁面
此時(shí),瀏覽器會(huì)自動(dòng)打開顯示你的應(yīng)用。每次修改代碼并保存后,頁面會(huì)自動(dòng)更新,顯示最新的變化。
常用組件
-
按鈕(Button):
if st.button('點(diǎn)擊我'):st.write('按鈕被點(diǎn)擊了!')
-
滑動(dòng)條(Slider):
number = st.slider('選擇一個(gè)數(shù)字', 0, 100, 25) st.write(f'你選擇的數(shù)字是: {number}')
-
選擇框(Selectbox):
option = st.selectbox('選擇一個(gè)選項(xiàng)', ['蘋果', '香蕉', '橙子']) st.write(f'你選擇的選項(xiàng)是: {option}')
-
文件上傳(File uploader):
uploaded_file = st.file_uploader("上傳一個(gè)文件") if uploaded_file is not None:st.write("文件已上傳")
-
文本輸入(Text Input):
user_input = st.text_input("請輸入文本") st.write(f"你輸入的文本是: {user_input}")
使用 Streamlit 構(gòu)建一個(gè)交互式數(shù)據(jù)可視化應(yīng)用
假設(shè)你有一個(gè)簡單的數(shù)據(jù)集并想用 Streamlit 來展示它。以下是一個(gè)展示 Pandas 數(shù)據(jù)框的例子:
import streamlit as st
import pandas as pd# 創(chuàng)建一個(gè)簡單的 DataFrame
data = {'名稱': ['蘋果', '香蕉', '橙子'],'數(shù)量': [10, 20, 30],'價(jià)格': [1.2, 0.5, 0.8]
}
df = pd.DataFrame(data)# 顯示 DataFrame
st.write("水果信息", df)# 使用圖表展示數(shù)據(jù)
st.bar_chart(df.set_index('名稱')['數(shù)量'])
Streamlit 的高級功能
-
緩存:
使用st.cache
可以緩存函數(shù)的結(jié)果,避免重復(fù)計(jì)算,提高性能:@st.cache def load_data():return pd.read_csv('large_dataset.csv')
-
多頁應(yīng)用:
Streamlit 支持在應(yīng)用中創(chuàng)建多個(gè)頁面,通過st.selectbox
或st.radio
來選擇不同的頁面內(nèi)容。 -
自定義組件:
如果需要更復(fù)雜的組件,可以使用streamlit.components
來創(chuàng)建自定義的 HTML、JavaScript 和 CSS 組件。