简体   繁体   English

如何在 Jupyter 中本地引用 JavaScript 库?

[英]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.

相关问题 如何在没有服务器的情况下在本地使用 Javascript 库? - How do I use a Javascript library locally without a server? 如何为javascript库创建参考? - How can I create a reference for javascript library? 如何在本地捆绑 JavaScript 和 CSS 依赖项以供离线使用? - How do I bundle JavaScript and CSS dependencies locally for offline use? 如何在 Linux Mint 本地服务我的 javascript 应用程序 - How do I serve my javascript app locally on Linux Mint 如何使用高效的 Javascript 在本地存储多个用户更改? - How do I store multiple user changes locally with efficient Javascript? Javascript不能在Heroku上工作但在本地工作,我该如何解决? - Javascript is not working on Heroku but works locally, how do I solve this? Dynamics CRM 2015:如何获得创建的Web资源按钮以引用添加到表单的Javascript库中的函数? - Dynamics CRM 2015: How do I get a Web Resource button I created to reference a function in a Javascript Library I added to the form? 我如何引用javascript文件? - How do I reference a javascript file? 如何通过JavaScript传递引用? - How do I pass a reference in JavaScript? 如何在JavaScript中引用一行代码? - How do I reference a line of code in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM