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

Callback Functions Not Firing as Expected

I have a handful of functions that should all fire when the page is rendered:

@app.callback(
    dash.dependencies.Output('topic-map', 'figure'),
    [dash.dependencies.Input('hidden-df', 'children'),
    dash.dependencies.Input('topic-dropdown', 'value'),
    dash.dependencies.Input('color-json', 'children')])
def update_map(data, topic_values, topic_colors):
    
    print('map about to be created')
    
    df = pd.read_json(data)
    
    if 'display_text' not in df.columns:
        df['display_text'] = [split_text(str(t)) for t in df.text]
    
    fig = generate_map(df, topic_values, topic_colors)
    print('map created')
    return fig

@app.callback(dash.dependencies.Output('topic-time', 'figure'),
    [dash.dependencies.Input('hidden-df', 'children'),
    dash.dependencies.Input('topic-dropdown', 'value'),
    dash.dependencies.Input('color-json', 'children')])
def update_time_series(data, selected_topics, topic_colors):
    
    print('time series about to be created')
    df = pd.read_json(data)
    df = df.loc[df['max_topic'].isin(selected_topics)]
    
    if 'datetime' not in df.columns:
        df['datetime'] = pd.to_datetime(df['date'])
        df['datetime'] = [i.date() for i in df.date]
    
    data = []
    for topic in selected_topics:
        df_group = df.loc[df.max_topic == topic].groupby(['datetime']).count()
        data.append(Scatter(y=df_group['lat'], x=df_group.index,name='Topic {}'.format(topic),
                            line=dict(color=topic_colors[topic])))
    
    layout = Layout(margin={'b':30,'t':30})
    fig = dict(layout=layout, data=data)
    print('time series created')
    return fig

#get dataframe
@app.callback(dash.dependencies.Output('hidden-df', 'children'),
             [dash.dependencies.Input('month-slider', 'value')])
def get_df(month_value):
    df = pd.read_csv('data/' + months[month_value] + '.csv', encoding='iso-8859-1')
    df.reindex(np.arange(0,df.shape[0]))
    if 'topic_name' not in df.columns:
            df['topic_name'] = 'unnamed'
            print(df.head())
    
    print('data read in')
    return df.to_json()

#get colors
@app.callback(dash.dependencies.Output('color-json', 'children'),
             [dash.dependencies.Input('hidden-df', 'children')])
def get_colors(data):
    df = pd.read_json(data)
    topic_colors = get_topic_colors(df.max_topic.unique())
    print('colors generated')
    return topic_colors

@app.callback(dash.dependencies.Output('topic-dropdown', 'options'),
    [dash.dependencies.Input('hidden-df', 'children')])
def get_topic_selections(data):
    
    df = pd.read_json(data)
    
    topic_options = df['max_topic'].unique()
    topic_labels = [str(i) + ': ' + str(list(df.loc[df.max_topic == i, 'topic_name'])[0]) for i in topic_options]
    
    colors = get_topic_colors(topic_options)
    print('Number of topic selections: ', str(len(topic_options)))
    return [{'label': topic_labels[i], 'value': topic_options[i]} for i in range(len(topic_labels))]

The print statements are peppered throughout for troubleshooting purposes. The get_df, get_colors, and get_topic_selections all fire successfully based on the terminal’s output of the print statements. However, it appears that the two plotting functions, update_time_series and update_map, do not even begin. I thought it might be an issue with the incoming json and color parameters, but the print statements that show when the functions start (‘map about to be created’ and ‘time series about to be created’), never show up in the terminal. It appears to just be hanging on the last POST. What is wrong with my code, and why won’t these callback functions work?

UPDATE: I did find that the update_map and update_time_series functions attempt to run before the json is loaded in. So it throws an exception and then just hangs. I would think that once the data goes from None to a JSON object, that should register as new input and attempt to run those callback functions again? Why is that not the case?

1 Like

Hm, hard to say. It might be a bug. Could you create a simple reproducable example that demonstrates the issue?

Thanks for reporting! :bowing_woman:

hi guys, i have faced a same issue ,at last i figured the error ,I misconfigured another dcc figure id ,when i corrected that id ,this automatically corrected my callback problem

I think I encountered a similar error myself, and made a post about it here.