Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Dash First Application

Create a simple Dash application

In the previous chapter we introduced how to install Dash. To ensure Dash is installed correctly, we can create a simple Dash application to test it.

1. Create a Python File

In your project directory, create a new Python file, for example app.py.

2. Write the Code

in app.py In the file, enter the following code:

Example

Import Dash-related libraries

import

dash

from

dash

import

dcc

,

html  

dcc is the Dash core components library, and html is the HTML components library

Create a Dash application instance

app

=

dash.

Dash

(

name

)

Define the application's layout

app.

layout

=

html.

Div

(

children

=

[

   

Add a title

    html.

H1

(

children

=

'Hello, Dash!'

)

,

   

Add a paragraph of description text

    html.

Div

(

children

=

'''
        Dash: a web application framework for Python.
    '''

)

,

   

Add a chart

    dcc.

Graph

(

       

id

=

'example-graph'

,

 

The chart's ID, used for callbacks

        figure

=

{

           

'data'

:

[

 

The chart's data

               

{

'x'

:

[

1

,

2

,

3

]

,

'y'

:

[

4

,

1

,

2

]

,

'type'

:

'bar'

,

'name'

:

'Shanghai'

}

,

               

{

'x'

:

[

1

,

2

,

3

]

,

'y'

:

[

2

,

4

,

5

]

,

'type'

:

'bar'

,

'name'

:

'Beijing'

}

,

           

]

,

           

'layout'

:

{

 

The chart's layout

               

'title'

:

'Dash Data Visualization Example'

 

The chart's title

           

}

       

}

   

)

]

)

Run the application

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

 

Start the app; debug=True enables debug mode

3. Run the Application

Run the following command in the terminal to start the Dash application:



python app.py

If everything is normal, you will see output similar to the following:

Dash is running on http://127.0.0.1:8050/



 * Serving Flask app 'test'

 * Debug mode: on

Open your browser and visit http://127.0.0.1:8050/, and you will see a simple Dash application page displaying a title, text, and a bar chart, as shown below

Through the steps above, we have successfully run a simple Dash application.

Dash is a powerful and easy-to-use framework, especially suitable for data visualization and interactive web application development.