Skip to content

Commit 8650b30

Browse files
tkakarkeller-mark
andauthored
Tkakar/cat 1261 fix issue 419 (#420)
* Updated prependbaseUrl * Adjusted baseURL for local files * Linting * Update url modification function * Revert usage of current_port * Lint --------- Co-authored-by: Mark Keller <[email protected]>
1 parent 560c63d commit 8650b30

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/vitessce/widget.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib.util
22
from urllib.parse import quote_plus
33
import json
4+
import sys
45

56
# Widget dependencies
67
import anywidget
@@ -104,7 +105,8 @@ def get_base_url_and_port(port, next_port, proxy=False, base_url=None, host_name
104105

105106
if base_url is None:
106107
if proxy:
107-
if importlib.util.find_spec('jupyter_server_proxy') is None:
108+
is_in_workspaces = sys.executable.startswith('/hive')
109+
if importlib.util.find_spec('jupyter_server_proxy') is None and not is_in_workspaces:
108110
raise ValueError(
109111
"To use the widget through a proxy, jupyter-server-proxy must be installed.")
110112
if host_name is None:
@@ -161,37 +163,52 @@ def get_uid_str(uid):
161163
const { createRoot } = await importWithMap("react-dom/client", importMap);
162164
163165
const e = React.createElement;
164-
166+
const WORKSPACES_URL_KEYWORD = 'https://workspaces-pt'
165167
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
166-
167168
// The jupyter server may be running through a proxy,
168169
// which means that the client needs to prepend the part of the URL before /proxy/8000 such as
169170
// https://hub.gke2.mybinder.org/user/vitessce-vitessce-python-swi31vcv/proxy/8000/A/0/cells
171+
// For workspaces: https://workspaces-pt.hubmapconsortium.org/passthrough/HOSTNAME/PORT/ADDITIONAL_PATH_INFO?QUERY_PARAMS=HELLO_WORLD
170172
function prependBaseUrl(config, proxy, hasHostName) {
171-
if(!proxy || hasHostName) {
172-
return config;
173-
}
174-
const { origin } = new URL(window.location.href);
175-
let baseUrl;
176-
const jupyterLabConfigEl = document.getElementById('jupyter-config-data');
177-
178-
if (jupyterLabConfigEl) {
179-
// This is jupyter lab
180-
baseUrl = JSON.parse(jupyterLabConfigEl.textContent || '').baseUrl;
181-
} else {
182-
// This is jupyter notebook
183-
baseUrl = document.getElementsByTagName('body')[0].getAttribute('data-base-url');
184-
}
185-
return {
186-
...config,
187-
datasets: config.datasets.map(d => ({
188-
...d,
189-
files: d.files.map(f => ({
190-
...f,
191-
url: `${origin}${baseUrl}${f.url}`,
192-
})),
193-
})),
194-
};
173+
if (!proxy || hasHostName) {
174+
return config;
175+
}
176+
const { origin, pathname } = new URL(window.location.href);
177+
const isInWorkspaces = origin.startsWith(WORKSPACES_URL_KEYWORD);
178+
179+
const jupyterLabConfigEl = document.getElementById('jupyter-config-data');
180+
181+
let baseUrl;
182+
if (isInWorkspaces) {
183+
const pathSegments = pathname.split('/');
184+
const passthroughIndex = pathSegments.indexOf('passthrough');
185+
if (passthroughIndex !== -1) {
186+
baseUrl = pathSegments.slice(0, passthroughIndex + 2).join('/');
187+
}
188+
} else if (jupyterLabConfigEl) {
189+
// This is jupyter lab
190+
baseUrl = JSON.parse(jupyterLabConfigEl.textContent || '').baseUrl;
191+
} else {
192+
// This is jupyter notebook
193+
baseUrl = document.getElementsByTagName('body')[0].getAttribute('data-base-url');
194+
}
195+
return {
196+
...config,
197+
datasets: config.datasets.map(d => ({
198+
...d,
199+
files: d.files.map(f => {
200+
if (isInWorkspaces && f.url.startsWith('proxy')) {
201+
// When the user is in workspaces, we do not use jupyter_server_proxy.
202+
// Instead, the workspaces infrastructure has its own "passthrough" functionality.
203+
f.url = f.url.replace('proxy', '');
204+
}
205+
return {
206+
...f,
207+
url: `${origin}${baseUrl}${f.url}`,
208+
};
209+
}),
210+
})),
211+
};
195212
}
196213
197214
async function render(view) {

0 commit comments

Comments
 (0)