I am using ipython Jupyter notebook. Let’s say I defined a function that occupies a lot of space on my screen. Is there a way to collapse the cell?
I want the function to remain executed and callable, yet I want to hide / collapse the cell in order to better visualize the notebook. How can I do this?
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 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.
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
回答 4
在〜/ .jupyter / custom /内创建具有以下内容的custom.js文件:
$("<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');}});
Second is the key: After opening jupiter notebook, click the Nbextension tab. Now Search “colla” from the searching tool provided by Nbextension(not by the web browser), then you will find something called “Collapsible Headings”
As others have mentioned, you can do this via nbextensions. I wanted to give the brief explanation of what I did, which was quick and easy:
To enable collabsible headings:
In your terminal, enable/install Jupyter Notebook Extensions by first entering:
pip install jupyter_contrib_nbextensions
Then, enter:
jupyter contrib nbextension install
Re-open Jupyter Notebook. Go to “Edit” tab, and select “nbextensions config”.
Un-check box directly under title “Configurable nbextensions”, then select “collapsible headings”.
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.
# Run me to hide code cellsfromIPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))
# Run me to hide code cells
from IPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))