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 操作和效果图:#比官方还仔细
===这个初探很仔细,还可以数据可视化,下次再说===
自己整理超级仔细,并分享,喜欢的话,就点赞、关注、评论、转发和收藏。
宝宝微量元素正常值宝宝微量元素正常值锌元素umolL76。5170。含锌丰富的食物有海产品,如牡蛎干贝瑶柱等坚果类食物也含锌很高,例如核桃杏仁芝麻等。补锌的要有葡萄糖酸锌和甘草锌。铜元素umolL1
宝宝口腔护理幼儿口腔护理的方法1当孩子第一颗牙齿萌出后,家长就要用指套式牙刷,以轻柔简短的来回动作清洗幼儿牙齿。每天最少做两次,一次在早餐后,另一次在睡觉前。若能在幼儿每次吃东西后就清洁牙齿,
宝宝缺钙的症状13岁宝宝缺钙的症状有的孩子缺钙表现为出牙迟缓或者牙齿不齐整,且不够坚硬,咀嚼较硬食物困难。头可呈方形或马鞍状畸形,骨缝加宽,骨边缘发软,囟门较大,至18个月时仍未闭合。出牙晚且顺
宝宝湿疹跟痱子如何区别?湿疹是怎么引起的1直接病因引起宝宝湿疹病因很复杂,其中过敏是最主要的因素,所以有过敏体质家族史(如父亲母亲祖父祖母外祖父外祖母兄弟姐妹等家庭成员有过湿疹过敏性鼻炎过敏性皮炎过敏性结
冬季如何坐月子?冬季坐月子冬季坐月子尤为重要,产妇的穿戴梳洗哺乳等不注意,很有可能落下月子病。冬季坐月子室内环境的讲究冬季坐月子的重点是要保暖。室内温度以2025为宜,切忌忽高忽低。在没有暖气的南
夏季如何坐月子?夏季如何坐月子夏天天气炎热,一般坐月子主张不吹风,不受凉,那到了大热天怎么办呢?体质不好的妈妈这样做还有可能中暑呢,夏季该如何坐月子呢?日常穿衣1衣服材质应该选择棉制的,既保暖又吸
坐月子能吹空调吗?坐月子能吹空调吗在我国传统观念里,产妇坐月子总是被保护在一个密不透风的环境里,有的甚至被子长袖装长袜统统派上用场。炎炎夏日,坐月子可以开空调吗?夏季坐月子可适当吹空调,防止中暑。为
月子病有哪些?月子病也称为产后风,月子病可以分为四种情况,分别是风犯血脉型寒凝血脉型湿滞血脉型和寒热错杂型,由于此类疾病技术与风湿性疾病,同时也与产后血虚风寒侵入血脉有关,那么月子病也哪些呢?1
月子里能吃水果吗?月子里能吃水果吗有些人认为,水果是凉性食物,吃后会着凉落下月子病。但是有选择地食用水果,不仅不会使产妇着凉,反而有益于产后身体恢复。坐月子吃水果有哪些好处水果含丰富的维生素矿物质纤
夏季如何坐月子夏季如何坐月子夏天天气炎热,一般坐月子主张不吹风,不受凉,那到了大热天怎么办呢?体质不好的妈妈这样做还有可能中暑呢,夏季该如何坐月子呢?日常穿衣1衣服材质应该选择棉制的,既保暖又吸
夏季月子餐食谱推荐月子餐什么时候吃产妇刚生完孩子是很虚的,不能一味的进补,要分阶段,分个体情况,一边调理一边进补。月子餐要针对产妇的不同阶段来满足产妇的不同需求,分为八个阶段第一阶段(产后14天)排