本文介绍了D3 4.0+在导入Jupyter Notebook时不会创建全局d3变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容将因JavaScript ReferenceError 而失败:

 来自IPython.display导入HTML,显示

display(HTML($
< script src =https://d3js.org/d3.v4.js>< / script>

< script>
console.log(d3);
< / script>
))

为什么?



等效的D3版本3.x可以使用(虽然第二次尝试,对我来说):



<$ p来自IPython.display的$ p> 导入HTML,显示

display(HTML(
< script src =https://d3js.org/ d3.v3.js>< / script>

< script>
console.log(d3);
< / script>
))

是我在这个主题上可以找到的最相关的问题/答案。

解决方案

原来这是由于 d3 导出本身的方式发生了内部变化。 3.x分支版本的 d3 将所有内部导出为全局变量(); 4.x分支版本不再这样做()。现在预计会有一个包管理器(比如 require.js ),并且导出会通过管道输送到管理器。



进一步的详细信息请参见。



现在应该做的是:

 < script src =scripts / require.js >< /脚本> 
< script> var d3 = require('d3')< / script>

之后,一切都应按预期工作。


The following will fail with a JavaScript ReferenceError:

from IPython.display import HTML, display

display(HTML("""
<script src="https://d3js.org/d3.v4.js"></script>

<script>
console.log(d3);
</script>
"""))

Why is that?

The equivalent D3 version 3.x will work (albeit on the second try, for me):

from IPython.display import HTML, display

display(HTML("""
<script src="https://d3js.org/d3.v3.js"></script>

<script>
console.log(d3);
</script>
"""))

This is the most relevant question/answer I could find on this topic.

解决方案

This turned out to be due to an internal change to the way that d3 exports itself. 3.x branch versions of d3 exported all of their internals as global variables (source code); 4.x branch versions no longer do that (source code). A package manager (like require.js) is expected instead now, and exports are piped to that instead.

Further details are in the requisite GitHub issue.

What you should do instead now is something like:

<script src="scripts/require.js"></script>
<script>var d3 = require('d3')</script>

After that, everything should work as expected.

这篇关于D3 4.0+在导入Jupyter Notebook时不会创建全局d3变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:51