如何为 Jupyter notebook 扩展加载 ES6 模块?

How to load ES6 module for Jupyter notebook extension?

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

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

添加了脚本标签,但我没能用正确的 mime 类型设置 scr 路径 correctly/provide 我的文件。

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

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

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

对于这两种情况,我得到

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 路径,其中文件以允许 ES6 模块所需的 mime 类型提供?也许像

<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

我用 Jupyter Notebook 版本 5.7.8 试过了,现在可以加载 ES6 模块了。