Callbacks reloads plot with random past data

Hi all,

I’m using dash 2.0. ( with bootstrap 1.1.0 ) and have an app deployed with WGSI atop Nginx.

The app runs a simulation and plots the results.

There are callbacks which set and select (sliders, inputs, dropdowns) different parameters, triggering the simulation, as well as other callbacks which simply plot a line or change the plotted domain displayed without rerunning the simulation.

The app tests as well as it should within a local testing environment but when deployed in production, triggering any callback will randomly show some previously displayed plot. It happens with maybe 30% of the callbacks, and is independent of the simulation; e.g. changing the plotted domain will show a plot from a different (but previously called) set of input parameters.

These are specifically previously shown plots; it does not show random garbage. This makes me think there is something with the caching, and maybe this is handled differently in a prod environment.

How can i suppress this behaviour, or get it to work as expected?

Do you use global variables?

I’m not using explicitly defined globals.

the structure is
index.py
pages/model_module

from model_module a layout object is returned via a callback.

Also, by-passing the landing page and setting app.layout = html.Div([model_module.layout]) replicates the behaviour.

@Emil yes, you were correct in assuming this: I am using an object outside the scope of the callback.

I thought i could get around the issue by defining a closure with the object attached to the outer and then updating it with the callback wrapped around the inner function.

And it worked on the localhost, but not in the production system.

This was integral to the app: the simulation and output data were all stored in the object as i did not want to re-run the simulation everytime i wanted to update the figure.

The question is how to update the figure without re-running the simulation. Can i get around this with the State object?

No, that is not possible. You will have stash the data in a Store component, or in a serverside cache.

Firstly, it was resolved by moving the object out of the callback. Thanks for the reminder! ^^
The simulations aren’t so large to cause a major bottleneck when updating the plots with non-simulation related actions,
so it seems okay to just rerun them even if it is redundant.

Re your latter comment, I effectively want to update (and store) some data object with the user- and model-parameters and simulation data; the data is plotted and presented as a table, and should be otherwise downloadable; moreover i want this to be unique and separate to a given session, ie user.
Can I achieve this by following Ex. 4 on Part 5. Sharing Data Between Callbacks?

I haven’t seen the code, but it sounds like you might still be using global variables (as a side note that is OK, if the variables are read-only). As you have experienced, it works sometimes (in your case locally, but not in production). Note that while it might seem to work now, you app might still break if/when you change deployment settings (this comment is obviously only relevant, if you are indeed using global variables).

Yes, that would be the general approach. You could also use a ServersideOutput (from dash-extensions) if you like the dash-component syntax better.