Skip to content

Commit 78db417

Browse files
committed
WIP: cdn fallbacks
1 parent cd044c9 commit 78db417

File tree

1 file changed

+82
-7
lines changed

1 file changed

+82
-7
lines changed

src/vitessce/widget.py

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,79 @@ def get_uid_str(uid):
156156
# lang: js
157157
ESM = """
158158
import { importWithMap } from 'https://unpkg.com/[email protected]';
159+
const successfulImportMap = {
160+
imports: {
161+
162+
},
163+
};
159164
const importMap = {
160165
imports: {
161166
"react": "https://esm.sh/[email protected]?dev",
162167
"react-dom": "https://esm.sh/[email protected]?dev",
163168
"react-dom/client": "https://esm.sh/[email protected]/client?dev",
164169
},
165170
};
171+
const fallbackImportMap = {
172+
imports: {
173+
//"react": "https://cdn.vitessce.io/[email protected]/index.js",
174+
//"react-dom": "https://cdn.vitessce.io/[email protected]/index.js",
175+
//"react-dom/client": "https://cdn.vitessce.io/[email protected]/es2022/client.mjs",
176+
177+
// Replace with version-specific URL below.
178+
"vitessce": "https://cdn.vitessce.io/[email protected]/dist/index.min.js",
179+
},
180+
};
181+
/*
182+
const fallbackDevImportMap = {
183+
imports: {
184+
"react": "https://cdn.vitessce.io/[email protected]/index_dev.js",
185+
"react-dom": "https://cdn.vitessce.io/[email protected]/index_dev.js",
186+
"react-dom/client": "https://cdn.vitessce.io/[email protected]/es2022/client.development.mjs",
187+
// Replace with version-specific URL below.
188+
"vitessce": "https://cdn.vitessce.io/@vitessce/[email protected]/dist/index.js",
189+
},
190+
};
191+
*/
192+
193+
async function importWithMapAndFallback(moduleName, importMap, fallbackMap) {
194+
console.log(`Importing ${moduleName} with import map`, importMap, fallbackMap);
195+
196+
let result = null;
197+
if (!fallbackMap) {
198+
// fallbackMap is null, user may have provided custom JS URL.
199+
result = await importWithMap(moduleName, {
200+
imports: {
201+
...importMap.imports,
202+
...successfulImportMap.imports,
203+
},
204+
});
205+
successfulImportMap.imports[moduleName] = importMap.imports[moduleName];
206+
} else {
207+
try {
208+
result = await importWithMap(moduleName, {
209+
imports: {
210+
...importMap.imports,
211+
...successfulImportMap.imports,
212+
},
213+
});
214+
successfulImportMap.imports[moduleName] = importMap.imports[moduleName];
215+
} catch (e) {
216+
console.warn(`Importing ${moduleName} failed with import map`, importMap, e);
217+
result = await importWithMap(moduleName, {
218+
imports: {
219+
...fallbackMap.imports,
220+
...successfulImportMap.imports,
221+
},
222+
});
223+
successfulImportMap.imports[moduleName] = fallbackMap.imports[moduleName];
224+
}
225+
}
226+
return result;
227+
}
166228
167-
const React = await importWithMap("react", importMap);
168-
const { createRoot } = await importWithMap("react-dom/client", importMap);
229+
230+
const React = await importWithMapAndFallback("react", importMap, fallbackImportMap);
231+
const { createRoot } = await importWithMapAndFallback("react-dom/client", importMap, fallbackImportMap);
169232
170233
const e = React.createElement;
171234
@@ -252,6 +315,7 @@ def get_uid_str(uid):
252315
}
253316
254317
async function render(view) {
318+
console.log("Rendering Vitessce widget...");
255319
const cssUid = view.model.get('uid');
256320
const jsDevMode = view.model.get('js_dev_mode');
257321
const jsPackageVersion = view.model.get('js_package_version');
@@ -268,10 +332,21 @@ def get_uid_str(uid):
268332
269333
const pkgName = (jsDevMode ? "@vitessce/dev" : "vitessce");
270334
271-
importMap.imports["vitessce"] = (customJsUrl.length > 0
335+
const hasCustomJsUrl = customJsUrl.length > 0;
336+
337+
importMap.imports["vitessce"] = (hasCustomJsUrl
272338
? customJsUrl
273339
: `https://unpkg.com/${pkgName}@${jsPackageVersion}`
274340
);
341+
let fallbackImportMapToUse = null;
342+
if (!hasCustomJsUrl) {
343+
fallbackImportMapToUse = fallbackImportMap;
344+
if (jsDevMode) {
345+
fallbackImportMapToUse.imports["vitessce"] = `https://cdn.vitessce.io/vitessce@${jsPackageVersion}/dist/index.min.js`;
346+
} else {
347+
fallbackImportMapToUse.imports["vitessce"] = `https://cdn.vitessce.io/@vitessce/dev@${jsPackageVersion}/dist/index.js`;
348+
}
349+
}
275350
276351
const {
277352
Vitessce,
@@ -292,7 +367,7 @@ def get_uid_str(uid):
292367
useComplexCoordinationSecondary,
293368
useCoordinationScopes,
294369
useCoordinationScopesBy,
295-
} = await importWithMap("vitessce", importMap);
370+
} = await importWithMapAndFallback("vitessce", importMap, fallbackImportMapToUse);
296371
297372
let pluginViewTypes = [];
298373
let pluginCoordinationTypes = [];
@@ -651,7 +726,7 @@ class VitessceWidget(anywidget.AnyWidget):
651726

652727
next_port = DEFAULT_PORT
653728

654-
js_package_version = Unicode('3.8.3').tag(sync=True)
729+
js_package_version = Unicode('3.8.5').tag(sync=True)
655730
js_dev_mode = Bool(False).tag(sync=True)
656731
custom_js_url = Unicode('').tag(sync=True)
657732
plugin_esm = List(trait=Unicode(''), default_value=[]).tag(sync=True)
@@ -664,7 +739,7 @@ class VitessceWidget(anywidget.AnyWidget):
664739

665740
store_urls = List(trait=Unicode(''), default_value=[]).tag(sync=True)
666741

667-
def __init__(self, config, height=600, theme='auto', uid=None, port=None, proxy=False, js_package_version='3.8.3', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, prefer_local=True, invoke_timeout=300000, invoke_batched=True, page_mode=False, page_esm=None, prevent_scroll=True, server_host=None):
742+
def __init__(self, config, height=600, theme='auto', uid=None, port=None, proxy=False, js_package_version='3.8.5', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, prefer_local=True, invoke_timeout=300000, invoke_batched=True, page_mode=False, page_esm=None, prevent_scroll=True, server_host=None):
668743
"""
669744
Construct a new Vitessce widget. Not intended to be instantiated directly; instead, use ``VitessceConfig.widget``.
670745
@@ -798,7 +873,7 @@ def _plugin_command(self, params, buffers):
798873
# Launch Vitessce using plain HTML representation (no ipywidgets)
799874

800875

801-
def ipython_display(config, height=600, theme='auto', base_url=None, host_name=None, uid=None, port=None, proxy=False, js_package_version='3.8.3', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, page_mode=False, page_esm=None, server_host=None):
876+
def ipython_display(config, height=600, theme='auto', base_url=None, host_name=None, uid=None, port=None, proxy=False, js_package_version='3.8.5', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, page_mode=False, page_esm=None, server_host=None):
802877
from IPython.display import display, HTML
803878
uid_str = "vitessce" + get_uid_str(uid)
804879

0 commit comments

Comments
 (0)