可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
回答2:
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
回答3:
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[]).
回答4:
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
回答5:
The hide_code extension allows you to hide individual cells, and/or the prompts next to them. Install as
pip3 install hide_code
Visit https://github.com/kirbs-/hide_code/ for more info about this extension.
回答6:
Firstly, follow Energya's instruction:
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
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"
This is what you want!
回答7:
JupyterLab supports cell collapsing. Clicking on the blue cell bar on the left will fold the cell.
回答8:
There's also an improved version of Pan Yan suggestion. It adds the button that shows code cells back:
%%html
<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>
Or python:
# 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>"""))