Callback on server start?

I have a Dash app running to process some data. The data is being stored in a dcc.Store with storage type local to have the data persist through sessions and browsers.

But as the system gets shut off when not needed, i would like to have the data cleared to not contaminate the next “session” with old data.

I would like to just be able to pull the plug on the entire thing, so a storage wipe on startup would be the most ideal.

I tried to just make a callback with the input being the storage/memory and the output being clear_data to clear it but it appears that gets called every time the memory changes so that is unusable.

Can anyone point me to relevant resources or have some tips for me? I sadly can’T seem to find anything relevant to this… :frowning:

Hi @Apotrox welcome to the forums. I did this the other way around, load data at startup.

You can use any component property which is not changed during runtime of your app to trigger the initial callback.

@callback(
    Output(your-store, data),
    Input(navbar, style)
)
def wipe(_):
   return {}

Since we are not using prevent_ initial_ call=True this callback gets called at startup and clears the data from the store.

What would be a component that will not get changed during runtime, even when reloading the browser tab or opening it on another browser?
Managed to get it working with a simple H5 that has no content but that gets reloaded every session.
Also tried to do it with a button but that gets initialized every session too.

To give a bit more context to the system, its a sensor for my 3d printing enclosure. I’d like to only have it record data and display it on the graph per printing session. And to not have the previous printing session “contaminate” the data of the current one, i’d like to be able to clear the data every time the raspberry pi this app is supposed to run on, starts up.

Maybe you might want to consider server-side caching of the recorded data?

If the data is not too big you could store it for example with CACHE_TYPE="SimpleCache" from Flask-Caching, which stores the data in memory, so it should be cleared every time you restart the server. Or you could clear cache explicitly in startup code.

1 Like

Will look into that, thanks!

For anyone looking for a solution to this, i ended up just serializing the data to a json file continously and reading it from there to update the graph when needed.
To delete the data at startup, i just overwrote the file with nothing or deleted and recreated it.
Also removed the client side caching to make it simpler.

Really couldn’t get flask caching to work unless deep diving into flask itself, which i honestly didn’t want to do.
Still thanks to the two actually giving me good suggestions :slight_smile:

2 Likes