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.
This is a case where using JupyterLab provides what you want out of the box. You can collapse the code cell in JupyterLab by pressing the blue vertical bar you'll see when you hover to the left of your code cell. When the notebook is reopened in JupyterLab, the setting will be respected.
Implement Auto-Indent Lines ( Ctrl + Alt + I ) and Reformat Code ( Ctrl + Alt + L ) for Python code cells in Jupyter notebook source code representation.
There is a shortcut for that: use o (that is the letter o) in the command mode to expand/collapse.
UPDATE:
The newer jupyter-lab
is a more modern and feature-rich interface which supports cell folding by default. See @intsco's answer below
UPDATE 2
Since jupyter-lab
now also supports extensions, you can extend the built-in cell-folding functionality with the Collapsible_Headings
extension.
Original answer:
The jupyter contrib nbextensions
Python package contains a code-folding extension that can be enabled within the notebook. Follow the link (Github) for documentation.
To install using command line:
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
To make life easier in managing them, I'd also recommend the jupyter nbextensions configurator
package. This provides an extra tab in your Notebook interface from where you can easily (de)activate all installed extensions.
Installation:
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
JupyterLab supports cell collapsing. Clicking on the blue cell bar on the left will fold the cell.
You can create a cell and put the following code in it:
%%html
<style>
div.input {
display:none;
}
</style>
Running this cell will hide all input cells. To show them back, you can use the menu to clear all outputs.
Otherwise you can try notebook extensions like below:
https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Home_3x
I had a similar issue and the "nbextensions" pointed out by @Energya worked very well and effortlessly. The install instructions are straight forward (I tried with anaconda on Windows) for the notebook extensions and for their configurator.
That said, I would like to add that the following extensions should be of interest.
Hide Input | This extension allows hiding of an individual codecell in a notebook. This can be achieved by clicking on the toolbar button:
Collapsible Headings | Allows notebook to have collapsible sections, separated by headings
Codefolding | This has been mentioned but I add it for completeness
Create custom.js file inside ~/.jupyter/custom/ with following contents:
$("<style type='text/css'> .cell.code_cell.collapse { max-height:30px; overflow:hidden;} </style>").appendTo("head");
$('.prompt.input_prompt').on('click', function(event) {
console.log("CLICKED", arguments)
var c = $(event.target.closest('.cell.code_cell'))
if(c.hasClass('collapse')) {
c.removeClass('collapse');
} else {
c.addClass('collapse');
}
});
After saving, restart the server and refresh the notebook. You can collapse any cell by clicking on the input label (In[]).
There are many answers to this question, all of which I feel are not satisfactory (some more than others), of the many extensions - code folding, folding by headings etc etc. None do what I want in simple and effective way. I am literally amazed that a solution has not been implemented (as it has for Jupyter Lab).
In fact, I was so dissatisfied that I have developed a very simple notebook extension that can expand/collapse the code in a notebook cell, while keeping it executable.
The GitHub repository: https://github.com/BenedictWilkinsAI/cellfolding
Below is a small demo of what the extension does:
Simply double clicking left of the code cell will collapse it to a single line:
Double clicking again will expand the cell.
The extension can be installed easily with pip:
pip install nbextension-cellfolding
jupyter nbextension install --py cellfolding --user
jupyter nbextension enable --py cellfolding --user
and is also compatible with nbextension configurator. I hope that people will find this useful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With