Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active February 22, 2024 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/1867d5b237853846c6355f3de62bfc47 to your computer and use it in GitHub Desktop.
Save fomightez/1867d5b237853846c6355f3de62bfc47 to your computer and use it in GitHub Desktop.
How to use nbformat to see a cell has been run - answer to SO https://stackoverflow.com/q/78038479/8508004

The code below scans an .ipynb file where the first two and fifth cells were run (named example_nb_saved_first_two_cells_run_plus_last.ipynb) and then makes a new notebook out of the executed cells:

import nbformat as nbf
ntbk = nbf.read("example_nb_saved_first_two_cells_run_plus_last.ipynb", nbf.NO_CONVERT)
cells_executed_previously = []
for cell in ntbk.cells:
    if cell.execution_count != None:
        cells_executed_previously.append(cell)
new_ntbk = ntbk
new_ntbk.cells = cells_executed_previously
nbf.write(new_ntbk, "new_notebook_of_only_executed_cells.ipynb", version=nbf.NO_CONVERT)

It uses nbformat and so it will run anywhere Jupyter is installed. The new notebook produced is called new_notebook_of_only_executed_cells.ipynb. What it is doing is seeing if the execution_count attribute of a cell is None (equivalent to null if you view the .ipynb in your favorite text editor) and skipping if that is the case. It collects all those that have been run. The key line is the conditional if cell.execution_count != None:.

The idea would be you have the input notebook alread in a Jupyter session and then in a new notebook, or as a script, you run the above code and then you'd have the output notebook produced.

You'll note in the output notebook file 'new_notebook_of_only_executed_cells.ipynb', only the first two and the last cells of the source are present. That is because those had been the only two executed in the source input notebook 'example_nb_saved_first_two_cells_run_plus_last.ipynb'.

I have made the notebook described as input and then executed the code above and included the result already; however, if you wanted to demonstrate that and/or build further on that click here to get a temporary Jupyter session served by the MyBinder service with the input notebook already present. You can edit it if you want, such as clearing out the run cells and re-running which ones you see fit and saving the notebook again in the active session. And then opening a new notebook and pasting in the code above and running it to make a new resulting outout notebook with only the cells executed in the original.

I've included the associated input and output notebooks in this gist for ease:

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment