diff --git a/vizro-core/examples/default/app.py b/vizro-core/examples/default/app.py index 68fef8c78..31ebf25b4 100644 --- a/vizro-core/examples/default/app.py +++ b/vizro-core/examples/default/app.py @@ -23,22 +23,37 @@ def retrieve_gapminder(): # 1. data_manager["gapminder"] = VizroDataSet( retrieve_gapminder, - # timeout=600, - unless=lambda: True + timeout=600, + # unless=lambda: True ) data_manager["gapminder2"] = VizroDataSet( retrieve_gapminder, - timeout=0 + timeout=600 ) # 2. # data_manager["gapminder"] = retrieve_gapminder # print("to set cache config") -# data_manager["gapminder"].set_cache_config(timeout=6000) +# data_manager._add_cache_config( +# "gapminder", +# timeout=60*3 +# ) # # data_manager["gapminder2"] = retrieve_gapminder # print("to set cache config") -# data_manager["gapminder2"].set_cache_config(unless=lambda: True) +# data_manager._add_cache_config( +# "gapminder2", +# unless=lambda: True +# ) + +# 2. +# data_manager["gapminder"] = retrieve_gapminder +# print("to set cache config") +# data_manager["gapminder"].add_cache_config(timeout=6000) +# +# data_manager["gapminder2"] = retrieve_gapminder +# print("to set cache config") +# data_manager["gapminder2"].add_cache_config(unless=lambda: True) @capture("action") diff --git a/vizro-core/src/vizro/managers/_data_manager.py b/vizro-core/src/vizro/managers/_data_manager.py index 3155f55c2..ec17b33ae 100644 --- a/vizro-core/src/vizro/managers/_data_manager.py +++ b/vizro-core/src/vizro/managers/_data_manager.py @@ -91,14 +91,14 @@ def __setitem__(self, dataset_name: DatasetName, data: Union[pd.DataFrame, pd_La self.__lazy_data[dataset_name] = data.data self.__cache_dataset_arguments[dataset_name] = data.get_cache_config() - def __getitem__(self, dataset_name: DatasetName) -> VizroDataSet: - """Returns the `VizroDataSet` object associated with `dataset_name`.""" - if dataset_name not in self.__original_data and dataset_name not in self.__lazy_data: - raise KeyError(f"Dataset {dataset_name} does not exist.") - # if dataset_name in self.__original_data: # no cache available - # raise ValueError(f"Dataset {dataset_name} is not lazy.") - if dataset_name in self.__lazy_data: - return VizroDataSet(self.__lazy_data[dataset_name], **self.__cache_dataset_arguments.get(dataset_name, {})) + # def __getitem__(self, dataset_name: DatasetName) -> VizroDataSet: + # """Returns the `VizroDataSet` object associated with `dataset_name`.""" + # if dataset_name not in self.__original_data and dataset_name not in self.__lazy_data: + # raise KeyError(f"Dataset {dataset_name} does not exist.") + # # if dataset_name in self.__original_data: # no cache available + # # raise ValueError(f"Dataset {dataset_name} is not lazy.") + # if dataset_name in self.__lazy_data: + # return VizroDataSet(self.__lazy_data[dataset_name], **self.__cache_dataset_arguments.get(dataset_name, {})) # happens before dashboard build @_state_modifier @@ -145,6 +145,15 @@ def _get_component_data(self, component_id: ComponentID) -> pd.DataFrame: # to not do any inplace=True operations, but probably safest to leave it here. return self.__original_data[dataset_name].copy() + def _add_cache_config(self, dataset_name: DatasetName, **kwargs): + """Adds a cache configuration to a dataset. + + This is only relevant if the dataset is lazy. + """ + if dataset_name not in self.__lazy_data: + raise KeyError(f"Dataset {dataset_name} does not exist or is not lazy.") + self.__cache_dataset_arguments[dataset_name] = kwargs + def _has_registered_data(self, component_id: ComponentID) -> bool: try: self._get_component_data(component_id)