Dash的快速入门将使您在5分钟内进入“ Hello World”

by Anuj Pahade

由Anuj Pahade

Dash的快速入门将使您在5分钟内进入“ Hello World” (This quick intro to Dash will get you to “Hello World” in under 5 minutes)

Dash is an open source library for creating reactive apps in Python. You can create amazing dashboards in your browser using Dash.

Dash是一个开源库,用于在Python中创建React式应用程序。 您可以使用Dash在浏览器中创建惊人的仪表板。

The Iris data set can be called the ‘hello world’ of data sets. In this article, we’ll learn how to build a simple Dash app in which we’ll use the Iris data set. This data set is clean, which is good for us so that we can focus on dashing instead of cleaning the data.

Iris数据集可以称为数据集的“ hello world”。 在本文中,我们将学习如何构建一个简单的Dash应用程序,并在其中使用Iris数据集。 该数据集很干净,这对我们有好处,因此我们可以专注于破折号而不是清理数据。

破折号设定 (Dash setup)

To build cool apps, you need hot libraries.

要构建出色的应用程序,您需要热库。

If you have not already installed Dash, then run these commands in your terminal :

如果尚未安装Dash,请在终端中运行以下命令 :

pip install dash==0.21.1 # The core dash backendpip install dash-renderer==0.12.1 # The dash front-endpip install dash-html-components==0.10.1 # HTML componentspip install dash-core-components==0.22.1 # Supercharged componentspip install plotly --upgrade

pip install dash==0.21.1 # The core dash backend pip install dash-renderer==0.12.1 # The dash front-end pip install dash-html-components==0.10.1 # HTML components pip install dash-core-components==0.22.1 # Supercharged components pip install plotly --upgrade

Run your app as :

以以下方式运行您的应用程序:

python helloiris.py

Be clear with your Python versions.

明确说明您的Python版本。

应用布局 (App layout)

We can build the layout with the dash_html_components library and the dash_core_components library. I have imported them as shown above. The dash_html_components is for all HTML tags, whereas the latter one is for interactive components built with React.js. Having said that, let’s write something in our browser using Dash:

我们可以使用dash_html_components库和dash_core_components库来构建布局。 我已经如上所示导入了它们。 dash_html_components适用于所有HTML标签,而后者适用于使用React.js构建的交互式组件。 话虽如此,让我们使用Dash在浏览器中编写一些内容:

app.layout = html.Div(children=[    html.H1(children='Iris visualization'),    html.Div(    '''        Built with Dash: A web application framework for Python.    ''')])

Yes! That’s how easy it is. The equivalent HTML code would look like this:

是! 就是这么简单。 等效HTML代码如下所示:

<div> <h1> Iris visualization &lt;/h1> <div> Built with Dash: A web application framework for Python. </div></div>

Notice the children attribute in the first Div . It is used to define the list of elements enclosed in that tag. This is a positional argument (always comes first) and can be skipped as you can see in the next H1 and Div shown above.

注意第一个Divchildren属性。 它用于定义该标签中包含的元素list 。 这是一个位置自变量(始终是第一个),可以跳过,如您在上面显示的下一个H1Div看到的。

Can we style it? I hear you ask. Well, of course! Dash allows you to write style dictionaries as you would write in a <style> tag in HTML. It also lets you write inline CSS and link external CSS files. Here is how we can do it.

我们可以样式吗? 我听到你问。 嗯,当然! Dash允许您编写样式字典,就像在HTML中的<sty le>标记中编写一样。 它还允许您编写内联CSS并链接外部CSS文件。 这是我们可以做到的。

风格字典 (Style dictionaries)

Let’s create a dictionary called colors.

让我们创建一个称为颜色的字典。

colors = {         'background': '#0000FF',         'color': '#FFA500'}

It can be attached with an element using the style attribute as shown.

如图所示,可以使用style属性将其附加到元素上。

app.layout = html.Div(style=colors,children=[    html.H1(children='Iris visualization'),    html.Div(    '''        Built with Dash: A web application framework for Python.    ''')])

内联CSS (Inline CSS)

In Dash, the keys of dictionaries are camelCased . So instead of text-align we use textAlign . Also the class attribute of HTML tags is className as you might know if you use React.

在Dash中,字典的键是camelCased 。 因此,我们使用textAlign代替text-align 。 如果使用React,HTML标记的class属性也是className

app.layout = html.Div(style=colors,children=[    html.H1(children='Iris visualization',style = {'textAlign':'center'}),
html.Div(style={'textAlign':'center'},children='''        Built with Dash: A web application framework for Python.    ''')])

