无法将 d3js 运行到网站(jupyter notebook)

Shi*_*nai 3 html javascript d3.js jupyter-notebook

我对 html、d3js 或 javascript 不太了解,但根据 Udacity 的一些课程,我可以将 d3js 加载到任何网站,并在 Web 开发人员控制台中输入此代码:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://d3js.org/d3.v4.min.js';
document.head.appendChild(script);
Run Code Online (Sandbox Code Playgroud)

看起来很简单,除了我的 jupyter notebook 之外,它适用于我尝试过的每一页。该脚本被附加在<head>标签中,但它的不透明度非常低,当我尝试运行一些 d3js 代码时它不起作用(运行d3.select("p");给出ReferenceError: d3 is not defined)。还有其他解决方法吗?

bri*_*ian 5

这是因为您需要将 d3 设为全局。我在一个单元格中所做的就是这个。Jupyter 已经加载了 require。

%%javascript
require.config({
    paths: {
        d3: "https://d3js.org/d3.v4.min"
     }
});

require(["d3"], function(d3) {
    window.d3 = d3;
});
Run Code Online (Sandbox Code Playgroud)