Selaa lähdekoodia

Optimize pre render step (#840)

Dinh Long Nguyen 1 vuosi sitten
vanhempi
säilyke
2f409345ac
3 muutettua tiedostoa jossa 21 lisäystä ja 5 poistoa
  1. 10 4
      taipy/gui/data/data_scope.py
  2. 7 0
      taipy/gui/gui.py
  3. 4 1
      taipy/gui/utils/_bindings.py

+ 10 - 4
taipy/gui/data/data_scope.py

@@ -19,9 +19,13 @@ from .._warnings import _warn
 
 class _DataScopes:
     _GLOBAL_ID = "global"
+    _META_PRE_RENDER = "pre_render"
+    _DEFAULT_METADATA = {_META_PRE_RENDER: False}
 
     def __init__(self) -> None:
         self.__scopes: t.Dict[str, SimpleNamespace] = {_DataScopes._GLOBAL_ID: SimpleNamespace()}
+        # { scope_name: { metadata: value } }
+        self.__scopes_metadata: t.Dict[str, t.Dict[str, str]] = {_DataScopes._GLOBAL_ID: _DataScopes._DEFAULT_METADATA.copy()}
         self.__single_client = True
 
     def set_single_client(self, value: bool) -> None:
@@ -30,20 +34,20 @@ class _DataScopes:
     def is_single_client(self) -> bool:
         return self.__single_client
 
-    def get_scope(self, client_id: t.Optional[str]) -> SimpleNamespace:
+    def get_scope(self, client_id: t.Optional[str]) -> t.Tuple[SimpleNamespace, t.Dict[str, str]]:
         if self.__single_client:
-            return self.__scopes[_DataScopes._GLOBAL_ID]
+            return self.__scopes[_DataScopes._GLOBAL_ID], self.__scopes_metadata[_DataScopes._GLOBAL_ID]
         # global context in case request is not registered or client_id is not
         # available (such as in the context of running tests)
         if not client_id:
             _warn("Empty session id, using global scope instead.")
-            return self.__scopes[_DataScopes._GLOBAL_ID]
+            return self.__scopes[_DataScopes._GLOBAL_ID], self.__scopes_metadata[_DataScopes._GLOBAL_ID]
         if client_id not in self.__scopes:
             _warn(
                 f"Session id {client_id} not found in data scope. Taipy will automatically create a scope for this session id but you may have to reload your page."  # noqa: E501
             )
             self.create_scope(client_id)
-        return self.__scopes[client_id]
+        return self.__scopes[client_id], self.__scopes_metadata[client_id]
 
     def get_all_scopes(self) -> t.Dict[str, SimpleNamespace]:
         return self.__scopes
@@ -56,6 +60,7 @@ class _DataScopes:
             return
         if id not in self.__scopes:
             self.__scopes[id] = SimpleNamespace()
+            self.__scopes_metadata[id] = _DataScopes._DEFAULT_METADATA.copy()
 
     def delete_scope(self, id: str) -> None:  # pragma: no cover
         if self.__single_client:
@@ -65,3 +70,4 @@ class _DataScopes:
             return
         if id in self.__scopes:
             del self.__scopes[id]
+            del self.__scopes_metadata[id]

+ 7 - 0
taipy/gui/gui.py

@@ -508,6 +508,9 @@ class Gui:
     def _get_data_scope(self) -> SimpleNamespace:
         return self.__bindings._get_data_scope()
 
+    def _get_data_scope_metadata(self) -> t.Dict[str, str]:
+        return self.__bindings._get_data_scope_metadata()
+
     def _get_all_data_scopes(self) -> t.Dict[str, SimpleNamespace]:
         return self.__bindings._get_all_scopes()
 
@@ -1943,6 +1946,9 @@ class Gui:
     def __pre_render_pages(self) -> None:
         """Pre-render all pages to have a proper initialization of all variables"""
         self.__set_client_id_in_context()
+        scope_metadata = self._get_data_scope_metadata()
+        if scope_metadata[_DataScopes._META_PRE_RENDER]:
+            return
         for page in self._config.pages:
             if page is not None:
                 with contextlib.suppress(Exception):
@@ -1950,6 +1956,7 @@ class Gui:
                         self._bind_custom_page_variables(page._renderer, self._get_client_id())
                     else:
                         page.render(self, silent=True)
+        scope_metadata[_DataScopes._META_PRE_RENDER] = True
 
     def _get_navigated_page(self, page_name: str) -> t.Any:
         nav_page = page_name

+ 4 - 1
taipy/gui/utils/_bindings.py

@@ -70,7 +70,10 @@ class _Bindings:
         self.__scopes = _DataScopes()
 
     def _get_data_scope(self):
-        return self.__scopes.get_scope(self.__gui._get_client_id())
+        return self.__scopes.get_scope(self.__gui._get_client_id())[0]
+
+    def _get_data_scope_metadata(self):
+        return self.__scopes.get_scope(self.__gui._get_client_id())[1]
 
     def _get_all_scopes(self):
         return self.__scopes.get_all_scopes()