外部CSS (External CSS)

We can create a list of URLs or paths to CSS files we want to include in our Dash app, and then use app.css.append_css to include them.

我们可以创建URL列表或指向要包含在Dash应用程序中CSS文件的路径,然后使用app.css.append_css包含它们。

external_css = ["https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",              "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" ]
for css in external_css:    app.css.append_css({"external_url": css})

We can include JavaScript in the exact same way by using app.scripts.append_script

我们可以使用app.scripts.append_script以完全相同的方式包含JavaScript

I hope you’re with me till now! This is how our helloiris.py file looks:

希望您一直与我在一起! 这是我们的helloiris.py文件的外观:

import dashimport dash_core_components as dccimport dash_html_components as html
app = dash.Dash()
#External CSSexternal_css = ["https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",                "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css", ]
for css in external_css:    app.css.append_css({"external_url": css})
#External JavaScriptexternal_js = ["http://code.jquery.com/jquery-3.3.1.min.js",               "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"]
for js in external_js:    app.scripts.append_script({"external_url": js})
#Internal CSScolors = {         'background': '#0000FF',         'color': '#FFA500'}
#Our app's Layoutapp.layout = html.Div(style=colors,children=[    html.H1(children='Iris visualization',style={'textAlign':'center'}),
html.Div(style={'textAlign':'center'},children='''     Built with Dash: A web application framework for Python.    ''')])
if __name__ == '__main__':    app.run_server(debug=True)

让我们获取一些数据 (Let’s get some data)

Assuming you’re familiar with pandas, we’ll use this Python library to import the iris.csv file in our app. If you don’t know what this dataset is about, then I recommend that you read and download it from here.

假设您熟悉熊猫,我们将使用此Python库在我们的应用程序中导入iris.csv文件。 如果您不知道此数据集的含义,那么建议您从此处阅读并下载。

import pandas as pd
header_names =[ 'sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
df = pd.read_csv('path/to/Iris.csv',names=header_names)

Now that our data is loaded into the df dataframe, it’s time for visualisation.

现在,我们的数据已加载到df数据帧中,是时候进行可视化了。

数据可视化 (Data visualization)

Remember the interactive components I told you about? The dash_core_components library? Well that’s what we are going to use here.

还记得我告诉过你的互动组件吗? dash_core_components库? 嗯,这就是我们在这里要使用的。

import plotly.graph_objs as go

Let’s add a new component to our app.layout . This time it’s not an HTML tag but an interactive graph. Dash uses Plotly to plot graphs.

让我们向app.layout添加一个新组件。 这次,它不是HTML标记,而是交互式图形。 Dash使用Plotly绘制图形。

dcc.Graph(        id='Iris Viz',        figure={            'data': [                go.Scatter(                    x=df[df['class'] == i]['petal_length'],                    y=df[df['class'] == i]['sepal_length'],                    mode='markers',                    opacity=0.7,                    marker={                        'size': 15,                        'line': {'width': 0.5, 'color': 'white'}                    },                    name=i                ) for i in df['class'].unique()            ],            'layout': go.Layout(                xaxis={'title': 'Petal Length'},                yaxis={'title': 'Sepal Length'},                margin={'l': 200, 'b': 40, 't': 100, 'r': 200},                legend={'x': 0, 'y': 1},                hovermode='closest'            )        }    )

Whoa! A whole paragraph in Python! Don’t worry. It’s not difficult to understand. Let’s go over it piece by piece:

哇! Python的整个段落! 不用担心 不难理解。 让我们一步一步地研究它:

The dcc.Graph has an id argument which is used to reference the graph in the future for deleting or overlaying or any other purposes.

dcc.Graph有一个id参数,该参数将来用于引用图形以进行删除或覆盖或任何其他目的。

The figure argument is the same as the one used in plotly.py. It takes in two arguments, data and layout.

figure参数与plotly.py中使用的参数相同。 它接受两个参数, datalayout

In data we can specify which columns of the dataframe to plot on which axis. We can also specify the mode, for example: marker and then the properties of marker such as width and line (meaning border).

data我们可以指定要在哪个轴上绘制数据框的哪些列。 我们还可以指定模式,例如: 标记 ,然后指定标记的属性,例如宽度线条 (意味着边框)。

In layout we define the axes labels, legend position, graph margins (left, top, bottom, right) and much more.

layout我们定义了轴标签,图例位置,图形边距(左,上,下,右)等等。

This isn’t it. These graphs are interactive and can be manipulated by user inputs.

不是吗 这些图是交互式的 ,可以通过用户输入进行操作。

Ok, so let’s go build some cool DashApps this summer!

好的,让我们今年夏天开始构建一些很棒的DashApps!

Stay tuned for my next posts. This is not my first time coding or making an app, but it’s my first article on Medium! I think claps and recommendations will motivate me :)

