Black Lives Matter. Please consider donating to Black Girls Code today.

Callback for multiple core components

Hello everyone. I’m new to Python and programming. I recently discovered Dash and hoping to build some cool things eventually. I’m struggling a bit with callbacks in general, but have a specific question on my first dashboard. Below is my code. It introduces a drop down with different county names as the options. When a county is selected, that county’s monthly number of Emergency Room visits is displayed in a graph (all dummy data). This part works great.

The next step though is where I’m having the trouble. I added two radio buttons: ‘Raw Trend’ & ‘By 100 ADP.’ I would like not only for the user to select the desired county, but then to use the radio buttons to view the data graphed two different ways. The difference in the graph is simply changing the Y axis from df[‘ER Visits’] which is what already is happening to df[‘By100ADP’] when the ‘By 100 ADP’ radio button is selected.

I tried to create another decorator for the radio buttons to also update the graph figure, but got an error message saying

You have already assigned a callback to the output
with ID “graph” and property “figure”. An output can only have
a single callback function. Try combining your inputs and
callback functions together into one function.

But alas, my Python chops aren’t good enough to do this. Of course it could be that my approach is altogether off and so thought I would ask for some help if somebody out there has time to shed some light. Any thoughts? Now starts the code…I should clarify that this gives me the drop down that works, but radio buttons are not doing anything…

import plotly.offline as pyo
import plotly.graph_objs as go
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd

df = pd.read_csv(‘DummyADP.csv’)

df[‘ER_Visits’] = df[‘ADP’]*.23
df[‘By100ADP’] = df[‘ER_Visits’]/df[‘ADP’]*100

County_Options = []
for county in df[‘County’].unique():
County_Options.append({‘label’:str(county), ‘value’:county})
print(County_Options)

app = dash.Dash()

app.layout = html.Div([
html.Label(‘Select County’),
dcc.Dropdown(id=‘County-Selector’,options = County_Options,
value=‘Alameda’),
dcc.RadioItems(id= ‘by100picker’,
options=[
{‘label’: ‘By 100 ADP’, ‘value’:‘By 100 ADP’},
{‘label’: ‘Raw Trend’, ‘value’: ‘Raw Trend’}],
value = ‘Raw Trend’
),
dcc.Graph(id=‘graph’)

			])

@app.callback(Output(‘graph’,‘figure’),
[Input(‘County-Selector’,‘value’)])

def update_figure(selected_county):
filtered_df = df[df[‘County’]== selected_county]

traces = []
for county in filtered_df['County'].unique():
	traces.append(go.Scatter(
		x = filtered_df['Period'],
		y = filtered_df['ER_Visits'],
		mode = 'markers+lines' 
	))


return {'data': traces,
		'layout':go.Layout(title = selected_county,
							xaxis = dict(title='Period'),
							yaxis = dict(title='ER Visits'))}

if name == ‘main’:
app.run_server()