Same data for multiple outputs

I have a situation where I fetch something some the database, do some calculation, and end up with a big list that I want to broadcast to multiple components in the layout. The only obvious way I can think of is something like:

@app.callback(
    Output("comp1", "some_data"),
    Output("comp2", "some_data"),
    Output("comp3", "some_data"),
    Input("input_comp", "value"))
def my_callback(input_value):
    results_list = read_db_and_calculate()
    return results_list, results_list, results_list

My thought is that this will cause the same list to be serialized three times and triple the payload of the update. Is there a better way?

Hi @Joechan you could return the list once into, let’s say, comp1 and redistribute the data to comp2 and comp3 via a clientside callback.

I recommend using a dcc.Store as the output and then adding a clientside callback with the store as the input and the “comp1”, “comp2”, “comp3” as the outputs. See example:

@app.callback(
    Output("data_store", "data"),
    Input("input_comp", "value"))
def my_callback(input_value):
    results_list = read_db_and_calculate()
    return results_list

clientside_callback(
    'data => [data, data, data]',
    Output("comp1", "some_data"),
    Output("comp2", "some_data"),
    Output("comp3", "some_data"),
    Input('data_store', 'data')
)
1 Like