In this short post we will see how to open Notebook in JupyterLab without the output cells.

This is helpful in cases when the output of one or many cells is causing performance issues or is too big to load.

It can be used also if reopening the Notebook is causing a crash due to huge output. In order to restore your Notebook info only with code cells and preserve your code use the technique described in this post.

Step 1: Create a new notebook in the same folder

First you will need to create a new empty Notebook in the same folder as the one which will be restored. This can be done by:

  • File
  • New
  • Notebook

Let's try to open notebook test.ipynb without the output.

Step 2: Define script to clear cell outputs

Add new code cell with the next code:

from nbformat import read, write

def strip_output(nb):
    for cell in nb.cells:
        if hasattr(cell, "outputs"):
            cell.outputs = []
        if hasattr(cell, "prompt_number"):
            del cell["prompt_number"]

nb = read(open("test.ipynb"), 4)
write(nb, open("test_2.ipynb", "w"), 4)

Change the name of the notebook if needed - "test.ipynb" and the output one too - "test_2.ipynb"

Just run the cell and you will get a new notebook with the code cells without the outputs.

Note: It will restore the last saved information from your notebook.

Bonus 1: Download the notebook as .ipynb

Since all notebooks in JupyterLab and Jupyter Notebooks are essentially JSON files you can download the file without opening it.

Then you can parse the content and get only the code cells by a jq program as:

jq -j '
  | map( select(.cell_type == "code") | .source + ["\n\n"] )
  | .[][]
  ' \
  test.ipynb >

Bonus 2: Download the notebook as a script

Another useful option is to get code cells without the markdown cells by:

jupyter nbconvert --to script notebook_name.ipynb

or export the the content as a Python script by

  • File
  • Download as
  • Executable Script

Bonus 3: JupyterLab clear output and save

If you can open the notebook or it's not saved yet you can try to save or restart the notebook Kernel by:

  • Save - by pressing - CTRL + S

Or clearing output by:

  • File
  • Kernel
  • Restart Kernel and Clear All Outputs...