使⽤streamlit构建快速美观的应⽤程序
Every data tells a story. It’s easier to understand them through visual reprentations rather than looking at thousands of records.Data Scientists often leverage graphs such as bar charts, line plots, area curves, etc to communicate this story to their stakeholders.Python and R have been preferred language of choice when working with data. There are multiple libraries such as Matplotlib, Seaborn, pyVis, etc through which one creates visualizations inside the Jupyter Notebooks or stand-alone apps using Bokeh, Flask, for example.
每个数据都讲述⼀个故事。 通过可视化表⽰⽐查看成千上万条记录更容易理解数据科学家经常利⽤条形图,折线图,⾯积曲线等图形将其故事传达给利益相关者,Python和R是⾸选语⾔处理数据时。 有多个库,例如Matplotlib,Seaborn,pyVis等,通过这些库,可以在Jupyter Notebook或使⽤Bokeh,Flask的独⽴应⽤程序内部创建可视化效果。
In this article, I will try to introduce you to Streamlit and get familiar with it. At the end of this article, you will be able to create an interactive app bad on your data. Let’s take a publicly available data t from . In the end, we will be able to create an app like this.
在本⽂中,我将尝试向您介绍Streamlit并熟悉它。 在本⽂的结尾,您将能够基于您的数据创建⼀个交互
式应⽤程序。 让我们从获取公开可⽤的数据集。 最后,我们将能够创建⼀个这样的应⽤程序。
Demo App (Gif by Author)
演⽰应⽤程序(作者提供的Gif)
The above gif is actually a screen record of the demo app. Streamlit provides you with an easy option to record your app with audio, in ca you need for a demo. This is very helpful if you plan to showca it to an audience.
韩语在线语音翻译
上⾯的gif实际上是演⽰应⽤程序的屏幕记录。 Streamlit为您提供了⼀个简单的选项,可以在需要演⽰的情况下⽤⾳频录制应⽤程序。 如果您打算向观众展⽰它,这将⾮常有帮助。
面试优缺点怎么回答
(Image by Author)
(图⽚由作者提供)
设置Streamlit (Setting up Streamlit)
Before we start with building apps, lets t up streamlit. I am working on a Ubuntu machine. Commands would be mostly the same for all OS.
在开始构建应⽤程序之前,让我们先进⾏精简设置。 我在Ubuntu机器上⼯作。 所有操作系统的命令⼏乎都是相同的。
python3 -m pip install streamlit
python3 -m pip安装streamlit
In ca you face errors such as this which is a quite common one:AttributeError: module ‘google.protobuf.descriptor’ has no attribute ‘_internal_create_key’
如果您遇到这样的错误,这是很常见的错误: AttributeError:模块'google.protobuf.descriptor'没有属性'_internal_create_key'
The fix is to upgrade protobuf小学四年级英语下册
解决⽅法是升级protobuf
python3 -m pip install — upgrade protobuf
英文短篇故事python3 -m pip install —升级protobuf
让我们来构建我们的应⽤程序 (Let’s build our App)小鹿斑比英文版
设计(Design)
Before we go ahead and start coding one should spend some time to create a rough layout. The way I go about is to draw it on a piece of paper — where charts should go, where would the drop-downs be? Will the charts be connected to the buttons?… so onIn ca you do not have a pen and paper, draw.io is an alternative to draw designs/flow charts/UML diagrams, etc. This is what I have had drawn on a piece of paper.
在我们继续进⾏编码之前,应该花⼀些时间来创建粗略的布局。 我的⽅法是将其绘制在⼀张纸上–图表应该放在哪⾥,下拉菜单应该在哪⾥? 图表可以连接到按钮吗?…等等如果没有笔和纸,draw.io是绘制设计/流程图/ UML图等的替代⽅法。这就是我在⼀块上绘制的内容纸
App Layout (Image by Author)relap
应⽤布局(作者提供的图像)
码 (Code)
Now that we have the design in mind, let’s start adding the Title followed by some description of the analysis. You may wish to add an image if that increas the appealing nature of the application.english
现在我们已经有了设计思想,让我们开始添加标题,然后对分析进⾏⼀些描述。 如果可能增加了应⽤程序的吸引⼒,您可能希望添加图像。
The code is very simple and intuitive. To load an image, you just have to read the image and call streamlit.image method. One could also write markdowns which are very similar to that of Github. To add a description we just have to call the write method with python objects as parameters. It could be a string or pandas data frame.
该代码⾮常简单直观。 要加载图像,只需读取图像并调⽤streamlit.image⽅法。 还可以编写与Github⾮常相似的降价促销。 要添加描述,我们只需要使⽤python对象作为参数调⽤write⽅法。 它可以是字符串或熊猫数据框。生活大爆炸第二季剧情
In the above code, we are reading the data t and showing a glance of the values. In terms of code,
it’s very straight forward. You just call that sampled pandas data frame. This is the magical part of streamlit, also called as Magic Commands. When you write a variable name, streamlit implicitly writes that out to your application just that way you would using the write method. We will be utilizing some of the magic commands in this app to create charts and interactive options.
在上⾯的代码中,我们正在读取数据集并显⽰这些值。 就代码⽽⾔,这⾮常简单。 您只需调⽤该采样的熊猫数据框即可。 这是streamlit的神奇部分,也称为Magic Commands 。 当您写⼀个变量名时,streamlit隐式地将其写到您的应⽤程序中,就像您使⽤write⽅法⼀样。我们将利⽤此应⽤程序中的⼀些魔术命令来创建图表和交互式选项。
To start the app — streamlit run app.py. You will get an app like this:
要启动该应⽤程序-streamlit运⾏app.py。 您将获得⼀个这样的应⽤程序:
Data Overview (Image by Author)
数据概述(作者提供的图像)
Having looked at the data, we would like to create some bi-variate visualizations. The should be interactive as well so that one can lect different values and analyze the trend. For example, I would like to show sales across years for various platforms. And to make it interactive, let’s add a drop-down where the end-ur can lect the values.
查看数据后,我们想创建⼀些双变量可视化。 这些也应该是交互式的,以便可以选择不同的值并分析趋势。 例如,我想显⽰各种平台多年来的销售额。 为了使其具有交互性,让我们添加⼀个下拉菜单,最终⽤户可以在其中选择值。
In the above code, we are creating one drop down and 2 charts (line graph and a stacked bar graph). To create a drop-down in streamlit, it's very simple — just call the Magic command st.lectbox
在上⾯的代码中,我们将创建⼀个下拉列表和2个图表(折线图和堆积的条形图)。 要创建流式下拉菜单,这⾮常简单-只需调⽤Magic命令st.lectbox
platform_name = st.lectbox(‘Select a Platform’,options= df.Platform.unique())
platform_name = st.lectbox('选择平台',options = df.Platform.unique())
Now how do we make sure that the value lected in the drop-down option is reflected on the graphs? Well, that’s very easy too. Whatever lection you make gets assigned in the platform_name variable. In this example, we are using altair
library to create charts. Refer to line 6. To embed the graph in the app, just call
现在我们如何确保在下拉选项中选择的值反映在图形上? 好吧,这也很容易。 您所做的任何选择都会在platform_name变量中分配。 在此⽰例中,我们使⽤altair库创建图表。 请参阅第6⾏。要将图形嵌⼊到应⽤程序中,只需调⽤
st.altair_chart(basic_chart)st.altair_chart(stacked_bar)
st.altair_chart(basic_chart)st.altair_chart(stacked_bar)
ジレンマ
And magic! below is what you get in your app
和魔术! 以下是您在应⽤程序中获得的
(Image by Author)
(图⽚由作者提供)
I found an interesting functionality in the API. If you wish to share code snippets in the app, it just a single line of code in streamlit. Write your code after you ho
我在API中发现了⼀个有趣的功能。 如果您希望在应⽤程序中共享代码段,则只需⼀⾏代码即可。 调⽤st.echo后编写代码
The above code is ud in creating the data frame for the above stacked bar graph.
上⾯的代码⽤于为上⾯的堆叠条形图创建数据框。斯巴达300勇士前传