【问题标题】:IPython Notebook: programmatically trigger a cell from JavaScriptIPython Notebook:以编程方式从 JavaScript 触发单元格
【发布时间】:2014-01-31 00:44:35
【问题描述】:

所以,这几天我一直在玩 IPython 笔记本,我喜欢它!但是,现在我需要做一些有点花哨的事情:

我有一个降价单元格;在其中,有一个 HTML 输入和按钮,以及一些附加到按钮的 JavaScript,它将获取输入的内容,并将其注入 python 内核。这是单元格:

<h3>Use JS to pass DOM info to Python kernel: </h3>
<input id='testinput' value='FragmentId'></input>
<button id='testComms' onclick='JS2PY()'>:D</button>

<script type="text/javascript">    
    function JS2PY(){
        var input = document.getElementById('testinput').value,
            kernel = IPython.notebook.kernel;

        kernel.execute('testVar = "' + input + '"');
    }
</script>

像魅力一样工作!接下来我有一个 python 代码单元;它做了一些 ROOT 的东西,并根据从上述单元注入到 python 内核的任何值绘制一个图。这是python单元:

def testfunc():
    from ROOT import TH1, TFile, TTree
    import rootnotes, numpy

    c2 = rootnotes.canvas("treeData", (600,400))

    testfile = TFile("fragment27422_000.root")
    testtree = testfile.Get("FragmentTree")

    buff = numpy.zeros(1, dtype=float)

    testtree.Branch(testVar, buff)

    testtree.Draw(testVar)
    return c2

testfunc()

如果我手动运行单元格也没有问题 - 太棒了!但我真正想要的是,当我在提升testVar 变量之后单击上面markdown 单元格中的那个按钮时,这个python 单元格会自动运行。提前道歉和感谢 - 这只是我使用 Python 的第二天,所以它可能非常简单。

【问题讨论】:

    标签: ipython-notebook


    【解决方案1】:

    解决方案/解决方法:我们可以调用其他单元格中定义的 python 函数,然后在 JavaScript 和 python 内核之间进行一次回调,然后通过IPython.notebook.kernel.execute 进行回调,而不是直接触发其他单元格;类似这样的代码单元:

    %%HTML
    
    <div id='testwrap'>
    <input id='varname'></input>
    <img id='imgtarget'></img>
    <button id='fetchplot' onclick='exec_code()'>Plot</button>
    </div>
    
    <script type="text/Javascript">
        function handle_output(out_type, out){
            document.getElementById('imgtarget').src = 'data:image/png;base64,' + out.data['image/png'];
        }
    
        function exec_code(){
            var kernel = IPython.notebook.kernel;
            var callbacks = {'output' : handle_output};
            kernel.execute('testVar = "' + document.getElementById('varname').value + '"');
            kernel.execute('testfunc(testVar)', callbacks, {silent:false});
        }
    </script>
    

    第一个 kernel.execute 将一些数据从 DOM 踢到内核,第二个使用回调在 JS 中使用 python 函数 testfunc(在其他单元格中定义)返回的任何内容。

    感谢 http://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/ 了解此解决方案的核心内容!

    【讨论】:

      【解决方案2】:

      第一个有趣的笔记本项目(现在帖子有点老了)。如今,可以像这样使用IPython.notebook.execute_cell(..)

      %%HTML
      
      <h3>Select an Experiment</h3>
      <input id='input' value='my input var'></input>
      <button id='testComms' onclick='JS2PY()'>:D</button>
      
      <script type="text/javascript">    
          function JS2PY() {
              const { value } = document.getElementById('input');
              const pycode = `testVar = '${value}'`;
              const { notebook } = IPython;
              const { kernel } = notebook;
              kernel.execute(pycode);
              // run cell using `testVar`
              notebook.select_next();
              notebook.execute_cell();
          }
      </script>
      

      在上面的示例中,下一个笔记本单元格应包含您要执行的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 2014-07-23
        相关资源
        最近更新 更多