Przeglądaj źródła

set default behavior (#2544)

change name
resolves #2344

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 1 miesiąc temu
rodzic
commit
0fad04a8e4

+ 4 - 4
frontend/taipy-gui/base/src/app.ts

@@ -40,7 +40,7 @@ export class TaipyApp {
     _cookieHandler: CookieHandler | undefined;
     variableData: DataManager | undefined;
     functionData: DataManager | undefined;
-    appId: string;
+    guiAddr: string;
     clientId: string;
     context: string;
     metadata: Record<string, unknown>;
@@ -63,7 +63,7 @@ export class TaipyApp {
         this.clientId = "";
         this.context = "";
         this.metadata = {};
-        this.appId = "";
+        this.guiAddr = "";
         this.routes = undefined;
         this.path = path;
         this.socket = socket;
@@ -168,7 +168,7 @@ export class TaipyApp {
     init() {
         this.clientId = "";
         this.context = "";
-        this.appId = "";
+        this.guiAddr = "";
         this.routes = undefined;
         const id = getLocalStorageValue(TAIPY_CLIENT_ID, "");
         this.sendWsMessage("ID", TAIPY_CLIENT_ID, id);
@@ -180,7 +180,7 @@ export class TaipyApp {
     }
 
     initApp() {
-        this.sendWsMessage("AID", "connect", "");
+        this.sendWsMessage("GA", "connect", "");
         this.sendWsMessage("GR", "", "");
     }
 

+ 2 - 2
frontend/taipy-gui/base/src/packaging/taipy-gui-base.d.ts

@@ -73,7 +73,7 @@ export type WsMessageType =
     | "ACK"
     | "GMC"
     | "GDT"
-    | "AID"
+    | "GA"
     | "GR"
     | "FV"
     | "BC"
@@ -125,7 +125,7 @@ export declare class TaipyApp {
     _cookieHandler: CookieHandler | undefined;
     variableData: DataManager | undefined;
     functionData: DataManager | undefined;
-    appId: string;
+    guiAddr: string;
     clientId: string;
     context: string;
     metadata: Record<string, unknown>;

+ 2 - 2
frontend/taipy-gui/base/src/socket.ts

@@ -5,7 +5,7 @@ import { TaipyApp } from "./app";
 export const initSocket = (socket: Socket, taipyApp: TaipyApp) => {
     socket.on("connect", () => {
         taipyApp.onWsMessageEvent("connect", null);
-        if (taipyApp.clientId === "" || taipyApp.appId === "") {
+        if (taipyApp.clientId === "" || taipyApp.guiAddr === "") {
             taipyApp.init();
         }
     });
@@ -13,7 +13,7 @@ export const initSocket = (socket: Socket, taipyApp: TaipyApp) => {
     socket.io.on("reconnect", () => {
         taipyApp.onWsMessageEvent("reconnect", null);
         console.log("WebSocket reconnected");
-        taipyApp.sendWsMessage("AID", "reconnect", taipyApp.appId);
+        taipyApp.sendWsMessage("GA", "reconnect", taipyApp.guiAddr);
     });
     // try to reconnect on connect_error
     socket.on("connect_error", (err) => {

+ 5 - 5
frontend/taipy-gui/base/src/wsAdapter.ts

@@ -25,8 +25,8 @@ export class TaipyWsAdapter extends WsAdapter {
     initWsMessageTypes: string[];
     constructor() {
         super();
-        this.supportedMessageTypes = ["MU", "ID", "GMC", "GDT", "AID", "GR", "AL", "ACK"];
-        this.initWsMessageTypes = ["ID", "AID", "GMC"];
+        this.supportedMessageTypes = ["MU", "ID", "GMC", "GDT", "GA", "GR", "AL", "ACK"];
+        this.initWsMessageTypes = ["ID", "GA", "GMC"];
     }
     handleWsMessage(message: WsMessage, taipyApp: TaipyApp): boolean {
         if (message.type) {
@@ -86,13 +86,13 @@ export class TaipyWsAdapter extends WsAdapter {
                     taipyApp.functionData = new DataManager(functionData);
                     taipyApp.onInitEvent();
                 }
-            } else if (message.type === "AID") {
+            } else if (message.type === "GA") {
                 const payload = message.payload as Record<string, unknown>;
                 if (payload.name === "reconnect") {
                     taipyApp.init();
                     return true;
                 }
-                taipyApp.appId = payload.id as string;
+                taipyApp.guiAddr = payload.id as string;
             } else if (message.type === "GR") {
                 const payload = message.payload as [string, string][];
                 taipyApp.routes = payload;
@@ -114,7 +114,7 @@ export class TaipyWsAdapter extends WsAdapter {
         if (
             this.initWsMessageTypes.includes(message.type) &&
             taipyApp.clientId !== "" &&
-            taipyApp.appId !== "" &&
+            taipyApp.guiAddr !== "" &&
             taipyApp.context !== "" &&
             taipyApp.routes !== undefined
         ) {

+ 15 - 12
frontend/taipy-gui/src/context/taipyReducers.ts

@@ -24,7 +24,7 @@ import { getBaseURL, TIMEZONE_CLIENT } from "../utils";
 import { parseData } from "../utils/dataFormat";
 import { MenuProps } from "../utils/lov";
 import { changeFavicon, getLocalStorageValue, IdMessage, storeClientId } from "./utils";
-import { lightenPayload, sendWsMessage, TAIPY_APP_ID, TAIPY_CLIENT_ID, WsMessage } from "./wsUtils";
+import { lightenPayload, sendWsMessage, TAIPY_GUI_ADDR, TAIPY_CLIENT_ID, WsMessage } from "./wsUtils";
 
 export enum Types {
     SocketConnected = "SOCKET_CONNECTED",
@@ -242,8 +242,8 @@ export const messageToAction = (message: WsMessage) => {
             changeFavicon((message.payload as Record<string, string>)?.value);
         } else if (message.type == "BC") {
             stackBroadcast((message as NamePayload).name, (message as NamePayload).payload.value);
-        } else if (message.type == "AID") {
-            checkAppId((message.payload as Record<string, string>).id);
+        } else if (message.type == "GA") {
+            checkGuiAddr((message.payload as Record<string, string>).id);
         }
     }
     return {} as TaipyBaseAction;
@@ -288,16 +288,19 @@ const initializeBroadcastManagement = (dispatch: Dispatch<TaipyBaseAction>) => {
     }, broadcast_timeout);
 };
 
-// App id
-const checkAppId = (appId: string) => {
-    if (!appId) {
+// Gui Address
+const checkGuiAddr = (guiAddr: string) => {
+    if (!guiAddr) {
         return;
     }
-    appId = `${appId}`;
-    const localAppId = getLocalStorageValue(TAIPY_APP_ID, "");
-    if (!localAppId || localAppId !== appId) {
-        localStorage && localStorage.setItem(TAIPY_APP_ID, appId);
-        localAppId && window.location.assign(getBaseURL());
+    guiAddr = `${guiAddr}`;
+    const localGuiAddr = getLocalStorageValue(TAIPY_GUI_ADDR, "");
+    if (!localGuiAddr || localGuiAddr !== guiAddr) {
+        localStorage && localStorage.setItem(TAIPY_GUI_ADDR, guiAddr);
+        if (localGuiAddr) {
+            console.info("Taipy GUI address changed, reloading the page");
+            window.location.assign(getBaseURL());
+        }
     }
 };
 
@@ -311,7 +314,7 @@ export const initializeWebSocket = (socket: Socket | undefined, dispatch: Dispat
             const id = getLocalStorageValue(TAIPY_CLIENT_ID, "");
             const payload: Record<string, unknown> = { id };
             if (lastReasonServer) {
-                payload["app_id"] = Number(getLocalStorageValue(TAIPY_APP_ID, ""));
+                payload["gui_addr"] = Number(getLocalStorageValue(TAIPY_GUI_ADDR, ""));
             }
             sendWsMessage(socket, "ID", TAIPY_CLIENT_ID, payload, id, undefined, false, () => {
                 dispatch({ type: Types.SocketConnected });

+ 2 - 2
frontend/taipy-gui/src/context/wsUtils.ts

@@ -2,7 +2,7 @@ import { Socket } from "socket.io-client";
 import { nanoid } from 'nanoid'
 
 export const TAIPY_CLIENT_ID = "TaipyClientId";
-export const TAIPY_APP_ID = "TaipyAppId";
+export const TAIPY_GUI_ADDR = "TaipyGuiAddr";
 
 export type WsMessageType =
     | "A"
@@ -20,7 +20,7 @@ export type WsMessageType =
     | "ACK"
     | "GMC"
     | "GDT"
-    | "AID"
+    | "GA"
     | "GR"
     | "FV"
     | "BC"

+ 0 - 1
taipy/gui/_default_config.py

@@ -42,7 +42,6 @@ _default_stylekit: Stylekit = {
 # Default config loaded by app.py
 default_config: Config = {
     "allow_unsafe_werkzeug": False,
-    "app_id": False,
     "async_mode": "gevent",
     "change_delay": None,
     "chart_dark_template": None,

+ 0 - 2
taipy/gui/config.py

@@ -30,7 +30,6 @@ from .utils import _is_in_notebook
 
 ConfigParameter = t.Literal[
     "allow_unsafe_werkzeug",
-    "app_id",
     "async_mode",
     "change_delay",
     "chart_dark_template",
@@ -105,7 +104,6 @@ Config = t.TypedDict(
     "Config",
     {
         "allow_unsafe_werkzeug": bool,
-        "app_id": t.Optional[bool],
         "async_mode": str,
         "change_delay": t.Optional[int],
         "chart_dark_template": t.Optional[t.Dict[str, t.Any]],

+ 10 - 11
taipy/gui/gui.py

@@ -704,10 +704,9 @@ class Gui:
                     payload.get("id", "") if isinstance(payload, dict) else str(payload)
                 )
                 client_id = res[0] if res[1] else None
-                if self._config.config.get("app_id", False):
-                    front_app_id = payload.get("app_id", None) if isinstance(payload, dict) else None
-                    if front_app_id is not None:
-                        self.__handle_ws_app_id({"name": message.get("name"), "payload": front_app_id})
+                front_gui_addr = payload.get("gui_addr", None) if isinstance(payload, dict) else None
+                if front_gui_addr is not None:
+                    self.__handle_ws_gui_addr({"name": message.get("name"), "payload": front_gui_addr})
             expected_client_id = client_id or message.get(Gui.__ARG_CLIENT_ID)
             self.__set_client_id_in_context(expected_client_id)
             g.ws_client_id = expected_client_id
@@ -732,8 +731,8 @@ class Gui:
                         self.__handle_ws_get_module_context(payload)
                     elif msg_type == _WsType.GET_DATA_TREE.value:
                         self.__handle_ws_get_data_tree()
-                    elif msg_type == _WsType.APP_ID.value:
-                        self.__handle_ws_app_id(message)
+                    elif msg_type == _WsType.GUI_ADDR.value:
+                        self.__handle_ws_gui_addr(message)
                     elif msg_type == _WsType.GET_ROUTES.value:
                         self.__handle_ws_get_routes()
                     elif msg_type == _WsType.LOCAL_STORAGE.value:
@@ -1341,18 +1340,18 @@ class Gui:
             send_back_only=True,
         )
 
-    def __handle_ws_app_id(self, message: t.Any):
+    def __handle_ws_gui_addr(self, message: t.Any):
         if not isinstance(message, dict):
             return
         name = message.get("name", "")
         payload = message.get("payload", "")
-        app_id = id(self)
-        if payload == app_id:
+        gui_addr = id(self)
+        if payload == gui_addr:
             return
         self.__send_ws(
             {
-                "type": _WsType.APP_ID.value,
-                "payload": {"name": name, "id": app_id},
+                "type": _WsType.GUI_ADDR.value,
+                "payload": {"name": name, "id": gui_addr},
             },
             send_back_only=True,
         )

+ 1 - 1
taipy/gui/types.py

@@ -43,7 +43,7 @@ class _WsType(Enum):
     BLOCK = "BL"
     NAVIGATE = "NA"
     CLIENT_ID = "ID"
-    APP_ID = "AID"
+    GUI_ADDR = "GA"
     MULTIPLE_MESSAGE = "MS"
     DOWNLOAD_FILE = "DF"
     PARTIAL = "PR"