dcc.Store - persist based on component value


I want to share some pretty heavy data between the callbacks. For this I use dcc.Store with storage_type=“local”. It’s great. But the thing is that what I want to store depends on the value that I pick in the dropdown.

There is such an option for dropdowns - it’s enough to provide persistence condition (documentation)

But for the city they would like to see the same one as the last time they chose that country - just set persistence=country_name (where country_name is the value of the chosen country) on the city dropdown and we’ll save one preferred city for each country.

To use the example from the documentation - let’s say that I have a large dataset for each city. I don’t want to download it everytime I pick the city. If I looked into this city before and downloaded the data, I’d like my app to remember it.

I would appreciate any tips? :slight_smile:

You can always define a dictionary within the dcc.Store component.


Then when you go to update the data you do this:

def fetch_city_data(storage, city):
    if city in storage:
        return storage[city]
        city_data = download_city_data(city)
        storage[city] = city_data
        return city_data. storage

You’ll need to also return the data updated in the storage component to the Store component.
Hope this helps!