Skip to content

Commit

Permalink
only drop original if there is lazy data
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyielia committed Oct 26, 2023
1 parent b7ddeaa commit c428f49
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
66 changes: 52 additions & 14 deletions vizro-core/examples/default/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ def retrieve_gapminder():


data_manager["gapminder"] = retrieve_gapminder
data_manager["gapminder2"] = retrieve_gapminder

df_gapminder = px.data.gapminder()
df_gapminder2 = px.data.gapminder()


@capture("action")
def delete_memoized_cache(delete_button_id_n_clicks):
"""Delete memoized cache."""
"""Delete one memoized cache."""
if delete_button_id_n_clicks:
data_manager._cache.delete_memoized(data_manager._get_original_data, data_manager, "gapminder")
return None


@capture("action")
def empty_cache(empty_button_id_n_clicks):
"""Empty the entire cache."""
if empty_button_id_n_clicks:
data_manager._cache.cache.clear()


page = vm.Page(
Expand All @@ -30,38 +40,66 @@ def delete_memoized_cache(delete_button_id_n_clicks):
vm.Graph(
figure=px.box(
"gapminder",
# df_gapminder,
x="continent",
y="lifeExp",
color="continent",
title="Distribution per continent",
),
id="the_graph",
),
# vm.Graph(
# figure=px.box(
# "gapminder",
# x="continent",
# y="lifeExp",
# color="continent",
# title="Distribution per continent",
# ),
# id="the_graph2",
# ),
vm.Graph(
id="the_graph2",
figure=px.scatter(
"gapminder2",
# df_gapminder2,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
facet_col="continent",
labels={
"gdpPercap": "GDP per capita",
"pop": "Population",
"lifeExp": "Life expectancy",
"continent": "Continent",
},
range_y=[25, 90],
color_discrete_map={
"Africa": "#00b4ff",
"Americas": "#ff9222",
"Asia": "#3949ab",
"Europe": "#ff5267",
"Oceania": "#08bdba",
},
),
),
vm.Button(
id="delete_button_id",
text="delete_memoized_cache",
actions=[
vm.Action(
function=delete_memoized_cache(),
inputs=["delete_button_id.n_clicks"],
outputs=[],
)
]
),
vm.Button(
id="empty_button_id",
text="empty_cache",
actions=[
vm.Action(
function=empty_cache(),
inputs=["empty_button_id.n_clicks"],
)
]
)
],
controls=[
vm.Filter(
column="year",
targets=["the_graph"],
selector=vm.RangeSlider(
title="Select timeframe",
),
Expand Down Expand Up @@ -93,7 +131,7 @@ def delete_memoized_cache(delete_button_id_n_clicks):
# "CACHE_THRESHOLD": 20, # The maximum number of items the cache can hold
# "CACHE_DEFAULT_TIMEOUT": 3000, # Unit of time is seconds
# }
data_manager._cache.config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 3000,}
data_manager._cache.config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 60,}
Vizro().build(dashboard).run()
# Vizro().build(dashboard).run(
# threaded=False,
Expand Down
4 changes: 2 additions & 2 deletions vizro-core/src/vizro/_vizro.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def build(self, dashboard: Dashboard):
data_manager._cache.init_app(self.dash.server, config=data_manager._cache.config)
# Note that model instantiation and pre_build are independent of Dash.
self._pre_build()
# to clear original data if the cache type is not NullCache
data_manager._drop_original_data()

self.dash.layout = dashboard.build()
# to clear original data if the cache type is not NullCache
data_manager._clean_original_data()

return self

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 @@ -89,7 +89,12 @@ def _get_component_data(self, component_id: ComponentID) -> pd.DataFrame:
raise KeyError(f"Component {component_id} does not exist. You need to call add_component first.")
dataset_name = self.__component_to_original[component_id]

return self._get_original_data(dataset_name)
component_data = self._get_original_data(dataset_name)

# clean up original data if the cache type is not NullCache
self._clean_original_data()

return component_data

def _has_registered_data(self, component_id: ComponentID) -> bool:
try:
Expand All @@ -102,13 +107,17 @@ def _clear(self):
self.__init__() # type: ignore[misc]

# to clear original data if the cache type is not NullCache
def _drop_original_data(self):
logger.debug(f"__original_data: {self.__original_data}")
logger.debug(f"config: {self._cache.config}")
logger.debug("drop original data")
if self._cache.config["CACHE_TYPE"] != "NullCache":
self.__original_data = {}
logger.debug(f"__original_data: {self.__original_data}")
def _clean_original_data(self):
logger.debug(f"__original_data before cleaning: {self.__original_data.keys()}")
# logger.debug(f"config: {self._cache.config}")
logger.debug("clean original data")
if self._cache.config["CACHE_TYPE"] == "NullCache":
return
for dataset_name in list(self.__original_data.keys()):
if dataset_name in self.__lazy_data:
logger.debug(f"drop --> {dataset_name}")
del self.__original_data[dataset_name]
logger.debug(f"__original_data after cleaning: {self.__original_data.keys()}")


data_manager = DataManager()
Expand Down

0 comments on commit c428f49

Please sign in to comment.