Allow localtileserver prefix override and fix inferred prefix on JH#1314
Allow localtileserver prefix override and fix inferred prefix on JH#1314giswqs merged 3 commits intoopengeos:masterfrom
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
This PR updates leafmap.common.get_local_tile_layer() to better handle localtileserver URL prefixing in hosted notebook environments (notably JupyterHub), preventing malformed proxy URLs and allowing user-defined overrides to take precedence.
Changes:
- Only sets
LOCALTILESERVER_CLIENT_PREFIXwhen it is not already defined in the environment. - Adjusts the JupyterHub-derived prefix formatting to avoid producing
//proxy/...URLs whenJUPYTERHUB_SERVICE_PREFIXincludes a trailing slash.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Only override LOCALTILESERVER_CLIENT_PREFIX if it's unset | ||
| if os.environ.get("LOCALTILESERVER_CLIENT_PREFIX") is None: | ||
| # Make it compatible with binder and JupyterHub | ||
| if os.environ.get("JUPYTERHUB_SERVICE_PREFIX") is not None: | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = ( | ||
| f"{os.environ['JUPYTERHUB_SERVICE_PREFIX'].lstrip('/')}proxy/{{port}}" | ||
| ) | ||
|
|
||
| if is_studio_lab(): | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = ( | ||
| f"studiolab/default/jupyter/proxy/{{port}}" | ||
| ) | ||
| elif is_on_aws(): | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = "proxy/{port}" | ||
| elif "prefix" in kwargs: | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = kwargs["prefix"] | ||
| kwargs.pop("prefix") | ||
| if is_studio_lab(): | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = ( | ||
| f"studiolab/default/jupyter/proxy/{{port}}" | ||
| ) | ||
| elif is_on_aws(): | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = "proxy/{port}" | ||
| elif "prefix" in kwargs: | ||
| os.environ["LOCALTILESERVER_CLIENT_PREFIX"] = kwargs["prefix"] | ||
| kwargs.pop("prefix") |
There was a problem hiding this comment.
This change alters how LOCALTILESERVER_CLIENT_PREFIX is inferred/overridden (including JupyterHub JUPYTERHUB_SERVICE_PREFIX normalization and honoring pre-set values). There are existing unit tests for leafmap.common (e.g., tests/test_common.py), but none cover this prefix-selection behavior; adding focused tests for the env-var combinations would help prevent regressions (double-slash, missing-slash, and ensuring a pre-set prefix isn’t overwritten).
Since
JUPYTERHUB_SERVICE_PREFIXincludes a trailing slash,LOCALTILESERVER_CLIENT_PREFIXincluded a double slash in the URL, causing localtileserver requests for JupyterHub to break.In addition, the current code made it impossible to override
LOCALTILESERVER_CLIENT_PREFIXif any of the other conditions matched. Now, it will only override if it's unset.