-
Notifications
You must be signed in to change notification settings - Fork 7
/
4_callbacks.py
42 lines (35 loc) · 1.12 KB
/
4_callbacks.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
# Import packages
from dash import Dash, html, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.express as px
# Incorporate data
df = px.data.gapminder()
# Initialize the app
app = Dash(__name__)
# App layout
app.layout = html.Div(
[
html.Div(children="My First App with Data, Graph, and Controls"),
html.Hr(),
dcc.Dropdown(
options=["pop", "lifeExp", "gdpPercap"],
value="lifeExp",
id="metric-controls",
),
# dcc.RadioItems(options=["pop", "lifeExp", "gdpPercap"], value="lifeExp", id="metric-controls"),
html.Br(),
dash_table.DataTable(data=df.to_dict("records"), page_size=10),
dcc.Graph(figure={}, id="my-graph"),
]
)
# Add controls to build the interaction
@callback(
Output(component_id="my-graph", component_property="figure"),
Input(component_id="metric-controls", component_property="value"),
)
def update_graph(col_chosen):
fig = px.histogram(df, x="continent", y=col_chosen, histfunc="avg")
return fig
# Run the app
if __name__ == "__main__":
app.run_server(debug=True)