How to auto-update jupyter notebook extension during development/debugging?

狂风中的少年 提交于 2019-12-08 05:04:17

问题


I try to write a custom extension for the Jupyter Notebook, as described here: https://towardsdatascience.com/how-to-write-a-jupyter-notebook-extension-a63f9578a38c

I use the Chrome developer tools on Windows7 to inspect and edit the Javascript source code of my custom extension. I hoped that when pressing F5 to update the page, my altered source code would be immediately applied.

However, as stated in the above mentioned article, I have to run

jupyter-contrib-nbextensions.exe install

and restart the server to refresh the notebook extension and to see the effects of my code changes.

Doing so after every little change is quite annoying when playing around and developing/debugging extensions.

=>Is there some sort of development option for automatically updating/reloading the extension?


回答1:


As a work around I wrote an extension "Workspace module":

https://github.com/stefaneidelloth/treezjs/tree/master/jupyter_notebook_extension/workspace_module

The purpose of that extension is to import a file "workspace.js" if it exists in the notebook folder. The main.js of my extension is:

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

    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','workspace.js');    

        document.body.appendChild(moduleScript);
    }


    return {
        load_ipython_extension: load_ipython_extension       
    };

});

The workspace.js redirects to my actual extension:

import './treezjs/src/treezJupyterNotebook.js';

This way, I am able to adapted my code during development without a need to re-execute the install command.



来源:https://stackoverflow.com/questions/55474703/how-to-auto-update-jupyter-notebook-extension-during-development-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!