1 說(shuō)明:
===初探===
1.1 Streamlit
1.1.1 是一個(gè)開(kāi)源Python庫(kù),可輕松構(gòu)建用于機(jī)器學(xué)習(xí)的漂亮應(yīng)用程序。
1.1.2 是一款專(zhuān)為機(jī)器學(xué)習(xí)工程師創(chuàng)建的免費(fèi)、開(kāi)源 App 構(gòu)建框架。
1.1.3 是一個(gè)簡(jiǎn)單而強(qiáng)大的應(yīng)用程序模型,可讓您快速構(gòu)建豐富的UI。
1.1.4 是第一個(gè)專(zhuān)門(mén)針對(duì)機(jī)器學(xué)習(xí)和數(shù)據(jù)科學(xué)團(tuán)隊(duì)的應(yīng)用開(kāi)發(fā)框架。
1.1.5 是開(kāi)發(fā)自定義機(jī)器學(xué)習(xí)工具的最快的方法,它的目標(biāo)是取代Flask在機(jī)器學(xué)習(xí)項(xiàng)目中的地位,可以幫助機(jī)器學(xué)習(xí)工程師快速開(kāi)發(fā)用戶(hù)交互工具。
1.2 環(huán)境:
華為筆記本電腦、深度deepin-linux操作系統(tǒng)、python3.8和微軟vscode編輯器。
1.3 官網(wǎng):
https://www.streamlit.io/
https://github.com/streamlit/streamlit
https://docs.streamlit.io/en/latest/
2 安裝過(guò)程:
=====
2.1 安裝:
pip install streamlit
#本機(jī)安裝
sudo pip3.8 install streamlit #慢
#推薦安裝方法
sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple streamlit #超快
2.2 第一個(gè)報(bào)錯(cuò):
ERROR: chartify 2.7.0 has requirement bokeh<2.0.0,>=1.4.0, but you'll have bokeh 2.1.0 which is incompatible.
ERROR: chartify 2.7.0 has requirement pandas<1.0.0,>=0.21.0, but you'll have pandas 1.0.4 which is incompatible.
#提示bokeh和pandas的版本均太高了,暫時(shí)忽略看看行不行,因?yàn)槲姨崆鞍惭bbokeh和pandas
2.3 第二個(gè)提醒:
WARNING: The script base58 is installed in '/usr/local/python3.8/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
#解決辦法:建立軟連接,本機(jī)如下
sudo ln -s /usr/local/python3.8/bin/base58 /usr/bin/base58
2.4 第三個(gè)提醒:
WARNING: The script streamlit is installed in '/usr/local/python3.8/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
#解決辦法:建立軟連接,本機(jī)如下
sudo ln -s /usr/local/python3.8/bin/streamlit /usr/bin/streamlit
2.5 驗(yàn)證:查看版本,成功。
xgj@xgj-PC:~$ streamlit --version
Streamlit, version 0.61.0
3 查看demo
=========
3.1 第一次,打開(kāi)終端輸入:
streamlit hello
3.2 需要輸入個(gè)人email:
Welcome to Streamlit!
If you're one of our development partners or you're interested in getting
personal technical support or Streamlit updates, please enter your email
address below. Otherwise, you may leave the field blank.
#需要輸入自己的email地址
Email: xxx@xxx
#回車(chē),出現(xiàn)下面,并自動(dòng)打開(kāi)瀏覽器
Privacy Policy:
As an open source project, we collect usage statistics. We cannot see and do
not store information contained in Streamlit apps. You can find out more by
reading our privacy policy at: https://streamlit.io/privacy-policy
If you'd like to opt out of usage statistics, add the following to
~/.streamlit/config.toml, creating that file if necessary:
[browser]
gatherUsageStats = false
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://192.168.12.106:8501
#按ctrl+c退出
3.3 第二次進(jìn)入,如下操作和示意圖:
4 st.image:
========
4.1 方法一:官方推薦法,缺點(diǎn)gif圖片顯示靜態(tài)的
import streamlit as st
#官方的顯示圖片法一
from PIL import Image #采用PIL讀取圖片
image = Image.open('/home/xgj/Desktop/Streamlit/20.jpeg')
#image = Image.open('/home/xgj/Desktop/Streamlit/21.jpg')
#image = Image.open('/home/xgj/Desktop/Streamlit/22.png')
#image = Image.open('/home/xgj/Desktop/Streamlit/23.gif') #靜態(tài)的圖片
#caption='顯示圖片',圖片信息;width=300,指定圖片寬度,自動(dòng)縮放;默認(rèn)是圖片本身的大小
st.image(image, caption='顯示圖片',width=300)
4.2 方法二:
import streamlit as st
#顯示圖片法二,推薦這種方法
#pic_file = open('/home/xgj/Desktop/Streamlit/12.gif', 'rb') #gif可以動(dòng)態(tài)顯示
pic_file = open('/home/xgj/Desktop/Streamlit/21.jpg', 'rb')
pic_bytes = pic_file.read()
st.image(pic_bytes)
4.3 效果圖:
#注意代碼塊中采用#注釋?zhuān)绻捎胮ython的'''和'''大片狀注釋法會(huì)在網(wǎng)頁(yè)上顯示代碼塊,不建議。
#如果修改代碼塊,保存后,因?yàn)榻K端啟動(dòng)服務(wù)器,所以只需要網(wǎng)頁(yè)刷新即可看到效果
#圖片顯示默認(rèn)的是默認(rèn)大小,也可以自定義顯示大小:width設(shè)置即可。
#采用微軟的vscode編輯器的run,沒(méi)反應(yīng),估計(jì)沒(méi)設(shè)置好,所以本文采用官方推薦的方法,就是終端
#輸入:streamlit run xxx.py運(yùn)行
5 st.video和st.audio
===============
5.1 代碼:
import streamlit as st
#播放動(dòng)畫(huà)
#video_file = open('/home/xgj/Desktop/Streamlit/11.mp4', 'rb') #mp4成功
#用st.video播放mp3可以,但是無(wú)效視頻框太大,占空間
#video_file = open('/home/xgj/Desktop/Streamlit/sn.mp3', 'rb') #不推薦
#video_bytes = video_file.read()
#st.video(video_bytes)
#播放mp3,推薦用audio法,包括ogg、wav格式
audio_file = open('/home/xgj/Desktop/Streamlit/sn.mp3', 'rb') #推薦
audio_bytes = audio_file.read()
st.audio(audio_bytes)
5.2 操作效果圖:
6 文本輸出:
========
6.1 代碼:
import streamlit as st
st.title('Helloworld,你好,世界')
st.header('Helloworld,你好,世界')
st.write('Helloworld,你好,世界')
#在終端輸入
#streamlit run 1.py
6.2 效果圖:
注意字體特點(diǎn)
7 信息提示框:
==========
7.1 代碼:
import streamlit as st
st.error('This is error!')
st.info('welcome to the streamlit!')
st.warning('This is warning!')
e = RuntimeError('This is an exception of type RuntimeError')
st.exception(e)
7.2 圖:
8 input輸入框:
===========
8.1 代碼:
import streamlit as st
#注意自動(dòng)依次布局
#文本輸入
url = st.text_input('Enter URL')
st.write('The Entered URL is', url)
#數(shù)字輸入
num = st.number_input('Enter NUMBER')
st.write('You enter number is',num)
#當(dāng)前日期選擇器
todaydate=st.date_input('Today is ')
st.write('Today is :',todaydate)
#當(dāng)前時(shí)間獲取
nowtime=st.time_input('Nowtime is:')
st.write('Nowtime is :',nowtime)
8.2 操作效果圖:
9 進(jìn)度條:
=======
9.1 學(xué)習(xí)st.sidebar=側(cè)邊欄,st.empty,st.success和python的格式化輸出知識(shí)復(fù)習(xí):比如顯示%=%%。
9.2 代碼:
import streamlit as st
import time
#正文的進(jìn)度條
my_bar = st.progress(0)
#進(jìn)度數(shù)字顯示
frame_text = st.empty()
#sidebar 側(cè)邊欄的進(jìn)度條
xx_bar=st.sidebar.progress(0)
#側(cè)邊欄進(jìn)度數(shù)字顯示
frame_textxx = st.sidebar.empty()
for i in range(100):
my_bar.progress(i + 1)
frame_text.text("Frame %i%%/100%%" % (i+ 1))
xx_bar.progress(i + 1)
frame_textxx.text("Frame %i%%/100%%" % (i+ 1))
time.sleep(0.1)
#清空顯示進(jìn)度條的百分比文本
#frame_text.empty()
#frame_textxx.empty()
#提示成功和完成信息框
st.sidebar.success("done")
st.success("done")
#在腳本所在的目錄下的終端輸入
#streamlit run 2-progres.py
9.3 操作和效果圖:
10 st.spinner,st.balloons和st.help用法:
==============================
10.1 代碼:
import streamlit as st
#導(dǎo)入時(shí)間
import time
with st.spinner('Please wait for it...'):
time.sleep(5)
st.success('Done!')
#彩蛋
st.balloons() #放慶祝氣球
#調(diào)出pandas.DataFrame的幫助信息
import pandas
st.help(pandas.DataFrame)
#運(yùn)行腳本
#streamlit run 9.py
10.2 操作和效果圖:
#注意st.help
11 st.slider滑動(dòng)條:
===============
11.1 代碼:
import streamlit as st
#滑動(dòng)條1
x = st.slider('x')
st.write(x, 'squared is', x * x)
#滑動(dòng)條2
age = st.slider('How old are you?', 0, 130, 25)
st.write("I'm ", age, 'years old')
#滑動(dòng)條3
values = st.slider(
'Select a range of values',
0.0, 100.0, (25.0, 75.0))
st.write('Values:', values)
#終端輸入
#streamlit run 5-slider.py
11.2 操作和效果圖:
#比官方還仔細(xì)