代码之家  ›  专栏  ›  技术社区  ›  Stefan

如何为Jupyter笔记本扩展加载ES6模块?

  •  0
  • Stefan  · 技术社区  · 7 年前

    我目前使用的是Jupyter笔记本服务器5.7.0版。我想写一个Jupyter笔记本扩展,通过在文档中添加脚本标签加载一些ES6模块,例如:

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

    添加了脚本标记,但我没有正确设置scr路径/为我的文件提供正确的mime类型。

    上面的脚本标记查找 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路径,在该路径中,文件以所需的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

    0 回复  |  直到 7 年前
        1
  •  0
  •   Stefan    7 年前

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