我想在Jupyter Lab/JupyterLab中展示一个d3.js可视化。我一直在关注各种网络教程,比如this one,它们通常都是从
%%javascript
require.config({
paths: {
d3: 'https://d3js.org/d3.v5.min'
}
});这在Jupyter Notebook中有效,但在Jupyter Lab中我得到:
Javascript Error: require is not defined如何在Jupyter Lab中显示d3.js可视化?
发布于 2021-02-05 16:34:33
此功能是由Jupyter Lab javascript扩展(默认安装)添加的。您可以看到一个演示笔记本here。
相关比特如下。要添加d3.js,您需要:
%%javascript
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.js';
document.head.appendChild(script);
console.log(window.d3)然后,下面的代码应该会起作用:
from IPython.display import Javascript
svg_script = '''
var svg = d3.select(element)
.append("svg")
.attr("width", 300)
.attr("height", 300);
svg.append("circle")
.style("stroke", "gray")
.style("fill", "cyan")
.attr("r", 130)
.attr("cx", 150)
.attr("cy", 150)
.transition()
.delay(100)
.duration(10000)
.attr("r", 10)
.attr("cx", 150)
.style("fill", "blue");
'''
Javascript(svg_script)https://stackoverflow.com/questions/66060012
复制相似问题