This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathdash-three-cascading-dropdowns.py
78 lines (60 loc) · 2.34 KB
/
dash-three-cascading-dropdowns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash(__name__)
all_options = {
'America': {
'New York': ['Statue of Liberty', 'Empire State Building'],
'San Francisco': ['Golden Gate Bridge', 'Mission District'],
},
'Canada': {
u'Montréal': ['Biodome', 'Parc Laurier'],
'Toronto': ['CN Tower', 'Royal Ontario Museum'],
}
}
app.layout = html.Div([
dcc.RadioItems(
id='countries-dropdown',
options=[{'label': k, 'value': k} for k in all_options.keys()],
value='America'
),
html.Hr(),
dcc.RadioItems(id='cities-dropdown'),
html.Hr(),
dcc.RadioItems(id='landmarks-dropdown'),
html.Div(id='display-selected-values')
])
@app.callback(
dash.dependencies.Output('cities-dropdown', 'options'),
[dash.dependencies.Input('countries-dropdown', 'value')])
def set_cities_options(selected_country):
return [{'label': i, 'value': i} for i in all_options[selected_country]]
@app.callback(
dash.dependencies.Output('cities-dropdown', 'value'),
[dash.dependencies.Input('cities-dropdown', 'options')])
def set_cities_value(available_options):
return available_options[0]['value']
@app.callback(
dash.dependencies.Output('landmarks-dropdown', 'options'),
[dash.dependencies.Input('countries-dropdown', 'value'),
dash.dependencies.Input('cities-dropdown', 'value')])
def set_landmarks_options(selected_country, selected_city):
return [{'label': i, 'value': i} for i in all_options[selected_country][selected_city]]
@app.callback(
dash.dependencies.Output('landmarks-dropdown', 'value'),
[dash.dependencies.Input('landmarks-dropdown', 'options')])
def set_landmarks_value(available_options):
return available_options[0]['value']
@app.callback(
dash.dependencies.Output('display-selected-values', 'children'),
[dash.dependencies.Input('countries-dropdown', 'value'),
dash.dependencies.Input('cities-dropdown', 'value'),
dash.dependencies.Input('landmarks-dropdown', 'value')])
def set_display_children(selected_country, selected_city, selected_landmark):
return u'{} is in {}, {}'.format(
selected_landmark, selected_city, selected_country,
)
if __name__ == '__main__':
app.run_server(debug=True)