请继续关注我的下一篇文章。 这不是我第一次编写或开发应用程序,但这是我在Medium上的第一篇文章! 我认为拍手和建议会激励我:)

Don’t hesitate to contact me via email: anujp5678[at]gmail[dot]com

不要犹豫,通过电子邮件与我联系:anujp5678 [at] gmail [dot] com

Or connect with me on LinkedIn https://www.linkedin.com/in/anuj-pahade/

或通过LinkedIn https://www.linkedin.com/in/anuj-pahade/与我联系

Keep dashing and happy coding!

继续使用破破烂烂的编码!

翻译自: https://www.freecodecamp.org/news/this-quick-intro-to-dash-will-get-you-to-hello-world-in-under-5-minutes-86f8ae22ca27/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394226.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JSON/xml

JSON是什么&#xff1a; JSON(JavaScriptObject Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于ECMAScript(欧洲计算机协会制定的js规范)的一个子集&#xff0c;采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据…

unity开宝箱动画_[技术博客]Unity3d 动画控制

在制作游戏时&#xff0c;导入的箱子模型本身自带动画。然而&#xff0c;它的动画是一个从打开到关闭的完整过程&#xff0c;并且没有给出控制打开关闭的方法。最直接的想法是对该动画进行拆分&#xff0c;再封装成不同的动画状态&#xff0c;但是不巧的是&#xff0c;这个动画…

php上传大文件时,服务器端php.ini文件中需要额外修改的选项

几个修改点&#xff1a; 1、upload_max_filesize 上传的最大文件 2、post_max_size 上传的最大文件 3、max_execution_time 修改为0表示无超时&#xff0c;一直等待 4、max_input_time 参考网址&#xff1a; 在php.ini中把max_input_time 和 max_execution_time设置得特别长…

《中国人工智能学会通讯》——11.21 结束语

11.21 结束语 本文针对交通流的网络性、异质性和动态性特点&#xff0c;结合当前多任务学习方法的不足提出了相应的解决方案。然而&#xff0c;在实际的应用场景中还存在更多的挑战&#xff0c;需要进一步深入的研究方向包括&#xff1a;① 高维任务的共同学习方法。在高维数据…

如何把一个软件嵌入另一个软件_自动化正在成为一个“软件”行业

摘要在智能制造时代&#xff0c;自动化行业正在成为一个软件行业&#xff0c;它正在改变着整个产业的未来&#xff0c;也将为制造业带来更为广阔的空间。自动化正在成为一个“软件”行业&#xff0c;在智能时代&#xff0c;软件正在成为自动化行业竞争的关键。自动化已然成为软…

leetcode1020. 飞地的数量(dfs)

给出一个二维数组 A&#xff0c;每个单元格为 0&#xff08;代表海&#xff09;或 1&#xff08;代表陆地&#xff09;。 移动是指在陆地上从一个地方走到另一个地方&#xff08;朝四个方向之一&#xff09;或离开网格的边界。 返回网格中无法在任意次数的移动中离开网格边界…

未来编程语言的走向_在编程方面我从失败走向成功的过程以及让我成功的原因

未来编程语言的走向In the past 10 years, I’ve had three separate experiences trying to learn programming. I’ve wondered why I’ve had such different results. What had caused me to both fail and succeed?在过去的10年中&#xff0c;我有3种不同的尝试学习编程的…

《中国人工智能学会通讯》——5.16 结 论

5.16 结 论 在过去的 30 年中&#xff0c;移动操作机器人在机器人实验室受到了广泛的关注并获得了比较充分的研究。未来随着工业领域的自动化需求&#xff0c;移动操作机器人将会深入到生产的各个环节。目前&#xff0c;几乎所有的移动操作机器人都没有在实际环境中获得广泛及充…

【转载 | 笔记】IIS无法删除应该程序池 因为它包含X个应用程序

IIS无法删除应该程序池 因为它包含X个应用程序 今天代码主分支在vs2015创建了虚拟目录http://localhost/webapp指向的物理路径是E:\webapp 之后新开了一个分支把代码放在了D:\webapp之后又在vs2015中创建了虚拟目录 http://localhost/webapp/home 这下就杯具了。在主分支调试的…

