How to Collapse Cells in Jupyter Notebook & Jupyter Lab

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# Table of Contents

  1. How to Collapse cells in Jupyter Notebook & Jupyter Lab
  2. Collapsing cells in Jupyter Lab out of the box
  3. Collapsing a cell via the Command Palette in Jupyter Lab
  4. Collapsible headings in Jupyter Lab
  5. How to Collapse cells in Jupyter Notebook using nbextensions

# How to Collapse Cells in Jupyter Notebook & Jupyter Lab

There are multiple ways to collapse cells in Jupyter Notebook:

  1. You can use the Jupyter nbextension package.
  2. The collapsing cells functionality is available out of the box in Jupyter Lab.

# Collapsing cells in Jupyter Lab out of the box

You can collapse cells in Jupyter lab out of the box.

First, make sure that you have jupyterlab installed.

shell
pip install jupyterlab # Or with pip3 pip3 install jupyterlab

Now, start Jupyter lab by issuing the following command.

shell
jupyter lab # or jupyter-lab

You can click on the blue line to the left of a cell to collapse it.

Here is the code of the cell in the example.

main.py
print('bobbyhadz.com') print('example.com') print('google.com')

And here is a short clip that demonstrates how collapsing cells works.

collapse cells in jupyter lab

You simply click on the blue line to the left of the cell to collapse it.

You can then expand the cell by clicking on the shorter blue line or on the three dots that appear in the place of the cell when it's collapsed.

# Collapsing a cell via the Command Palette in Jupyter Lab

If you use Jupyter Lab, you can also collapse a cell via the Command Palette.

You can click on View and then Activate Command Palette.

Or you can press Ctrl + Shift + C to open the Command Palette.

Then type Collapse and view the available options.

type collapse in command palette

You can:

  1. Collapse all code.
  2. Collapse all outputs.
  3. Collapse selected code.
  4. Collapse selected outputs.
  5. Collapse all cells.

# Collapsible headings in Jupyter Lab

If you use Jupyter Lab, you can also collapse headings via the Collapsible Headings module.

First, open your terminal and install the module by running the following command.

shell
pip install aquirdturtle_collapsible_headings # Or pip3 pip3 install aquirdturtle_collapsible_headings

Restart your Jupyter Lab server.

shell
jupyter lab # or jupyter-lab

Make sure Markdown is selected and enter the following code.

markdown-cell-1
# My Heading

And add the following code in another cell.

markdown-cell-2
- first - second - third

select markdown and add heading

Note that the heading and the list items are 2 separate cells.

You can press Shift + Enter to run each cell.

Notice that there is a small arrow to the left of your heading that can be used to collapse/expand it.

collapse expand heading in jupyter lab

If you don't see the arrow next to your headings, try to restart your Jupyter Lab development server and make sure you have the Collapsible Headings module installed.

# How to Collapse cells in Jupyter Notebook using nbextensions

You can also use the jupyter_contrib_nbextensions package to collapse cells in Jupyter Notebook.

First, run the following two commands in your terminal.

shell
pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user

Now, also install the jupyter_nbextensions_configurator package.

shell
pip install jupyter_nbextensions_configurator jupyter nbextensions_configurator enable --user

Restart your Jupyter Notebook development server.

shell
jupyter notebook # or jupyter-notebook
  1. Click on the Nbextensions tab.
  2. Make sure the "disable configuration for nbextensions" checkbox is unchecked.
  3. Make sure the following checkboxes are checked:
  • Codefolding
  • Codefolding in Editor
  • Collapsible Headings
  • Hide input
  • Hide input all

enable codefolding in jupyter notebook

The code folding extension enables you to fold blocks of code.

using codefolding extension

The heading folding extension enables you to collapse headings.

Suppose you have the following markdown in the first cell.

shell
## Example Heading

And the following in the second cell.

shell
- first - second - third

Make sure Markdown is selected from the dropdown menu.

make sure markdown is selected

You can now collapse headings by clicking on the arrow to the left.

collapsing headings in jupyter notebook

There is another extension from Nbextensions that also enables you to collapse each cell or all cells.

  1. On the main page of your Notebook, click on the Nbextensions tab again.

  2. Make sure Hide input and Hide input all are checked.

make sure hide input and hide input all are checked

Now switch back to your Notebook and there will be an arrow that you can use to collapse a cell.

use arrow to collapse cell

First, select the cell and then click on the arrow to collapse it.

You can click on the arrow again to expand the cell.

There is also an "eye" icon that can be used to collapse/expand all code cells.

collapse expand all code cells

The first time you click on the eye icon, all code cells are collapsed.

To expand all code cells, click on the eye icon again.

I've also written an article on how to clear cell output in Jupyter Notebook.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.