How to Collapse cells in a jupyter notebook

Data Science projects can be particularly tough to produce and work on and can also be a mammoth task to replicate. It is here where the knowledge of how to collapse cells in a Jupyter notebook comes into the picture and it can help you make your code 10x more readable and easy to understand. Reproducibility of the code becomes easier and if your code has to be production-ready, it must be easy to work on and understood by other data scientists equally.

Some of these projects can have code and markdown texts or comments which can be as long as thousands of lines for really big projects. One of the most important things to consider when making such long projects or code is the ‘readability’ of the code. Only when your code is easily readable, it can be easily understood and deployed.

In this short article, we’ll go over how to install NBextensions or Notebook extensions to enable the collapse cells in a Jupyter Notebook.

What are Jupyter Notebooks?

Jupyter Notebook is probably the best open-source web application that one can use to create, share and work on documents that can contain code, visuals, and any other textual form of information needed. Starting in the year 2014, it is being maintained by the people at Project Jupyter. 

Although Jupyter Notebooks are, a similar project to the initial IPython, it gets its name i.e. Jupyter because of the multiple programming languages that it supports: Julia, Python, and R. Python and R are two of the most popular programming languages being used today and in Data Science, their importance is second to none other. One cannot become a data scientist without having to deal with these programming languages first. 

How to collapse Cells in a Jupyter Notebook?

While collapsible cells are not an in-built function, one can collapse cells in a Jupyter Notebook using an extension named NBextensions or Notebook Extensions. Use the following code in your Anaconda Prompt or Command-Line to install the NBextensions package:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

To know more about NBextensions: Read the documentation here.

Once done, start your Jupyter Notebook and you will find a new ribbon on the toolbar in your Jupyter Notebook as follows;

How to collapse cells in a Jupyter Notebook
NBextensions ribbon in the toolbar of Jupyter Notebook

Click on that ribbon and you will see all the available features of the NBextensions. Also, ensure you don’t disable all of them by unchecking the box on the top left as shown below.

How to collapse cells in a Jupyter Notebook
Code folding and collapse cells in a jupyter notebook

To Collapse Cells in a Jupyter Notebook, ensure you have checked the following options to be turned on in the features as shown in the image above:

  1. Code folding
  2. Code folding in Editor
  3. Collapsible headings

Once you have checked them, you can start a new notebook using Python 3 in your desired working directory.

Also, read -> how to change the working directory in a Jupyter notebook at BuggyProgrammer

When you create a new notebook, ideally you should see small triangles next to your code indicating that you can now collapse them as required. You can hide/show the contents of the cell using the triangle and the blue button as shown in the image. One will only see the first line of the code in a case where the cell has collapsed.

How to collapse cells in a Jupyter Notebook
A collapsed cell for reference purposes

Note: You can also use the keyboard shortcut – Alt + F to collapse the cell 

How to collapse cells in a Jupyter Notebook
Contents of the collapsed cell in a Jupyter notebook

How to Collapse Headings in a Jupyter Notebook?

When you have markdown texts in your code, which you can make by changing the cell type, there are multiple headings and code under them. Maybe there is a function you defined to work for your analysis but it takes away a lot of space and maybe there are just too many commented cells underneath to support some back-end analysis which may or may not be required by the end-user. It is in these cases that collapsible headings will help.

In a markdown cell, use a # followed by the heading words to create a heading of size H1. You can increase the number of #’s from 1 to 6 to reduce the size of the heading from H1 to H6. Which will look something like this;

Collapse headings in jupyter Notebooks
Heading in a markdown cell (notice the triangle on the left for collapsible heading)

To collapse the heading, use the triangle on the left of the markdown cell.

14 1 Collapse cells in a jupyter notebook,jupyter notebook
Collapsed heading withholding the code cells as can be seen in the previous image.

Conclusion

Finding innovative ways to make your code more readable and your analysis neater is a part of being a data scientist. While you can have the most amazing of code and analysis, it is equally important to ensure that you can communicate the story your data is telling you to the stakeholders clearly. Hence, ensure that you only show the stakeholders what they need to know and not bombard them with irrelevant data or parts in your analysis that they might not understand. Collapsible cells, different headings and clearer visuals will help you take your project to the next level.

Try out collapsible cells in a jupyter notebook and let us know if you find it useful!

Find more such content on our blog : Buggy Programmer

Share this post

Read next...

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments