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

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.