Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#416-migrate-sql-cli-failing-test
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoandre-avaiga authored Jun 25, 2024
2 parents 918916e + 0e9e9bf commit 4e82132
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
6 changes: 4 additions & 2 deletions frontend/taipy-gui/base/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -41,6 +42,7 @@ export class TaipyApp {
this.functionData = undefined;
this.clientId = "";
this.context = "";
this.metadata = {};
this.appId = "";
this.routes = undefined;
this.path = path;
Expand Down Expand Up @@ -162,7 +164,7 @@ export class TaipyApp {
if (!path || path === "") {
path = window.location.pathname.slice(1);
}
sendWsMessage(this.socket, "GMC", "get_module_context", { path: path }, this.clientId);
sendWsMessage(this.socket, "GMC", "get_module_context", { path: path || "/" }, this.clientId);
}

trigger(actionName: string, triggerId: string, payload: Record<string, unknown> = {}) {
Expand All @@ -175,7 +177,7 @@ export class TaipyApp {
}

getPageMetadata() {
return JSON.parse(localStorage.getItem("tp_cp_meta") || "{}");
return this.metadata;
}
}

Expand Down
1 change: 0 additions & 1 deletion frontend/taipy-gui/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export type { OnChangeHandler, OnInitHandler, ModuleData };

window.addEventListener("beforeunload", () => {
document.cookie = "tprh=;path=/;Max-Age=-99999999;";
localStorage.removeItem("tp_cp_meta");
});
12 changes: 9 additions & 3 deletions frontend/taipy-gui/base/src/wsAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions frontend/taipy-gui/src/components/Taipy/Navigate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions taipy/gui/custom/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class ResourceHandler(ABC):
User can implement this class to provide custom resources for the custom pages
"""

id: str = ""
rh_id: str = ""

def __init__(self) -> None:
_ExternalResourceHandlerManager().register(self)

def get_id(self) -> str:
return self.id if id != "" else str(id(self))
return self.rh_id if self.rh_id != "" else str(id(self))

@abstractmethod
def get_resources(self, path: str, taipy_resource_path: str) -> t.Any:
Expand Down
18 changes: 14 additions & 4 deletions taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,24 @@ def __request_var_update(self, payload: t.Any):

def __handle_ws_get_module_context(self, payload: t.Any):
if isinstance(payload, dict):
page_path = str(payload.get("path"))
if page_path in {"/", ""}:
page_path = Gui.__root_page_name
# Get Module Context
if mc := self._get_page_context(str(payload.get("path"))):
if mc := self._get_page_context(page_path):
page_renderer = self._get_page(page_path)._renderer
self._bind_custom_page_variables(
self._get_page(str(payload.get("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},
}
)

Expand Down Expand Up @@ -2149,6 +2158,8 @@ def _get_page(self, page_name: str):

def _bind_custom_page_variables(self, page: CustomPage, client_id: t.Optional[str]):
"""Handle the bindings of custom page variables"""
if not isinstance(page, CustomPage):
return
with self.get_flask_app().app_context() if has_app_context() else contextlib.nullcontext(): # type: ignore[attr-defined]
self.__set_client_id_in_context(client_id)
with self._set_locals_context(page._get_module_name()):
Expand Down Expand Up @@ -2178,7 +2189,6 @@ def __render_page(self, page_name: str) -> t.Any:
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
Expand Down
1 change: 0 additions & 1 deletion taipy/gui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4e82132

Please sign in to comment.