Jelajahi Sumber

State support for IDE (#2222)

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 6 bulan lalu
induk
melakukan
b1e88226e4
2 mengubah file dengan 25 tambahan dan 8 penghapusan
  1. 9 8
      taipy/gui/state.py
  2. 16 0
      taipy/gui/state_support.py

+ 9 - 8
taipy/gui/state.py

@@ -11,7 +11,7 @@
 
 import inspect
 import typing as t
-from abc import abstractmethod
+from abc import ABCMeta, abstractmethod
 from contextlib import nullcontext
 from operator import attrgetter
 from pathlib import Path
@@ -26,7 +26,7 @@ if t.TYPE_CHECKING:
     from .gui import Gui
 
 
-class State(SimpleNamespace):
+class State(SimpleNamespace, metaclass=ABCMeta):
     """Accessor to the bound variables from callbacks.
 
     `State` is used when you need to access the value of variables
@@ -114,9 +114,7 @@ class State(SimpleNamespace):
         val = attrgetter(name)(self)
         _attrsetter(self, name, val)
 
-    def _set_context(self, gui: "Gui") -> t.ContextManager[None]:
-        return nullcontext()
-
+    @abstractmethod
     def broadcast(self, name: str, value: t.Any):
         """Update a variable on all clients.
 
@@ -127,9 +125,7 @@ class State(SimpleNamespace):
             name (str): The variable name to update.
             value (Any): The new variable value.
         """
-        with self._set_context(self._gui):
-            encoded_name = self._gui._bind_var(name)
-            self._gui._broadcast_all_clients(encoded_name, value)
+        raise NotImplementedError
 
     def __enter__(self):
         self._gui.__enter__()
@@ -196,6 +192,11 @@ class _GuiState(State):
     def get_gui(self) -> "Gui":
         return super().__getattribute__(_GuiState.__gui_attr)
 
+    def broadcast(self, name: str, value: t.Any):
+        with self._set_context(self._gui):
+            encoded_name = self._gui._bind_var(name)
+            self._gui._broadcast_all_clients(encoded_name, value)
+
     def __getattribute__(self, name: str) -> t.Any:
         if name == "__class__":
             return _GuiState

+ 16 - 0
taipy/gui/state_support.py

@@ -0,0 +1,16 @@
+# Copyright 2021-2024 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+
+from .state import State
+
+
+class StateSupport(State):
+    pass