瀏覽代碼

fix decouple metadata (#1446)

* fix decouple metadata

* per Fred + refactor

* per Fred
Dinh Long Nguyen 11 月之前
父節點
當前提交
0e9e9bf76f

+ 3 - 1
frontend/taipy-gui/base/src/app.ts

@@ -24,6 +24,7 @@ export class TaipyApp {
     appId: string;
     clientId: string;
     context: string;
+    metadata: Record<string, unknown>;
     path: string | undefined;
     routes: Route[] | undefined;
     wsAdapters: WsAdapter[];
@@ -41,6 +42,7 @@ export class TaipyApp {
         this.functionData = undefined;
         this.clientId = "";
         this.context = "";
+        this.metadata = {};
         this.appId = "";
         this.routes = undefined;
         this.path = path;
@@ -175,7 +177,7 @@ export class TaipyApp {
     }
 
     getPageMetadata() {
-        return JSON.parse(localStorage.getItem("tp_cp_meta") || "{}");
+        return this.metadata;
     }
 }
 

+ 0 - 1
frontend/taipy-gui/base/src/index.ts

@@ -7,5 +7,4 @@ export type { OnChangeHandler, OnInitHandler, ModuleData };
 
 window.addEventListener("beforeunload", () => {
     document.cookie = "tprh=;path=/;Max-Age=-99999999;";
-    localStorage.removeItem("tp_cp_meta");
 });

+ 9 - 3
frontend/taipy-gui/base/src/wsAdapter.ts

@@ -43,9 +43,15 @@ export class TaipyWsAdapter extends WsAdapter {
                 taipyApp.clientId = id;
                 taipyApp.updateContext(taipyApp.path);
             } else if (message.type === "GMC") {
-                const mc = (message.payload as Record<string, unknown>).data as string;
-                window.localStorage.setItem("ModuleContext", mc);
-                taipyApp.context = mc;
+                const payload = message.payload as Record<string, unknown>;
+                taipyApp.context = payload.context as string;
+                if (payload?.metadata) {
+                    try {
+                        taipyApp.metadata = JSON.parse((payload.metadata as string) || "{}");
+                    } catch (e) {
+                        console.error("Error parsing metadata from Taipy Designer", e);
+                    }
+                }
             } else if (message.type === "GDT") {
                 const payload = message.payload as Record<string, ModuleData>;
                 const variableData = payload.variable;

+ 1 - 5
frontend/taipy-gui/src/components/Taipy/Navigate.tsx

@@ -27,7 +27,7 @@ const Navigate = ({ to, params, tab, force }: NavigateProps) => {
     const { dispatch, state } = useContext(TaipyContext);
     const navigate = useNavigate();
     const location = useLocation();
-    const SPECIAL_PARAMS = ["tp_reload_all", "tp_reload_same_route_only", "tprh", "tp_cp_meta"];
+    const SPECIAL_PARAMS = ["tp_reload_all", "tp_reload_same_route_only", "tprh"];
 
     useEffect(() => {
         if (to) {
@@ -65,10 +65,6 @@ const Navigate = ({ to, params, tab, force }: NavigateProps) => {
                     if (tprh !== undefined) {
                         // Add a session cookie for the resource handler id
                         document.cookie = `tprh=${tprh};path=/;`;
-                        const meta = params?.tp_cp_meta;
-                        if (meta !== undefined) {
-                            localStorage.setItem("tp_cp_meta", meta);
-                        }
                         navigate(0);
                     }
                 }

+ 8 - 3
taipy/gui/gui.py

@@ -1116,13 +1116,19 @@ class Gui:
                 page_path = Gui.__root_page_name
             # Get Module Context
             if mc := self._get_page_context(page_path):
+                page_renderer = self._get_page(page_path)._renderer
                 self._bind_custom_page_variables(
-                    self._get_page(page_path)._renderer, self._get_client_id()
+                    page_renderer, self._get_client_id()
                 )
+                # get metadata if there is one
+                metadata: t.Dict[str, t.Any] = {}
+                if hasattr(page_renderer, "_metadata"):
+                    metadata = getattr(page_renderer, "_metadata", {})
+                meta_return = json.dumps(metadata, cls=_TaipyJsonEncoder) if metadata else None
                 self.__send_ws(
                     {
                         "type": _WsType.GET_MODULE_CONTEXT.value,
-                        "payload": {"data": mc},
+                        "payload": {"context": mc, "metadata": meta_return},
                     }
                 )
 
@@ -2183,7 +2189,6 @@ class Gui:
                 to=page_name,
                 params={
                     _Server._RESOURCE_HANDLER_ARG: pr._resource_handler.get_id(),
-                    _Server._CUSTOM_PAGE_META_ARG: json.dumps(pr._metadata, cls=_TaipyJsonEncoder),
                 },
             ):
                 # Proactively handle the bindings of custom page variables

+ 0 - 1
taipy/gui/server.py

@@ -48,7 +48,6 @@ class _Server:
     __OPENING_CURLY = r"\1&#x7B;"
     __CLOSING_CURLY = r"&#x7D;\2"
     _RESOURCE_HANDLER_ARG = "tprh"
-    _CUSTOM_PAGE_META_ARG = "tp_cp_meta"
 
     def __init__(
         self,