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

Clearing memory between multiple instances

Hello, I have created a Dash app that has two Dropdowns, one for a stock and two for a market index and creates a graph comparing the two over a period of time. In my code I define a last_graph variable so the screen doesn’t clear back to nothing in case the user enters back-space in both of the edit boxes.

On on a browser on my development machine I can open the page within any number of tabs and create unique graphs on each one. When I open another tab, it starts blank. When I connect to this instance from different machines on my home network, each independent instance works fine and starts fresh. I have deployed this app on to an AWS instance. I can connect to it and create a graph. However, when I connect to it again on another tab or completely different machine, it doesn’t start fresh but displays the last_graph image created from the most recent session, even when it’s a completely different machine. When I select a Stock or Index via the Dropdown, it starts fresh from there and draws just one line (versus also graphing the stock or index value from the prior session).

Can anyone describe why i am experiencing different behavior between my internal and external deployments? Home is OSX and AWS is Linux, could that be the cause? I suspect this has to do with “your callbacks should never mutate variables outside of their scope”. What I can do so that each new connecting instance completely clears itself or if there’s a better way I should be storing last_graph?

Is there a suggested way to get a unique session id? I implemented the suggestion from https://stackoverflow.com/questions/58610254/unique-session-id-for-different-dash-sessions and the session id is reset each time the app is restarted and common between multiple browser sessions.

Do you store last_graph as a global variable?

essentially. It’s an object variable of the class used to do all the queries.

Then that it probably the issue. It is generally not recommended to use global variables as they cause the kind of issue that you see. Instead, you should store the data either server side (e.g. in a file or a memory cache) or client side, e.g. in a Dash Store object.

raise PreventUpdate is what I needed to use instead of saving the chart and displaying it. I am going to dig into the Dcc.Store examples to learn how to do this correctly in the future and am sure that I will be using a Redis data cache soon.