[英]How do I reference a JavaScript library locally in Jupyter?
I would like to reference the d3 JavaScript library in my Jupyter Notebook.我想在我的 Jupyter Notebook 中引用 d3 JavaScript 库。 However, the remote node that I am working on (in AWS) has no outbound HTTP access.
但是,我正在处理的远程节点(在 AWS 中)没有出站 HTTP 访问。 I figured I can download the
js
file, SCP it to the node, and reference it locally (from the node back to itself or the local file system).我想我可以下载
js
文件,将它 SCP 到节点,并在本地引用它(从节点回到自身或本地文件系统)。
I followed the tutorial here , and this SO post is somewhat getting towards what I want to do (though the OP is asking for something different).我遵循了这里的教程,这篇SO 帖子有点接近我想做的事情(尽管 OP 要求有所不同)。 The tutorial references d3 via HTTP.
本教程通过 HTTP 引用了 d3。
If I simply modify the code to reference a local d3 JS file as follows, the example no longer works.如果我简单地修改代码以引用本地 d3 JS 文件,则该示例不再有效。
%%javascript
require.config({
paths: {
d3: 'd3.min.js'
}
});
Observing the JavaScript console does not produce any errors.观察 JavaScript 控制台不会产生任何错误。
Any ideas on how reference local 3rd party JavaScript libraries in a Jupyter Notebook?关于如何在 Jupyter Notebook 中引用本地 3rd 方 JavaScript 库的任何想法?
Once downloaded locally, you can do: 本地下载后,您可以执行以下操作:
%%javascript
element.append('<div id="viz"></div>');
require(['d3.min.js'], function(d3){
var sampleSVG = d3.select("#viz")
.append("svg")
.attr("width", 100)
.attr("height", 100);
sampleSVG.append("circle")
.style("stroke", "gray")
.style("fill", "white")
.attr("r", 40)
.attr("cx", 50)
.attr("cy", 50)
.on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
.on("mouseout", function(){d3.select(this).style("fill", "white");});
});
Maybe you want to access a local javascript file by url in the jupyter notebook?也许您想通过 jupyter notebook 中的 url 访问本地 javascript 文件?
I found the '/files' path is a good workaround, use:我发现“/files”路径是一个很好的解决方法,请使用:
require.config({
paths: {
d3: '/files/js/d3.min'
}
});
if you save the local file under the 'js' directory of the root of the jupyter server.如果将本地文件保存在 jupyter 服务器根目录的 'js' 目录下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.