Code folding is a quite useful solution, e.g., to enhance the visibility of scripts with large amounts of code. It allows to view only those parts of the script that are currently of interest.
How to enable code folding in JupyterLab
Code folding in JupyterLab can be enabled in the JSON Settings Editor. To do so, choose from the menu bar
Settings->Advanced Settings Editor, and in the then opened
Settings tab, click on JSON Settings Editor in the upper right corner:
In the JSON Settings Editor, click in the left panel on Notebook. In the corresponding editor window User Preferences (the rightmost window) type
Ctrl+f and search for “codeFolding”:
Replace in all occurring
This enables code folding in Jupyter notebooks. To enable code folding in pure Python scripts as well, repeat these steps also in the Text Editor tab.
Code folding in action
In Jupyter notebooks, code folding looks like:
And in pure Python scripts:
What can be folded?
We can fold all default Python code blocks like functions, for-loops, if-blocks, or classes. Unlike other editors and IDE like PyCharm, Spyder or VS Code, we are, however, not (yet) able to fold code cells (initialized by
# %%), which would further enhance the visibility of long code scripts. Maybe this will be possible in a future version of JupyterLab.