Last updated: Apr 13, 2024
Reading time·4 min
There are multiple ways to collapse cells in Jupyter Notebook:
nbextension
package.You can collapse cells in Jupyter lab out of the box.
First, make sure that you
have jupyterlab
installed.
pip install jupyterlab # Or with pip3 pip3 install jupyterlab
Now, start Jupyter lab by issuing the following command.
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.
print('bobbyhadz.com') print('example.com') print('google.com')
And here is a short clip that demonstrates how collapsing cells works.
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.
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.
You can:
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.
pip install aquirdturtle_collapsible_headings # Or pip3 pip3 install aquirdturtle_collapsible_headings
Restart your Jupyter Lab server.
jupyter lab # or jupyter-lab
Make sure Markdown
is selected and enter the following code.
# My Heading
And add the following code in another cell.
- first - second - third
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.
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.
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.
pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user
Now, also install the jupyter_nbextensions_configurator
package.
pip install jupyter_nbextensions_configurator jupyter nbextensions_configurator enable --user
Restart your Jupyter Notebook development server.
jupyter notebook # or jupyter-notebook
Nbextensions
tab.The code folding extension enables you to fold blocks of code.
The heading folding extension enables you to collapse headings.
Suppose you have the following markdown in the first cell.
## Example Heading
And the following in the second cell.
- first - second - third
Make sure Markdown is selected from the dropdown menu.
You can now collapse headings by clicking on the arrow to the left.
There is another extension from Nbextensions
that also enables you to collapse
each cell or all cells.
On the main page of your Notebook, click on the Nbextensions
tab again.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: