Dash Common Interactive Components
Dash is based on Python and allows developers to build interactive web applications using pure Python code.
One of Dash's core components is dash.dcc(Dash Core Components), which provides rich UI components such as sliders, dropdown menus, graphs, and more. These components help developers quickly build powerful web applications.
dash.dcc Components include, but are not limited to:
- Graph: used to draw various charts, such as line charts, bar charts, scatter charts, and so on.
- Dropdown: a dropdown menu that allows the user to select from multiple options.
- Slider: a slider that allows the user to select a numeric range by dragging the slider.
- Input: an input box that allows the user to enter text or numbers.
- Textarea: a multi-line text input box, suitable for entering longer text content.
- Checklist: a checkbox list that allows the user to select multiple options.
- RadioItems: a radio button list that allows the user to select one option from multiple options.
- DatePickerSingle and DatePickerRange: a date picker that allows the user to select a single date or a date range.
- Upload: a file upload component that allows the user to upload files.
These components are the foundation for building interactive web applications. Developers can combine them to create complex user interfaces.
In the latest version of Dash, the following way to import dcc is recommended:
from dash import dcc
Common Interactive Components
| Component | Description | Example Code |
|---|---|---|
dcc.Input |
Create a text input box. | dcc.Input(id='input', type='text', placeholder='请输入内容...') |
dcc.Dropdown |
Create a dropdown menu. | dcc.Dropdown(id='dropdown', options=[{'label': '选项1', 'value': '1'}], value='1') |
dcc.Slider |
Create a slider. | dcc.Slider(id='slider', min=0, max=10, step=1, value=5) |
dcc.Graph |
Create an interactive chart (based on Plotly.js). | dcc.Graph(id='graph', figure={'data': [{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar'}]}) |
dcc.Textarea |
Create a multi-line text input box. | dcc.Textarea(id='textarea', value='请输入多行文本...') |
dcc.Checklist |
Create a checkbox list. | dcc.Checklist(id='checklist', options=[{'label': '选项1', 'value': '1'}], value=['1']) |
dcc.RadioItems |
Create a radio button group. | dcc.RadioItems(id='radio', options=[{'label': '选项1', 'value': '1'}], value='1') |
dcc.DatePickerSingle |
Create a date picker (single). | dcc.DatePickerSingle(id='date-picker', date='2023-10-01') |
dcc.DatePickerRange |
Create a date range picker. | dcc.DatePickerRange(id='date-range', start_date='2023-10-01', end_date='2023-10-07') |
dcc.Markdown |
Render Markdown text. | dcc.Markdown('''# 标题\n- 列表项1''') |
dcc.Store |
Store data on the client side for sharing across callbacks. | dcc.Store(id='store', data={'key': 'value'}) |
dcc.Upload |
Create a file upload component. | dcc.Upload(id='upload', children=html.Div('拖放或点击上传文件')) |
dcc.Tabs |
Create a tab component. | dcc.Tabs(id='tabs', children=[dcc.Tab(label='标签1', value='1')]) |
dcc.Tab |
Create a single tab (must be used with dcc.Tabs ). |
dcc.Tab(label='标签1', value='1') |
dcc.Interval |
A component that triggers callbacks on a timer. | dcc.Interval(id='interval', interval=1000) |
dcc.Location |
A component used to manage URLs. | dcc.Location(id='url', pathname='/') |
dcc.Link |
Create a hyperlink for page navigation (must be used with dcc.Location ). |
dcc.Link('跳转到首页', href='/') |
dcc.ConfirmDialog |
Create a confirmation dialog. | dcc.ConfirmDialog(id='confirm', message='确定要执行此操作吗?') |
dcc.ConfirmDialogProvider |
Provide a confirmation dialog (must be used with button and other components). | dcc.ConfirmDialogProvider(html.Button('删除'), id='confirm-provider') |
dcc.Loading |
Create a loading animation component. | dcc.Loading(id='loading', children=[html.Div('加载中...')]) |
dcc.Download |
A component used to trigger file downloads. | dcc.Download(id='download') |
Application Example
Create a simple Dash application
The following is a simple Dash application example showing how to use the dash.dcc component:
Example
import
dash
from
dash
import
dcc
,
html
from
dash.
dependencies
import
Input
,
Output
Create a Dash application
app
=
dash.
Dash
(
name
)
Define the application's layout
app.
layout
=
html.
Div
(
[
dcc.
Input
(
id
=
'input-text'
,
type
=
'text'
,
value
=
''
)
,
html.
Div
(
id
=
'output-text'
)
]
)
Define the Callback Function
@
app.
callback
(
Output
(
'output-text'
,
'children'
)
,
[
Input
(
'input-text'
,
'value'
)
]
)
def
update_output
(
value
)
:
return
f
'You entered: {value}'
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
In the example above, we created a simple Dash application containing an input box and an area that displays the entered content.
When the user enters content in the input box, the application updates the content of the display area in real time.

Use dcc.Graph Drawing Charts
dcc.Graph Is dash.dcc is one of the most commonly used components, used to draw various charts.
The following is an example of using dcc.Graph Example of drawing a line chart:
Example
import
dash
from
dash
import
dcc
,
html
import
plotly.
express
as
px
import
pandas
as
pd
Create a Dash application
app
=
dash.
Dash
(
name
)
Create sample data
df
=
pd.
DataFrame
(
{
'x'
:
[
1
,
2
,
3
,
4
,
5
]
,
'y'
:
[
10
,
11
,
12
,
13
,
14
]
}
)
Create a line chart using Plotly Express
fig
=
px.
line
(
df
,
x
=
'x'
,
y
=
'y'
,
title
=
'Sample Line Chart'
)
Define the application's layout
app.
layout
=
html.
Div
(
[
dcc.
Graph
(
figure
=
fig
)
]
)
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
In the example above, we used plotly.express to create a simple line chart, and used the dcc.Graph component to display it in the application.

Use dcc.Dropdown Create a Dropdown Menu
dcc.Dropdown component allows the user to select from multiple options.
The following is an example of using dcc.Dropdown example:
Example
import
dash
from
dash
import
dcc
,
html
from
dash.
dependencies
import
Input
,
Output
Create a Dash application
app
=
dash.
Dash
(
name
)
Define the application's layout
app.
layout
=
html.
Div
(
[
dcc.
Dropdown
(
id
=
'dropdown'
,
options
=
[
{
'label'
:
'Option 1'
,
'value'
:
'1'
}
,
{
'label'
:
'Option 2'
,
'value'
:
'2'
}
,
{
'label'
:
'Option 3'
,
'value'
:
'3'
}
]
,
value
=
'1'
The default selected value
)
,
html.
Div
(
id
=
'dropdown-output'
)
]
)
Define the Callback Function
@
app.
callback
(
Output
(
'dropdown-output'
,
'children'
)
,
[
Input
(
'dropdown'
,
'value'
)
]
)
def
update_output
(
value
)
:
return
f
'You selected: {value}'
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
In the example above, we created a dropdown menu. When the user selects an option, the application displays the selected content.

dash.dcc Common Properties and Callbacks of Components
dash.dcc Components have very rich properties; each component has its specific properties.
The following are the properties of some common components:
-
dcc.Input: id: the unique identifier of the component.type: the input type, such astext,number, etc.value: the current value of the input box.
-
dcc.Dropdown: id: the unique identifier of the component.options: the options list of the dropdown menu, where each option is a dictionary containinglabelandvalue.value: the currently selected value.
-
dcc.Graph: id: the unique identifier of the component.figure: the chart object to display, usually created byplotly.expressorplotly.graph_objects.
-
dcc.Slider: id: the unique identifier of the component.min: the minimum value of the slider.max: the maximum value of the slider.value: the current value of the slider.
Callback Function
In Dash, callbacks are used to handle user interaction. Callbacks are defined through the @app.callback decorator, specifying the input and output components.
The following is a simple callback function example:
Example
@
app.
callback
(
Output
(
'output-component'
,
'children'
)
,
[
Input
(
'input-component'
,
'value'
)
]
)
def
update_output
(
value
)
:
return
f
'You entered: {value}'
In the example above, the callback function update_output will be called when the input-component value changes, and passes the new value to the output-component.
Practical application cases
dash.dcc component has wide applications in real projects. The following is a practical application case showing how to use the dash.dcc component to build an interactive data visualization application.
Interactive Data Visualization Application
Suppose we have a dataset containing temperature and humidity data for different cities. We can use the dash.dcc component to build an interactive application that allows the user to select a city and view the corresponding temperature and humidity charts.
Example
import
dash
from
dash
import
dcc
,
html
from
dash.
dependencies
import
Input
,
Output
import
plotly.
express
as
px
import
pandas
as
pd
Create a Dash application
app
=
dash.
Dash
(
name
)
Create sample data
df
=
pd.
DataFrame
(
{
'City'
:
[
'Beijing'
,
'Shanghai'
,
'Guangzhou'
,
'Shenzhen'
]
,
'Temperature'
:
[
20
,
25
,
30
,
28
]
,
'Humidity'
:
[
50
,
60
,
70
,
65
]
}
)
Define the application's layout
app.
layout
=
html.
Div
(
[
dcc.
Dropdown
(
id
=
'city-dropdown'
,
options
=
[
{
'label'
: city
,
'value'
: city
}
for
city
in
df
[
'City'
]
]
,
value
=
'Beijing'
The default selected city
)
,
dcc.
Graph
(
id
=
'temperature-graph'
)
,
dcc.
Graph
(
id
=
'humidity-graph'
)
]
)
Define the Callback Function
@
app.
callback
(
[
Output
(
'temperature-graph'
,
'figure'
)
,
Output
(
'humidity-graph'
,
'figure'
)
]
,
[
Input
(
'city-dropdown'
,
'value'
)
]
)
def
update_graphs
(
selected_city
)
:
filtered_df
=
df
[
df
[
'City'
]
==
selected_city
]
temperature_fig
=
px.
bar
(
filtered_df
,
x
=
'City'
,
y
=
'Temperature'
,
title
=
'Temperature'
)
humidity_fig
=
px.
bar
(
filtered_df
,
x
=
'City'
,
y
=
'Humidity'
,
title
=
'Humidity'
)
return
temperature_fig
,
humidity_fig
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
In the example above, the user can select a city via the dropdown menu, and the application updates the temperature and humidity bar charts based on the user's selection.
