Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Allow setting URL prefix for data_server_proxied
Browse files Browse the repository at this point in the history
Suggests a fix for #40 and documents how to best use data_server_proxied
with JupyterHub.
  • Loading branch information
dlukes committed Apr 12, 2020
1 parent 62e5438 commit ee9bbac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -53,6 +53,32 @@ installed on the jupyter server, and use the proxied data server transformer:
alt.data_transformers.enable('data_server_proxied')
```

The `urlpath` parameter allows you to override the prefix of the proxy URL. By
default, it's set to `..`, which is currently the only way to make it work for
arbitrary users when running inside the classic notebook on Binder. If you
intend your notebooks to be run on Binder but inside JupyterLab, change it to
`.` instead, which will work provided JupyterLab is in the [default
workspace](https://jupyterlab.readthedocs.io/en/stable/user/urls.html#managing-workspaces-ui).

```python
# for notebooks intended for JupyterLab on Binder
alt.data_transformers.enable('data_server_proxied', urlpath='.')
```

On a custom JupyterHub instance, a much more robust option is to take advantage
of JupyterHub's [`/user-redirect`](https://jupyterhub.readthedocs.io/en/stable/reference/urls.html#user-redirect)
feature (which is not available on Binder):

```python
# this will work for any JupyterHub user, whether they're using the classic
# notebook, JupyterLab in the default workspace, or JupyterLab in a named
# workspace
alt.data_transformers.enable('data_server_proxied', urlpath='/user-redirect')
```

If your JupyterHub lives somewhere else than at your server's root, add the
appropriate prefix to `urlpath`.

## Example

[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/altair-viz/altair_data_server/master?urlpath=lab/tree/AltairDataServer.ipynb)
Expand Down
9 changes: 7 additions & 2 deletions altair_data_server/_altair_server.py
Expand Up @@ -56,13 +56,18 @@ def __call__(

class AltairDataServerProxied(AltairDataServer):
def __call__(
self, data: pd.DataFrame, fmt: str = "json", port: Optional[int] = None
self,
data: pd.DataFrame,
fmt: str = "json",
port: Optional[int] = None,
urlpath: str = "..",
) -> Dict[str, str]:
result = super().__call__(data, fmt=fmt, port=port)

url_parts = parse.urlparse(result["url"])
urlpath = urlpath.rstrip("/")
# vega defaults to <base>/files, redirect it to <base>/proxy/<port>/<file>
result["url"] = f"../proxy/{url_parts.port}{url_parts.path}"
result["url"] = f"{urlpath}/proxy/{url_parts.port}{url_parts.path}"

return result

Expand Down

0 comments on commit ee9bbac

Please sign in to comment.