python作中国地图背景气泡图_exce表格中怎么制作中国地图背景数据气泡图

exce表格中怎么制作中国地图背景数据气泡图exce表格中怎么制作中国地图背景数据气泡图?excel表格中想要在中国地图上显示气泡来看看地区分布情况&#xff0c;该怎么设置中国地图气泡图表呢?下面我们就来看看详细的教程&#xff0c;需要的朋友可以参考下1、如图1所示&#xff…

leetcode979. 在二叉树中分配硬币(dfs)

给定一个有 N 个结点的二叉树的根结点 root&#xff0c;树中的每个结点上都对应有 node.val 枚硬币&#xff0c;并且总共有 N 枚硬币。 在一次移动中&#xff0c;我们可以选择两个相邻的结点&#xff0c;然后将一枚硬币从其中一个结点移动到另一个结点。(移动可以是从父结点到…

python怎么显示求余的除数_Python算术运算符及用法详解

算术运算符也即数学运算符&#xff0c;用来对数字进行数学运算&#xff0c;比如加减乘除。下表列出了 Python 支持所有基本算术运算符。表 1 Python 常用算术运算符运算符说明实例结果加12.45 1527.45-减4.56 - 0.264.3*乘5 * 3.618.0/除法(和数学中的规则一样)7 / 23.5//整除…

任务完成:我从CNC2018 GetAJob挑战中学到的东西

什么是CNC2018&#xff1f; (What is CNC2018?) CNC2018 stands for the CodeNewbie Challenge of 2018 put on by CodeNewbie. If you haven’t heard of CodeNewbie, it’s a community and podcast run by Saron Yitbarek. They also host live Twitter Chats on Sundays a…

HTML td 标签的 colspan 属性

表格单元横跨两列的表格&#xff1a; <table border"1"><tr><th>Month</th><th>Savings</th></tr><tr><td colspan"2">January</td></tr><tr><td colspan"2">Fe…

Kotlin的Lambda表达式以及它们怎样简化Android开发(KAD 07)

作者&#xff1a;Antonio Leiva 时间&#xff1a;Jan 5, 2017 原文链接&#xff1a;https://antonioleiva.com/lambdas-kotlin/ 由于Lambda表达式允许更简单的方式建模式函数&#xff0c;所以它是Kotlin和任何其他现代开发语言的最强工具之一。 在Java6中&#xff0c;我们仅能下…

Pyhon进阶9---类的继承

类的继承 基本概念 定义 格式如下 继承中的访问控制 class Animal:__CNOUT 0HEIGHT 0def __init__(self,age,weight,height):self.__CNOUT self.__CNOUT 1self.age ageself.__weight weightself.HEIGHT heightdef eat(self):print({} eat.format(self.__class__.__name__…

python怎么备份列表_python实例:backup 备份

python实例&#xff1a;backup 备份本文来源于《python简明教程》中的实例1. 提出问题&#xff1a; 我想要一个可以为我的所有重要文件创建备份的程序。2. 分析明确问题&#xff1a;我们如何确定该备份哪些文件&#xff1f;备份保存在哪里&#xff1f;我们怎么样存储备份&#…

leetcode1466. 重新规划路线(dfs)

n 座城市&#xff0c;从 0 到 n-1 编号&#xff0c;其间共有 n-1 条路线。因此&#xff0c;要想在两座不同城市之间旅行只有唯一一条路线可供选择&#xff08;路线网形成一颗树&#xff09;。去年&#xff0c;交通运输部决定重新规划路线&#xff0c;以改变交通拥堵的状况。 路…

mysql数学函数名_Mysql数学函数

所有的数学函数在发生错误的情况下&#xff0c;均返回 NULL。-一元减。改变参数的符号&#xff1a;mysql> SELECT - 2;-> -2注意&#xff0c;如果这个操作符被用于一个 BIGINT&#xff0c;返回值也是一个 BIGINT&#xff01;这就意味着&#xff0c;应该避免在一个可能有值…

angular 渐进_如何创建具有Angular和无头CMS的渐进式Web应用程序

angular 渐进by Ondrej Chrastina通过Ondrej Chrastina 如何创建具有Angular和无头CMS的渐进式Web应用程序 (How to create a progressive web app featuring Angular and headless CMS) Have you ever wondered how a headless Content Management System fits in with Progr…