Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code editing .ipynb file: How to collapse notebook cells?

I'm editing an .ipynb file in VS Code using the Jupyter Notebook extension. I can't seem to figure out how to collapse (or expand) cells.

Can someone explain to me how to do this (using hotkeys or not using hotkeys), or give ideas for what I might be doing wrong?

I tried each of the hotkeys shown below, under three sets of conditions: 1. with the blue vertical bar on the left-hand side of my cell input selected, 2. with the blue vertical bar on the left-hand side of my cell output selected, 3. with my cursor in my cell input.

"Notebook: Collapse Cell Input" and "Notebook: Collapse Cell Output", with default bindings

like image 442
BeeePollen Avatar asked Sep 06 '25 12:09

BeeePollen


2 Answers

And another way to collapse cells is coming to v1.64. See

Cell collapsing UI

Notebook cells have a blue bar on the left side to indicate that they are focused. This bar is now interactive - you can click the top part to collapse the cell input, and the bottom part to collapse the output.

cell collapse demo

  • from https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_64.md#cell-collapsing-ui
like image 74
Mark Avatar answered Sep 10 '25 02:09

Mark


In my VSCode insiders build version 1.61.0, its working.

What's working ->

 a) One shot expand/collapse all cells(code/output) in '.ipynb'(notebook)
 b) Single cell code/output expand/collapse in '.ipynb'(notebook)
 c) code folding in `.py` file with `#%%` (hierarchy style)
  1. for .ipynb (your requirement)

    There is keybinding available: jupyter.notebookeditor.expandallcells/collapseallcells

    what it does?

            Expands/collapses all cells in one shot
    

    Find this setting->

         Top menu File->Preferences->Keyboard Shortcuts
         In search("Type to search in keybindings"...top side) 
         type "Notebook Expand" or "Notebook Collapse", check out:
    
                 Notebook: Expand/Collapse All Cells, 
                 Notebook: Expand/Collapse Cell Input and 
                 Notebook: Expand/Collapse Cell Output
    
    
         Its possible to expand/collapse single/all cells with these shortcuts keys.
    

To make this answer complete (others have answered)->

    1.  double click gutter area (immediate space on left side of code cell) to Expand/Collapse cell (code/output)

    2.  right click on gutter area to get Expand/Collapse cell (code/output)
  1. Detailed post for '.ipynb' and '.py' here
like image 34
Vikas Sharma Avatar answered Sep 10 '25 02:09

Vikas Sharma