在 ipython 个笔记本中导入 javascript 个文件以创建自定义小部件

import javascript files in ipython notebook to create a custom widget

我是 "ipython notebook" 的新手,我想使用 html 和 javascript 创建一个带有 ipywidgets 的自定义小部件。我正在使用 javascript 库,我想知道是否有办法将它导入 ipython notebook。

提前谢谢你

我找到了解决方案,您只需要将文件放在机器的某个位置,然后像这样使用魔法单元 HTML:

%%HTML
<script src="path-to-your-file"></script>

您可能需要查看 custom.js 文件,它可以在 Users\username\.ipython\profile_default\static\custom(或 jupyter,具体取决于安装的版本)中找到。您可以在此处添加指向您的 js 文件的链接,它们会在您每次打开笔记本时 运行。

您还可以查看笔记本扩展(例如 these

导入 javascript 文件以在自定义小部件中使用的方法是使用 require.config 将脚本加载到小部件中。例如,如果您想从 https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.7.0/fabric.min.js 加载 fabric.js,您将执行以下操作(请注意,您不要在路径中包含“.js”!)

%%javascript
require.undef('hello');
require.config({
  //Define 3rd party plugins dependencies
  paths: {
    fabric: "https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.7.0/fabric.min"
  }
});
define('hello', ["@jupyter-widgets/base", 'fabric'], function(widgets) {...