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

Dash Dynamic Chart Updating

Dash Dynamic Chart Updating

In modern data visualization applications, dynamically updating charts is a very important feature. It allows users to view data changes in real time without refreshing the entire page.

In Dash, the core mechanism for dynamically updating charts is the callback function (Callback), which allows you to dynamically update chart content when the user interacts with the page (such as clicking buttons, selecting dropdown menus, etc.).

Basic Structure of a Callback Function

A typical callback function consists of the following parts:

  1. Input: defines the component and its property that trigger the callback.
  2. Output: defines the component and its property that need to be updated.
  3. Callback Function Body: computes and returns the output value based on the input value.

See Dash Callback Function details at:https://www.runoob.com/dash/dash-callback.html.

A Simple Dynamic Updating Chart

Through the callback function, you can dynamically update the chart's data and layout.

The following is a complete Dash application example where the user can select a dataset via a dropdown menu to dynamically display the corresponding line chart:

Example

from

dash

import

Dash

,

html

,

dcc

,

Input

,

Output

import

plotly.

express

as

px

import

pandas

as

pd

Create the Dash App

app

=

Dash

(

name

)

Define the sample dataset

datasets

=

{

   

'Dataset 1'

: pd.

DataFrame

(

{

       

'x'

:

[

1

,

2

,

3

,

4

,

5

]

,

       

'y'

:

[

10

,

15

,

13

,

17

,

21

]

   

}

)

,

   

'Dataset 2'

: pd.

DataFrame

(

{

       

'x'

:

[

1

,

2

,

3

,

4

,

5

]

,

       

'y'

:

[

5

,

10

,

8

,

12

,

15

]

   

}

)

,

   

'Dataset 3'

: pd.

DataFrame

(

{

       

'x'

:

[

1

,

2

,

3

,

4

,

5

]

,

       

'y'

:

[

20

,

18

,

22

,

19

,

25

]

   

}

)

}

Define the Layout

app.

layout

=

html.

Div

(

[

    html.

H1

(

"Dynamic Line Chart Example"

)

,

 

Title

    dcc.

Dropdown

(

       

id

=

'dataset-dropdown'

,

 

The dropdown menu's ID

        options

=

[

{

'label'

: name

,

'value'

: name

}

for

name

in

datasets.

keys

(

)

]

,

 

Dropdown menu options

        value

=

'Dataset 1'

 

The default selected dataset

   

)

,

    dcc.

Graph

(

id

=

'line-chart'

)

 

The Graph component used to display the line chart

]

)

Define the Callback Function

@

app.

callback

(

    Output

(

'line-chart'

,

'figure'

)

,

 

Output to the figure property of the Graph component with id 'line-chart'

    Input

(

'dataset-dropdown'

,

'value'

)

 

The input comes from the value property of the dropdown menu with id 'dataset-dropdown'

)

def

update_line_chart

(

selected_dataset

)

:

   

Get the selected dataset

    df

=

datasets

[

selected_dataset

]

   

Create a line chart using Plotly Express

    fig

=

px.

line

(

df

,

x

=

'x'

,

y

=

'y'

,

title

=

f

'{selected_dataset} Line Chart'

)

   

return

fig

Run the application

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

 

Start the app; debug=True enables debug mode

The display is shown as follows:

Code Analysis

1. Layout Section:

2. Callback Function:

-

Check whether the input is empty.

-

Convert the input to a float and calculate its square.

-

Return the formatted result.

4. Run the Application: