Jupyter Notebooks: How do I execute only cells that have a particular tag

krishnab

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.

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.

krishnab

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.

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()"))

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.

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

And that should be it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cells in Jupyter notebooks

How to have interactivity of Jupyter Notebooks online?

How can I display the version of my Jupyter notebook and run cells in Jupyter notebooks? I get an error: bad interpreter

Do I need internet to use jupyter notebooks?

How do I download source code from particular repo with only files that have been changed or added during a particular commit in that repo?

Using XPath, how do I select only the links that have a particular child present under the links sibling element

Jupyter notebooks in pycharm do not have Local History support in Pycharm?

How do I execute a php switch case with ajax onclick and have it print the result in an H tag?

How do I import module in jupyter notebook directory into notebooks in lower directories?

How do I cron an ipython jupyter notebook's cells?

How do I execute functions in a particular order when defined in a class?

How could I have a form tag execute 2 actions?

How to input a formular for this particular situation, I Have No Idea how to do this

How do you debug imported Jupyter notebooks in VS Code?

How do I target the first child that does not have a particular class?

How can I share Jupyter notebooks with non-programmers?

How can I configure indentation size in my Jupyter notebooks?

Can I specify that a particular tag always have a particular class?

How do I configure mathjax for iPython notebooks?

How do I make the eventListener to listen only for that particular element?

How do I enable mouse only for a particular split screen in VIM?

How do I search in Windows using the wildcard but only in the particular folder?

How Do I Clear Any Cells That Have Certain Text Strings?

How To Set Sources For Jupyter Notebooks

How do I get this formula to fill in only blank cells?

How do i Subtract only 2 cells in excel IF they are not blank

How can I fix this regex in order to get html tag only from a particular url?

How to select users having only particular tag not another tag

How can I do element into anchor tag dont have to clickable?