Skip to content

Commit

Permalink
direct add to dm
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyielia committed Nov 8, 2023
1 parent 4ce8129 commit 822d325
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
25 changes: 20 additions & 5 deletions vizro-core/examples/default/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 17 additions & 8 deletions vizro-core/src/vizro/managers/_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 822d325

Please sign in to comment.