【问题标题】:How to load ES6 module for Jupyter notebook extension?如何为 Jupyter 笔记本扩展加载 ES6 模块?
【发布时间】:2019-04-09 00:46:46
【问题描述】:

我目前使用 Jupyter Notebook Server 5.7.0 版。我想编写一个 Jupyter 笔记本扩展,通过向文档添加脚本标签来加载一些 ES6 模块,例如:

<script type="module" scr='./es6module.js'>

脚本标签已添加,但我未能正确设置 scr 路径/为我的文件提供正确的 mime 类型。

上面的脚本标签在 notebook-dir 中查找 es6module.js 文件。

我还尝试在我的扩展文件夹中引用一个文件:

<script type="module" scr='/nbextensions/my_extension_folder/es6module.js'>

对于这两种情况,我都会得到 ​​p>

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

=>是否有一些 http 路径,其中文件以所需的 mime 类型提供以允许 ES6 模块?也许像

<script type="module" scr='/http/nbextensions/my_extension_folder/es6module.js'>

=>或者我应该尝试使用 Python 启动我自己的 http 服务器吗?

Jupyter.notebook.kernel.execute('http.server');

示例扩展代码:

define([
    'require',
    'jquery',
    'base/js/namespace'       
], function(
    requirejs,
    $,
    Jupyter       
) {

    var load_ipython_extension = function() {  
        if (Jupyter.notebook !== undefined && Jupyter.notebook._fully_loaded) {
            init();                    
        } else {
            console.log("[workspace_module] Waiting for notebook availability")
            events.on("notebook_loaded.Notebook", function() {
                init();                           
            })
        }
    };

    function init(){

        console.log("[workspace_module] trying to load workspace.js")

        var moduleScript = document.createElement('script');
        moduleScript.setAttribute('type','module'); 

        moduleScript.setAttribute('src','/nbextensions/my_extension_folder/es6module.js');  

        document.head.appendChild(moduleScript);
    }


    return {
        load_ipython_extension: load_ipython_extension       
    };

});

编辑

可能与

有关

https://github.com/jupyter/notebook/pull/4468

【问题讨论】:

    标签: javascript jupyter es6-modules jupyter-contrib-nbextensions


    【解决方案1】:

    我在 Jupyter Notebook 版本 5.7.8 上进行了尝试,现在可以加载 ES6 模块。

    【讨论】:

      猜你喜欢
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2021-02-01
      • 2019-09-17
      • 2020-11-22
      • 2021-04-24
      • 2021-03-03
      相关资源
      最近更新 更多