Sharing manipulated data in multiple outputs

I’m struggling with this concept. I have a scatter graph which is fed data, and that data is filtered and manipulated by 10 radio button inputs. The filtering is quite complex, a lot of Pandas and dataframes and sql, but it works great.

Now, as well as outputting to my scatter graph, I need to output this manipulated data somewhere else (to a table, or a div, or whatever) but I can’t work out how. Dash can do multiple outputs, but only (to my knowledge!) based on specific inputs. The input can’t be a variable or dataframe/source.

Should I be dumping my data to a global variable, and then use that as an input in another callback? Or am I going about this the wrong way?

Thanks

Will

Normally what I do is to split my transformations in get_data and get_figure, so for instance.

get_data():
df = sql pandas stuff here
return df

get_plot_1():
df = get_data()
figure = [some kind of figure]
return figure

get_plot_2():
df = get_data()
figure = [other kind of figure]
return figure

And make sure to write your callbacks appropriately.

Is that what you mean?
*Sorry about not writing I proper example I hope it’s understandable at least.
** I’m not sure this is the best practice, but it’s what I’ve been doing without experiencing performance issues

1 Like

Use a hidden DIV to output your manipulated data as a JSON dump & then use that data as an Input for further manipulations in other callbacks with JSON loads.

Here is an exmaple from Chris

3 Likes

Thanks both. I ended up using the first example in the link @raghunath posted; that example from @chriddyp is really useful. It still doesn’t seem right to call the same function twice to get identical data, but am new to Flask and reactive coding etc, so am happy that it’s working.

2 Likes

Hello,
I am doing this for a sql based get_data. Which means I am hitting the database multiple times - Global variables is not an options, as it doesnt refresh with page reload.

Is there any other way to do this efficiently?

Thanks
Karthik

Would caching the results of your callback help? This means that repeated callbacks with the same signature won’t cause additional database hits.
See https://plot.ly/dash/performance.

Also see Part 4. Sharing Data Between Callbacks | Dash for Python Documentation | Plotly