Streamlit是python的一个机器学习、数据科学、应用开发框架

1 说明:

===初探===

1.1 Streamlit

1.1.1 是一个开源Python库,可轻松构建用于机器学习的漂亮应用程序。

1.1.2 是一款专为机器学习工程师创建的免费、开源 app 构建框架。

1.1.3 是一个简单而强大的应用程序模型,可让您快速构建丰富的UI。

1.1.4 是第一个专门针对机器学习和数据科学团队的应用开发框架。

1.1.5 是开发自定义机器学习工具的最快的方法,它的目标是取代Flask在机器学习项目中的地位,可以帮助机器学习工程师快速开发用户交互工具。

1.2 环境:

华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。

1.3 官网:

https://www.streamlit.io/
https://github.com/streamlit/streamlit
https://docs.streamlit.io/en/latest/

2 安装过程:

=====

2.1 安装:

pip install streamlit
#本机安装
sudo pip3.8 install streamlit  #慢
#推荐安装方法
sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple streamlit #超快

2.2 第一个报错:

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的版本均太高了,暂时忽略看看行不行,因为我提前安装bokeh和pandas

2.3 第二个提醒:

  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.

  #解决办法:建立软连接,本机如下
sudo ln -s /usr/local/python3.8/bin/base58 /usr/bin/base58

2.4 第三个提醒:

  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.

#解决办法:建立软连接,本机如下
sudo ln -s /usr/local/python3.8/bin/streamlit /usr/bin/streamlit

2.5 验证:查看版本,成功。

xgj@xgj-PC:~$ streamlit --version

Streamlit, version 0.61.0

3 查看demo

=========

3.1 第一次,打开终端输入:

streamlit hello

3.2 需要输入个人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

#回车,出现下面,并自动打开浏览器
  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 第二次进入,如下操作和示意图:

4 st.image:

========

4.1 方法一:官方推荐法,缺点gif图片显示静态的

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')  #静态的图片

#caption='显示图片',图片信息;width=300,指定图片宽度,自动缩放;默认是图片本身的大小
st.image(image, caption='显示图片',width=300)

4.2 方法二:

import streamlit as st
#显示图片法二,推荐这种方法
#pic_file = open('/home/xgj/Desktop/Streamlit/12.gif', 'rb')  #gif可以动态显示
pic_file = open('/home/xgj/Desktop/Streamlit/21.jpg', 'rb') 

pic_bytes = pic_file.read()
st.image(pic_bytes)  

4.3 效果图:

#注意代码块中采用#注释,如果采用python的'''和'''大片状注释法会在网页上显示代码块,不建议。

#如果修改代码块,保存后,因为终端启动服务器,所以只需要网页刷新即可看到效果
#图片显示默认的是默认大小,也可以自定义显示大小:width设置即可。
#采用微软的vscode编辑器的run,没反应,估计没设置好,所以本文采用官方推荐的方法,就是终端
#输入:streamlit run xxx.py运行

5 st.video和st.audio

===============

5.1 代码:

import streamlit as st

#播放动画
#video_file = open('/home/xgj/Desktop/Streamlit/11.mp4', 'rb') #mp4成功
#用st.video播放mp3可以,但是无效视频框太大,占空间
#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 效果图:

注意字体特点

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
#注意自动依次布局
#文本输入
url = st.text_input('Enter URL')
st.write('The Entered URL is', url)
#数字输入
num = st.number_input('Enter NUMBER')
st.write('You enter number is',num)
#当前日期选择器
todaydate=st.date_input('Today is ')
st.write('Today is :',todaydate)
#当前时间获取
nowtime=st.time_input('Nowtime is:')
st.write('Nowtime is :',nowtime)

8.2 操作效果图:

9 进度条:

=======

9.1 学习st.sidebar=侧边栏,st.empty,st.success和python的格式化输出知识复习:比如显示%=%%。

9.2 代码:

import streamlit as st
import time
#正文的进度条
my_bar = st.progress(0)
#进度数字显示
frame_text = st.empty()
#sidebar 侧边栏的进度条
xx_bar=st.sidebar.progress(0)
#侧边栏进度数字显示
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)
#清空显示进度条的百分比文本
#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
#导入时间
import time
with st.spinner('Please wait for it...'):
    time.sleep(5)
st.success('Done!')
#彩蛋
st.balloons()  #放庆祝气球
#调出pandas.DataFrame的帮助信息
import pandas
st.help(pandas.DataFrame)
#运行脚本
#streamlit run 9.py

10.2 操作和效果图:

#注意st.help

11 st.slider滑动条:

===============

11.1 代码:

import streamlit as st
#滑动条1
x = st.slider('x')
st.write(x, 'squared is', x * x)
#滑动条2
age = st.slider('How old are you?', 0, 130, 25)
st.write("I'm ", age, 'years old')
#滑动条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 操作和效果图:

#比官方还仔细

===这个初探很仔细,还可以数据可视化,下次再说===

自己整理超级仔细,并分享,喜欢的话,就点赞、关注、评论、转发和收藏。

举报
评论 0