Skip to content

Commit f983518

Browse files
authored
Merge pull request #2792 from Bobgy/load_cdn_directly
Use data-jupyter-widgets-cdn-only attribute to load modules only from CDN
2 parents 3d31ff3 + b568375 commit f983518

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

docs/source/embedding.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ html_template = """
124124
<!-- Load IPywidgets bundle for embedding. -->
125125
<script
126126
data-jupyter-widgets-cdn="https://cdn.jsdelivr.net/npm/"
127+
data-jupyter-widgets-cdn-only
127128
src="https://unpkg.com/@jupyter-widgets/html-manager@*/dist/embed-amd.js"
128129
crossorigin="anonymous">
129130
</script>
@@ -181,6 +182,9 @@ documents, you may want to use a templating engine like
181182
We also change the CDN from its default of unpkg to use jsdelivr by setting the
182183
`data-jupyter-widgets-cdn` attribute.
183184

185+
What's more, we only load modules from the CDN by setting the
186+
`data-jupyter-widgets-cdn-only` attribute.
187+
184188
In all embedding functions in `ipywidgets.embed`, the state of all widgets
185189
known to the widget manager is included by default. You can alternatively
186190
pass a reduced state to use instead. This can be particularly relevant if you
@@ -270,6 +274,11 @@ While the default CDN is using https://unpkg.com it can be configured by
270274
setting the optional `data-jupyter-widgets-cdn` attribute for script tag which loads `embed-amd.js`,
271275
as shown in the example above.
272276

277+
While the default strategy is loading modules from the same site, and then
278+
falling back to CDN. This can be configured by setting the optional
279+
`data-jupyter-widgets-cdn-only` attribute for script tag which loads `embed-amd.js`
280+
as shown in the example above too.
281+
273282
The [widget-cookiecutter](https://github.com/jupyter/widget-cookiecutter)
274283
template project contains a template project for a custom widget library
275284
following the best practices for authoring widgets, which ensure that your

packages/html-manager/src/libembed-amd.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import * as libembed from './libembed';
55

66
let cdn = 'https://unpkg.com/';
7+
let onlyCDN = false;
78

89
// find the data-cdn for any script tag, assuming it is only used for embed-amd.js
910
const scripts = document.getElementsByTagName('script');
1011
Array.prototype.forEach.call(scripts, (script: HTMLScriptElement) => {
1112
cdn = script.getAttribute('data-jupyter-widgets-cdn') || cdn;
13+
onlyCDN =
14+
onlyCDN || script.hasAttribute('data-jupyter-widgets-cdn-only');
1215
});
1316

1417
/**
@@ -62,22 +65,28 @@ export function requireLoader(
6265
moduleName: string,
6366
moduleVersion: string
6467
): Promise<any> {
68+
const require = (window as any).requirejs;
69+
if (require === undefined) {
70+
throw new Error(
71+
'Requirejs is needed, please ensure it is loaded on the page.'
72+
);
73+
}
74+
function loadFromCDN(): Promise<any> {
75+
const conf: { paths: { [key: string]: string } } = { paths: {} };
76+
conf.paths[moduleName] = moduleNameToCDNUrl(moduleName, moduleVersion);
77+
require.config(conf);
78+
return requirePromise([`${moduleName}`]);
79+
}
80+
if (onlyCDN) {
81+
console.log(`Loading from ${cdn} for ${moduleName}@${moduleVersion}`);
82+
return loadFromCDN();
83+
}
6584
return requirePromise([`${moduleName}`]).catch(err => {
6685
const failedId = err.requireModules && err.requireModules[0];
6786
if (failedId) {
68-
console.log(`Falling back to ${cdn} for ${moduleName}@${moduleVersion}`);
69-
const require = (window as any).requirejs;
70-
if (require === undefined) {
71-
throw new Error(
72-
'Requirejs is needed, please ensure it is loaded on the page.'
73-
);
74-
}
75-
const conf: { paths: { [key: string]: string } } = { paths: {} };
76-
conf.paths[moduleName] = moduleNameToCDNUrl(moduleName, moduleVersion);
7787
require.undef(failedId);
78-
require.config(conf);
79-
80-
return requirePromise([`${moduleName}`]);
88+
console.log(`Falling back to ${cdn} for ${moduleName}@${moduleVersion}`);
89+
loadFromCDN();
8190
}
8291
});
8392
}

0 commit comments

Comments
 (0)