Inconsitent behaviour when reading data from datatables on callback

Hi,

I have the following code that read the content of two editable datatables and produces three figures when a button is clicked:

@app.callback(Output('feat-importance-new', 'figure'),
			Output('new-scores-dist', 'figure'),
			Output('model-performance', 'figure'),
			Input('submit-button', 'n_clicks'),
			[State('table-weight', 'data'),
			State('table-threshold', 'data')],
            prevent_initial_callback=True)
def update_data(submit_button, weight_data, threshold_data):
	
	if 'submit-button' == ctx.triggered_id:
		
		global weight
		global thres
		
		df_weight = pd.DataFrame.from_records(weight_data)
		weight = dict(zip(df_weight['Feature'], df_weight['new_Weight']))
		df_thres = pd.DataFrame.from_records(threshold_data)
		thres = dict(zip(df_thres['Feature'], df_thres['new_Threshold']))
		
		df = remap_risk(df_table_copy)	
		df_en = one_hot_encoding(df)
		df['rescore'] = df_en.apply(rescore, axis=1)
		
		fig_featimp = feat_importance(df_en)
		
		fig_rescore = px.histogram(df, x='rescore', color_discrete_sequence=['#eb3e00'])
		fig_rescore.update_yaxes(showgrid=True, gridcolor='LightGray')
		fig_rescore.update_layout(plot_bgcolor='white')
			
		fig_perf = sanity_checks(df_en)
		
		return fig_featimp, fig_rescore, fig_perf

	else:
		raise PreventUpdate

Now, the second and first figures are correctly updated upon button clicking. By this I mean that the current values from the tables are used.

However, if I only click once, the first figure still uses the old values. I need to click twice in order to update that particular figure.

The function that produces that figure is slow (30 seconds) whereas the others are fast.

I am going to try and save the edited tables to the store as opposed to reading the data from the tables directly.

However, I do not understand this behaviour. Given that the three figures are produced within the same callback, shouldn’t all of them be using the same input data?

Cheers,

Ed

Hi @edmoman,

As far as I can see, the figures do not depend on the arguments of the update_data function. It seems that you are using this callback to update some global variables only. I’m sure you have already seen this recommendations concerning global variables.

1 Like

Hello @edmoman,

Just taking a stab, but it seems that this is undefined in the scope of this callback.

Should this be driven off of the data in one of the tables?

1 Like

Indeed, getting rid of the globals solves the issue. Thanks!

1 Like