Skip to content

Commit

Permalink
fix: wrong api url when deployment custom proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
longxiaofei committed May 31, 2024
1 parent 614f938 commit 7790c1b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ const initOnJupyter = async(props: IAppProps) => {
}

const initOnHttpCommunication = async(props: IAppProps) => {
const comm = initHttpCommunication(props.id, props.communicationUrl);
const comm = await initHttpCommunication(props.id, props.communicationUrl);
communicationStore.setComm(comm);
if ((props.gwMode === "explore" || props.gwMode === "filter_renderer") && props.needLoadLastSpec) {
const visSpecResp = await comm.sendMsg("get_latest_vis_spec", {});
Expand Down
38 changes: 36 additions & 2 deletions app/src/utils/communication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,48 @@ const initJupyterCommunication = (gid: string) => {
}
}

const initHttpCommunication = (gid: string, baseUrl: string) => {
const getRealApiUrl = async(basePath: string, baseApiUrl: string) => {
if (basePath === "") {
return baseApiUrl;
}

const basePathPart = basePath.split("/");
const possibleBasePaths: string[] = [];
for (let i = basePathPart.length; i >= 0; i--) {
possibleBasePaths.push(basePathPart.slice(0, i).join("/"));
}
const possibleApiUrls = possibleBasePaths.slice(0, 3).map(path => `${path.length === 0 ? '' : '/'}${path}/${baseApiUrl}`);

return (await Promise.all(possibleApiUrls.map(async(url) => {
try {
const resp = await fetch(
url,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "ping", data: {} }),
}
)
const respJson = await resp.json();
if (respJson.code === 0) {
return url;
}
return null;
} catch {
return null;
}
}))).find(url => url !== null) as string;
}

const initHttpCommunication = async(gid: string, baseUrl: string) => {
// temporary solution in streamlit could
const domain = window.parent.document.location.host.split(".").slice(-2).join('.');
let url = "";
if (domain === "streamlit.app") {
url = `/~/+/_stcore/_pygwalker/comm/${gid}`
} else {
url = `/${baseUrl}/${gid}`
const basePath = window.parent.location.pathname.replace(/\/+$/, '').replace(/^\/+/, '');
url = await getRealApiUrl(basePath, `${baseUrl}/${gid}`);
}

const sendMsg = async(action: string, data: any, timeout: number = 30_000) => {
Expand Down
2 changes: 1 addition & 1 deletion pygwalker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pygwalker.services.global_var import GlobalVarManager
from pygwalker.services.kaggle import show_tips_user_kaggle as __show_tips_user_kaggle

__version__ = "0.4.8.8"
__version__ = "0.4.8.9a0"
__hash__ = __rand_str()

from pygwalker.api.jupyter import walk, render, table
Expand Down
1 change: 1 addition & 0 deletions pygwalker/api/pygwalker.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def _upload_to_cloud_dashboard(data: Dict[str, Any]):

comm.register("get_latest_vis_spec", get_latest_vis_spec)
comm.register("request_data", reuqest_data_callback)
comm.register("ping", lambda _: {})

if self.use_save_tool:
comm.register("upload_spec_to_cloud", upload_spec_to_cloud)
Expand Down
2 changes: 1 addition & 1 deletion pygwalker/communications/streamlit_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
streamlit_comm_map = {}

_STREAMLIT_PREFIX_URL = config.get_option("server.baseUrlPath").strip("/")
BASE_URL_PATH = (_STREAMLIT_PREFIX_URL + "/_stcore/_pygwalker/comm/").strip("/")
BASE_URL_PATH = "/_stcore/_pygwalker/comm/".strip("/")
PYGWALKER_API_PATH = make_url_path_regex(
_STREAMLIT_PREFIX_URL,
r"/_stcore/_pygwalker/comm/(.+)"
Expand Down

0 comments on commit 7790c1b

Please sign in to comment.