Skip to content

Commit 8aba183

Browse files
Use data-jupyter-widgets-cdn attribute to make the CDN configurable
This replaces jupyter-widgets#2185 which used a global variable.
1 parent 4d8a682 commit 8aba183

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

docs/source/embedding.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ html_template = """
122122
</script>
123123
124124
<!-- Load IPywidgets bundle for embedding. -->
125-
<script
125+
<script
126+
data-jupyter-widgets-cdn="https://cdn.jsdelivr.net/npm/"
126127
src="https://unpkg.com/@jupyter-widgets/html-manager@*/dist/embed-amd.js"
127128
crossorigin="anonymous">
128129
</script>
@@ -177,6 +178,9 @@ In this example, we used a Python string for the template, and used the
177178
documents, you may want to use a templating engine like
178179
[Jinja2](http://jinja.pocoo.org/).
179180

181+
We also change the CDN from its default of unpkg to use jsdelivr by setting the
182+
`data-jupyter-widgets-cdn` attribute.
183+
180184
In all embedding functions in `ipywidgets.embed`, the state of all widgets
181185
known to the widget manager is included by default. You can alternatively
182186
pass a reduced state to use instead. This can be particularly relevant if you
@@ -262,13 +266,17 @@ documentation. An illustration of this is the http://jupyter.org/widgets
262266
gallery.
263267

264268
The widget embedder attempts to fetch the model and view implementation of the
265-
custom widget from the npm CDN https://unpkg.com. The URL that is requested
269+
custom widget from the npm CDN https://unpkg.com by default. The URL that is requested
266270
for, e.g. the `bqplot` module name, with the semver range `^2.0.0` is
267271

268272
`https://unpkg.com/bqplot@^2.0.0/dist/index.js`
269273

270274
which holds the webpack bundle for the bqplot library.
271275

276+
While the default CDN is using https://unpkg.com it can be configured by
277+
setting the optional `data-jupyter-widgets-cdn` attribute for script tag which loads `embed-amd.js`,
278+
as shown in the example above.
279+
272280
The [widget-cookiecutter](https://github.com/jupyter/widget-cookiecutter)
273281
template project contains a template project for a custom widget library
274282
following the best practices for authoring widgets, which ensure that your

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4-
const __jupyter_widgets_cdn__ = (window as any).__jupyter_widgets_cdn__ || 'https://unpkg.com';
5-
64
import * as libembed from './libembed';
75

6+
let cdn = 'https://unpkg.com/';
7+
8+
// find the data-cdn for any script tag, assuming it is only used for embed-amd.js
9+
const scripts = document.getElementsByTagName('script');
10+
Array.prototype.forEach.call(scripts, (script) => {
11+
cdn = script.getAttribute('data-jupyter-widgets-cdn') || cdn;
12+
});
13+
814
/**
915
* Load a package using requirejs and return a promise
1016
*
@@ -37,15 +43,14 @@ function moduleNameToCDNUrl(moduleName: string, moduleVersion: string) {
3743
fileName = moduleName.substr(index+1);
3844
packageName = moduleName.substr(0, index);
3945
}
40-
return `${__jupyter_widgets_cdn__}/${packageName}@${moduleVersion}/dist/${fileName}`;
46+
return `${cdn}${packageName}@${moduleVersion}/dist/${fileName}`;
4147
}
4248

4349
function requireLoader(moduleName: string, moduleVersion: string) {
4450
return requirePromise([`${moduleName}`]).catch((err) => {
4551
let failedId = err.requireModules && err.requireModules[0];
4652
if (failedId) {
47-
console.log(`Falling back to ${__jupyter_widgets_cdn__} for ` +
48-
`${moduleName}@${moduleVersion}`);
53+
console.log(`Falling back to ${cdn} for ${moduleName}@${moduleVersion}`);
4954
let require = (window as any).requirejs;
5055
if (require === undefined) {
5156
throw new Error("Requirejs is needed, please ensure it is loaded on the page.");

0 commit comments

Comments
 (0)