Can I collapse/fold cells in the exported HTML file from a notebook?

It seems like a have to see all the cell outputs unfolded in an exported HTML file from a notebook.
However, because some cells contain long outputs, I would like them to be collapsible for readability.

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 configuratorpackage. 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

@takefive Is what you are describing for the traditional notebook interface or JupyterLab? Or both? JupyterLab has better inherent support for code-folding that even survives to reopening later. However, I don’t now about saving it as HTML.

The original poster didn’t provide any detail of what they were using. And no version information about the interface either.

Is there a good solution for this? jiofi.local.html bitlife pc

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?

Since you mention ‘executable’ / ‘callable’, I suspect you actually aren’t talking about exporting as HTML, and instead saving as a notebook file? Clarify if I misunderstood. Assuming that…
This is a case where using JupyterLab provides what you want out of the box. You can collapse the code cell in JupyterLab (and not Jupyter Notebook 7+) 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. In other words, if you had it hidden, it will remain hidden.

Click here for a demonstration.
When that demonstration notebook opens, a huge block of code will be collapsed at the top. You can click the three dots to reveal it.


See also this thread and discussion if you haven’t already.



Jupyter Notebook 7+ has code-folding that can be activated in the same ways as JupyterLab, see above. This is because Jupyter Notebook 7+ is built on JupyterLab components.


In the classic Jupyter notebook world (6.4 and below and NbClassic), there;s a number of solutions for hiding cells or the code cells or the output. The hide_code extension is one of them. If you go here there there is a link to more about it and by clicking launch binder you can test it right in your browser. You can chose toggle all icon, looks like </> from the toolbar or select from the menubar View > Cell Toolbar > Hide code to access the custom per cell settings.
I have some notes about the export not quite working from the session, however, that was two years ago.

Thanks for your reply.. .