From faddd9514ed381da1ab4010767f7318630f6cbfe Mon Sep 17 00:00:00 2001 From: Dinh Long Nguyen Date: Wed, 15 May 2024 20:36:31 +0700 Subject: [PATCH] Propagate shared variable for new scope (#1080) (#1287) * Propagate shared variable for new scope * put gui type in quote --- taipy/gui/data/data_scope.py | 10 +++++++++- taipy/gui/utils/_bindings.py | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/taipy/gui/data/data_scope.py b/taipy/gui/data/data_scope.py index baca8357d9..f47dc4f421 100644 --- a/taipy/gui/data/data_scope.py +++ b/taipy/gui/data/data_scope.py @@ -16,13 +16,17 @@ from .._warnings import _warn +if t.TYPE_CHECKING: + from ..gui import Gui + class _DataScopes: _GLOBAL_ID = "global" _META_PRE_RENDER = "pre_render" _DEFAULT_METADATA = {_META_PRE_RENDER: False} - def __init__(self) -> None: + def __init__(self, gui: "Gui") -> None: + self.__gui = gui self.__scopes: t.Dict[str, SimpleNamespace] = {_DataScopes._GLOBAL_ID: SimpleNamespace()} # { scope_name: { metadata: value } } self.__scopes_metadata: t.Dict[str, t.Dict[str, t.Any]] = { @@ -63,6 +67,10 @@ def create_scope(self, id: str) -> None: if id not in self.__scopes: self.__scopes[id] = SimpleNamespace() self.__scopes_metadata[id] = _DataScopes._DEFAULT_METADATA.copy() + # Propagate shared variables to the new scope from the global scope + for var in self.__gui._get_shared_variables(): + if hasattr(self.__scopes[_DataScopes._GLOBAL_ID], var): + setattr(self.__scopes[id], var, getattr(self.__scopes[_DataScopes._GLOBAL_ID], var, None)) def delete_scope(self, id: str) -> None: # pragma: no cover if self.__single_client: diff --git a/taipy/gui/utils/_bindings.py b/taipy/gui/utils/_bindings.py index f47ded91a6..04886e8f5e 100644 --- a/taipy/gui/utils/_bindings.py +++ b/taipy/gui/utils/_bindings.py @@ -23,7 +23,7 @@ class _Bindings: def __init__(self, gui: "Gui") -> None: self.__gui = gui - self.__scopes = _DataScopes() + self.__scopes = _DataScopes(gui) def _bind(self, name: str, value: t.Any) -> None: if hasattr(self, name): @@ -69,7 +69,7 @@ def _get_or_create_scope(self, id: str): return id, create def _new_scopes(self): - self.__scopes = _DataScopes() + self.__scopes = _DataScopes(self.__gui) def _get_data_scope(self): return self.__scopes.get_scope(self.__gui._get_client_id())[0]