Skip to content

Commit

Permalink
Propagate shared variable for new scope (#1080) (#1287)
Browse files Browse the repository at this point in the history
* Propagate shared variable for new scope

* put gui type in quote
  • Loading branch information
dinhlongviolin1 authored May 15, 2024
1 parent e72d58d commit faddd95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion taipy/gui/data/data_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions taipy/gui/utils/_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit faddd95

Please sign in to comment.