沧州热线
沧州热线 > 微商 > 正文

Streamlit 使用体验(1)

导读: 

'x',x # <-- Draw the string 'x' and then the value of xStreamlit 将识别变量或者合法的值在指定行,然后自动输出到你的app中通过st.write。


Streamlit.io


今天给大家介绍一下快速demo神器--streamlit。

我也是机缘巧合在刷头条的时候发现了一个快速demo神器,快速demo工具搭建神器。后来就一顿查询资料,发现这个是为了针对机器学习和数据科学团队能快速演示自己的模型。

其官网的原话:

“Streamlit is the first app framework specifically for Machine Learning and Data Science teams.
So you can stop spending time on frontend development and get back to what you do best.”(https://streamlit.io)

翻译过来:

“Streamlit是第一个专门针对机器学习和数据科学团队的应用开发框架,您能够不用花费时间至前端开发并留足时间把该做的事情做好。”

目前看下来,streamlit,它是快速demo话自定义机器学习工具的最快的方法,你可以认为它的目标是取代Flask在机器学习项目中的地位,可以帮助机器学习 工程师快速开发用户交互工具。然后并不需要HTTP requests, HTML, JavaScript, etc,只需要你的编辑器和一个浏览器。

看着这么好的工具,于是我就决定亲身体验一下,正好手头上有一个小项目需要不断地调试,但是单纯代码调试设计很多复杂,且一遍遍运行很枯燥,所以决定把这个项目服务话,从数据数据输入开始,到结果输出全部使用streamlit来呈现,包括必要的报错信息以及逻辑跳转。所以在此做个简短的streamlit 初体验总结。首先分为三部分来讲,第一部分为介绍以及部分用法,第二部分为优点,第三部分为缺点和改进。


1. 介绍

Streamlit框架本身就是使用Python写的,目前版本号为0.51。由于没有隐含状态,你可以直接使用python自己调用函数去重构。只要你会开发Python Scripts,你就会开发Streamlit APP.

简单来说streamlit其实就是把建站过程python化,通过预先写好的script脚本,用streamlit run 命令去拉起一个服务来渲染你的app。并且通过其后台服务,做到前端后端交互,从而实现app的demo。


streamlit示意图 1



5分钟安装

根据如下指南可以快速安装Streamlit。

1. 首先确认系统中安装了Python 2.7.0 或者Python 3.6.x 或者更高

2. 通过pip 安装 streamlit

$pip install streamlit

3. 运行hello wrold demo

$ streamlit hello

4. 接下来几秒钟简单的hello world demo将会从你的默认浏览器中弹出


Streamlit API清单

  • Magic commands
# Draw a title and some text to the app:df=pd.DataFrame({&#39;col1&#39;: [1,2,3]})df # &lt;-- Draw the dataframex=10&#39;x&#39;, x # &lt;-- Draw the string &#39;x&#39; and then the value of x

Streamlit 将识别变量或者合法的值在指定行,然后自动输出到你的app中通过st.write

它将自动识别docstrings 并且忽略它,因此他会忽略文件和函数的头部的字符串注释

还有如下其他组件:

  • Display text
  • Display data
  • Display charts
  • Display media
  • Display interactive widgets
  • Add widgets to sidebar
  • Display code
  • Display progress and status
  • Placeholders, help, and options
  • Mutate data
  • Optimize performance


未完待续。。。