Jupyter Notebooks:如何仅执行具有特定标签的单元格 [英] Jupyter Notebooks: How do I execute only cells that have a particular tag

查看:64
本文介绍了Jupyter Notebooks:如何仅执行具有特定标签的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小部件,该小部件将仅执行笔记本中的某些单元格——以根据更改的数据生成新图.而 Jupyter 可以执行诸如执行笔记本中的所有单元格,或执行当前单元格下的所有单元格等操作.

I am creating a widget that will execute only certain cells in the notebook--to generate new plots based upon changed data. While Jupyter can do things like execute all cells in the notebook, or execute all cells below the current cell, etc.

相反,我想为要选择性执行的单元格创建标签.然后当我按下按钮时,我可以执行此代码并运行单元格.

Instead, I wanted to create tags for the cells that I want to selectively execute. Then when I push the button I can execute this code and run the cells.

小部件代码本身并不重要——那部分很简单.更难的是弄清楚如何在笔记本内实现python和javascript之间的通信.谁有想法.

The widget code itself is not important--that part is easy. The harder thing is to figure out how to implement the communication between python and javascript within the notebook. Anyone have any ideas.

推荐答案

所以我想通了.我认为其他人一直在寻找与此类似的答案,所以我将其发布.

So I figured this out. I think other folks have been looking for a answer similar to this, so I am posting it.

首先,我必须标记笔记本中的相关单元格.您可以通过笔记本中的视图"和单元格工具栏"来完成此操作.

First, I had to tag the relevant cells in the notebook. You can do that through the "View", and then "Cell Toolbar" in the notebook.

其次,使用这些函数创建一个代码单元.第一个函数会得到具有特定标签的所有单元格.第二个单元格将运行所有具有那个标签.就我而言,标记名是plotcell",但这可以更改为任何内容.在尝试使用这个函数之前,这个单元需要运行,所以我通常把它作为一个初始化单元.

Second, create a code cell with these functions. The first function will get all cells that have a certain tag. The second cell will run all cells that have that tag. In my case, the tagname was 'plotcell', but this can be changed to anything. This cell needs to run before trying to use this function, so I usually make it an initialization cell.

%%javascript
window.findCellIndicesByTag = function findCellIndicesByTag(tagName) {
  return (Jupyter.notebook.get_cells()
    .filter(
      ({metadata: {tags}}) => tags && tags.includes(tagName)
    )
    .map((cell) => Jupyter.notebook.find_cell_index(cell))
  );
};

window.runPlotCells = function runPlotCells() {
    var c = window.findCellIndicesByTag('plotcell');
    Jupyter.notebook.execute_cells(c);
};

最后,要运行此代码,您需要使用以下代码:

Finally, to run this code, you need to use the following code:

from IPython.core.display import Javascript
from IPython.display import display

def runPlotCells():
    display(Javascript("window.runPlotCells()"))

此代码将执行先前创建的 Javascript 函数以运行 PlotCells.我使用了 display 函数,以便笔记本中的输出被抑制,结果在浏览器开发者控制台中可见.

This code will execute the previously created Javascript function to runPlotCells. I used the display function so that the output in the notebook is suppressed and the results are visible in the browser developer console.

然后在按钮的 on_click 事件之后运行此函数.

You then run this function after the on_click event from the button.

应该就是这样.

这篇关于Jupyter Notebooks:如何仅执行具有特定标签的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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