在页面加载时评估具有所需javascript文件的Jupyter输入单元格 [英] Jupyter input cell with required javascript file are evaluated at page load

查看:157
本文介绍了在页面加载时评估具有所需javascript文件的Jupyter输入单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jupyter笔记本开发用于数据可视化的前端Web。
这样做,b / c我想在python中开发数据分析程序,并能够使用最少的工具将它们推送到Web上。
作为第一步,我基本上遵循了这个发布并能够在笔记本中开发我的可视化。

I'am using the jupyter notebook to develop a front-end web for data visualization. Doing so, b/c i'd like to develop data-analysis procedures in python and be able to push them on the web using a minimum number of tools. As a first step, i basically followed this post and was able to develop my visualization in the notebook.

现在,我想把我为剧情渲染编写的javascript代码(主要是D3) )在一个单独的js文件(customPlot.js)的jupyter的输入单元格中,并能够在我的下一个笔记本中调用它。

Now, I would like to put the javascript code i wrote for plot rendering (mostly D3) in the input cells of jupyter in a separate js file ("customPlot.js") and be able to call it in my next notebook.

我有以下设置:


  • 禁用浏览器(chrome)缓存

  • browser (chrome) cache disabled

custom.js文件:

A custom.js file :

包含声明和记录内容

requirejs.config({
    paths: {
        d3: 'd3.v3.min',
        customPlot : 'customPlot'
    }
});

requirejs.onResourceLoad = function(context, map, depArray) {
    var duration = new Date() - start;
    console.log("onResourceLoad", duration + "ms", map.id);
}
start = +new Date();




  • customPlot.js 文件作为静态内容到笔记本服务器。

    • A customPlot.js file served as static content to the notebook server.
    • 我用<设置文件 .jupyter / jupyter_notebook_config.py / p>

      I set up the file .jupyter/jupyter_notebook_config.py with

      c.NotebookApp.extra_static_paths = ["/path/to/mylib/js"]  
      

      customPlot.js 的内容非常简单

      define(function (require, exports, module) {
      
          module.exports = {
              plot2D : function (div, svg, fn_htmtl) {alert('say hi');}
          };
      });
      




      • 一个jupyter笔记本:

      • 带有用于评估javascript的单元格。

        with a cell used to evaluate javascript.

        %%javascript
        require(['d3', 'customPlot'], function(d3, customPlot){
        // plot rendering code here
             customPlot.plot2D();
        });
        

        首次评估单元格时,它可以正常工作。
        但是如果重新加载笔记本页面,一旦页面重新加载,我甚至在评估单元格之前就会评估customPlot.plot2D()命令。

        如果我注释掉customPlot.plot2D()的调用,它仍然会在下一个笔记本页面重新加载时调用,令人惊讶!

        现在,浏览notebook.ipynb文件,我偶然发现了已评估单元格的内容历史悠久,其中 customPlot.plot2D 未被评论。

        AFAIU,在我当前设置下 %% javascript <的先前内容/ em>行在页面重新加载时执行(我们显然不希望这样,因为控制台将显示过时的日志或函数将使用未定义的参数调用)。我是否错过了设置/配置点,或者您是否看到任何方法来规避这种行为?

        我当然错过了一点,因为d3.js似乎没有在页面重新加载时进行评估。

        The first time the cell is evaluated it works fine. But if the notebook page is reloaded the "customPlot.plot2D()" command is evaluated as soon as the page is reloaded before i even evaluate the cell.
        If i comment out the call to "customPlot.plot2D()", it will still be called at then next notebook page reload, surprising !
        Now, browsing through the notebook.ipynb file, i stumbled on a long historic of the content of the evaluated cell, among which customPlot.plot2D was not commented.
        AFAIU, under my current settings the previous contents of %%javascript lines are executed at page reload (we obviously dont want that, as the console will display obsolete logs or function will be called with undefined arguments). Did i miss a setting/configuration point or do you see any way to circumvent this behavior ?
        I certainly missed a point as d3.js does not seem to be evaluated at page reload.

        感谢您抽出宝贵时间阅读我的问题。

        Thank you for taking the time to read my problem.

        推荐答案

        是自定义。 js文件你提到jupyter中的默认值或者你个人提供的那个?

        Is the custom.js file you are mentioning the default one from jupyter or a personal one you made?

        你提到的代码可以放在单独的.js文件中,可以作为一个例如,笔记本扩展。

        The code you are mentioning could be put in separate .js file that can work as a notebook extension for example.

        每次重新加载/打开笔记本时都会执行JavaScript,除了函数之外,它将遍历所有代码。现在在一个单独的js文件中存在加载/重新加载的相同问题,但有更多的解决方法:

        The JavaScript will be executed each time you reload/open a notebook and will go through all the code except in functions. Now inside a separate js file there are the same problems of loading/reloading but there are more workaround possibilities:


        • 一种可能性是拥有一切在函数中,当按下选项或按钮进行某些操作时调用它们。

        • 另一种可能性是在回调函数(promise)中有所需的部分,这样它也可以链接到某个单元格的执行。

        • 还有一种可能性是添加可以作为标志的元数据来检查代码是否先前已经加载(虽然重新启动内核时不会很好)

        这篇关于在页面加载时评估具有所需javascript文件的Jupyter输入单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