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.pswp.cn/news/394226.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394226.shtml
英文地址,請注明出處:http://en.pswp.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…