Building Dashboards with Dash and Plotly
Alex Scriven
Data Scientist
from dash.dependencies import Input, Output
component_id
: Identify the componentcomponent_property
: What will be changedcomponent_property
: What to use in triggered function
@app.callback(
Output(component_id='my_plot', component_property='figure'),
Input(component_id='my_input', component_property='value')
)
def some_function(data): # Subset Data # Recreate Figure return fig
dcc.Dropdown(id='title_dd',
options=[{'label':'Title 1',
'value':'Title 1'},
{'label':'Title 2',
'value':'Title 2'}]),
app.layout = html.Div(children=[
dcc.Dropdown(id='title_dd', options=[{'label':'Title 1', 'value':'Title 1'}, {'label':'Title 2', 'value':'Title 2'}]),
dcc.Graph(id='my_graph')])
@app.callback( Output(component_id='my_graph', component_property='figure'),
Input(component_id='title_dd', component_property='value') )
#@app.callback() def update_plot(selection): title = "None Selected" if selection: title = selection
bar_fig = px.bar( data_frame=ecom_sales, title=f'{title}', x='Total Sales ($)', y='Country')
return bar_fig
#@app.callback() def update_plot(input_country): country = 'All Countries'
sales = ecom_sales.copy(deep=True)
if input_country: country = input_country sales = sales[sales['Country'] == input_country]
bar_fig = px.bar( data_frame=sales, title=f"Sales in {country}", x='Total Sales ($)', y='Country') return bar_fig
Building Dashboards with Dash and Plotly