Callbacks and Output issues encountered

Hey Guys! Happy New Year!

I hope everyone is doing great.

I am trying to avoid repetition of creating the same charts over a time for a bunch of metrics and In order to achieve this, I have one radioitems with 6 options and one checklist with 3 options. Each radioitems/button represents the metric-based information such as if I click on the white people button, metrics related to white people must be rendered accurately. I simply want one output to display data with respect to selection of options mentioned.

Buttons: btn1 - white , btn2 - aian …btn6-native-hawaii
Checklist Options: total, male, female.

Each button has 3 metrics (total, male, and female) so in total, there are 18 metrics. I want to display the return with respect to each race and selected option from the checklist. Please note: I also want only one option from checklist options to be selected at a time.

Code snippet:


list_of_columns = [
    'statecode', 
    'countycode', 
    'fipscode', 
    'state', 
    'county', 
    'year', 
    'total_population',
    'total_population_male',
    'total_population_female',
    'white_estimate_total',
    'white_estimate_total_male',
    'white_estimate_total_female',
    'aian_estimate_total',
    'aian_estimate_total_male',
    'aian_estimate_total_female',
    'black_estimate_total',
    'black_estimate_total_male',
    'black_estimate_total_female',
    'asian_estimate_total',
    'asian_estimate_total_male',
    'asian_estimate_total_female',
    'hispanic_estimate_total',
    'hispanic_estimate_total_male',
    'hispanic_estimate_total_female',
    'native_hawaii_estimate_total',
    'native_hawaii_estimate_total_male',
    'native_hawaii_estimate_total_female',
]

# # UPDATE TOTAL, MALE, AND FEMALE CHECKLIST OPTIONS 
@app.callback(
    Output('multi-race-total-male-female-checklist-id', 'value', allow_duplicate=True),
    [Input('multi-race-total-male-female-checklist-id', 'value')],
    prevent_initial_call=True,
)
def update_multi_races_checklist(checklist_options):
    # Ensure to select only one value at a time
    return checklist_options[-1] if checklist_options else None

@app.callback(
    [Output('multi-race-total-male-female-checklist-id', 'value', allow_duplicate=True)],
    [Input("multi-race-buttons-id", 'value')],
    prevent_initial_call=True
)
def update_checklist_options_with_buttons(selected_button):
    print(f"Selected button: {selected_button}")

    if selected_button == 'white-race-population-id':
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id']
    elif selected_button == 'aian-race-population-id':
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id']
    elif selected_button == 'black-race-population-id':
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id'] 
    elif selected_button == 'asian-race-population-id':
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id']
    elif selected_button == 'hispanic-race-population-id':
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id']
    else:
        selected_checklist_options = ['multi-race-total-pop-id', 'multi-race-total-pop-male-id', 'multi-race-total-pop-female-id']

    print(f"Checklist IDs: {selected_checklist_options}")
    return selected_checklist_options

@app.callback(
    [
        Output("testing-races_multi-metrics", 'children')
    ],
    [
        Input('filtered-data-store', 'data'),
        Input('multi-race-total-male-female-checklist-id', 'value'),
    ],
    [   
        State('us-states-dropdown-id', 'value'),
        State('us-counties-dropdown-id', 'value'),
        State('prev-yr-dropdown-id', 'value'),
        State('present-yr-dropdown-id', 'value'),
        State("multi-race-buttons-id", 'value'),
    ]
)
def update_multi_races_charts(filtered_data_json, selected_checklist, selected_state, selected_county, var_prev_year, var_curr_year, selected_buttons):
    print(f"selected_checklist: {selected_checklist}")
    print(f"selected_buttons: {selected_buttons}")
    
    filtered_data = pd.read_json(StringIO(filtered_data_json), orient='split')

    if 'multi-race-total-pop-id' in selected_checklist:
        selected_metrics = ['white_estimate_total', 'aian_estimate_total', 'black_estimate_total', 'asian_estimate_total', 'hispanic_estimate_total', 'native_hawaii_estimate_total']
    elif 'multi-race-total-pop-male-id' in selected_checklist:
        selected_metrics = ['white_estimate_total_male', 'aian_estimate_total_male', 'black_estimate_total_male', 'asian_estimate_total_male', 'hispanic_estimate_total_male', 'native_hawaii_estimate_total_male']
    else:
        selected_metrics = ['white_estimate_total_female', 'aian_estimate_total_female', 'black_estimate_total_female', 'asian_estimate_total_female', 'hispanic_estimate_total_female', 'native_hawaii_estimate_total_female']
    
    curr_yr_pop = (
            filtered_data.loc[filtered_data['year'] == int(var_curr_year), selected_metrics].iloc[0]
            if var_curr_year is not None and not filtered_data.loc[filtered_data['year'] == int(var_curr_year)].empty
            else None
    )
    
    
    print(f"Current year pop for race for {selected_buttons} & {selected_checklist}: {curr_yr_pop}")
    
    
    return [html.Div(f"Current Year Pop for {selected_buttons} & {selected_checklist}: {curr_yr_pop}")]

So far, I am close to solve this. However, with above settings, when I click on the white_button, I am getting all the race’s value with respect to each option from the checklist in one display no matter if other buttons are clicked or not.

Current Year Pop for white-race-population-id & multi-race-total-pop-id: white_estimate_total 194886464 aian_estimate_total 2786431 black_estimate_total 41288572 asian_estimate_total 19112979 hispanic_estimate_total 61755866 native_hawaii_estimate_total 624863 Name: 12, dtype: int64

And when I set the selected_metrcs[0] for calculating single curr_yr_pop, I am getting the desired resulst for white race and corresponding options from the checklist. However, when I click on other buttons(aian, black, asian, etc), I am getting the same white race data for each race.

Current Year Pop for white-race-population-id & multi-race-total-pop-id: 194886464
Current Year Pop for white-race-population-id & multi-race-total-pop-male-id: 96850281
Current Year Pop for white-race-population-id & multi-race-total-pop-female-id: 98036183

with above results, we can see, with setting the selected_metrics[0], we are getting the desired result for button white no matter other buttons are clicked or not. I believe I am missing a point to form a relation between buttons and checklist options or maybe missing some indexing stuff.

Your input into this will be highly appreciated.

Thanks so much!

Hi @satyndragautam, could you provide a MRE for this?

Hey thanks so much for offering help. However, I got it resolved. :slight_smile: