Jupyter Notebook: command for hide the output of a cell?

IpythonJupyterJupyter Notebook

Ipython Problem Overview

In my notebook, I have a cell returning temp calculation results. It's a bit long, so after it is run, I want to hide it and when needed, to show it.

To do it manually, I can double click the left side of the output, to hide it

enter image description here

After double click enter image description here

But is there any way I can do this by code? For example,

the last line of the cell, use a command like %%hide output, and the output would be hidden after finished running.

Additionally, can I get this feature in output HTML?

Ipython Solutions

Solution 1 - Ipython

Add ; by the end of the cell to hide the output of that cell.

Solution 2 - Ipython

In the newer versions(5.0.0 at the time I'm writing this), pressing o in the command mode hides the output of the cell in focus. The same happens if you triple click in front of the output.

o is

Solution 3 - Ipython

You can add %%capture to the beginning of the cell.

Jupyter provides a magic cell command called %%capture that allows you to capture all of to outputs from that cell.

You can use it like this:

%%capture test print('test')

test.stdout => 'test\n'


Solution 4 - Ipython

In newer versions of Jupiter Notebook, select the desired cell, make sure you're in command mode and then on the menubar press Cell > Current Outputs. You have then three options:

  • Toggle (press O in the command mode to apply the same effect)
  • Toggle Scrolling (the default output)
  • Clear (to clear the output all together)

Image to Menubar Options

Additionally, you can apply the same effect to all the cells in your document if you chose All Output instead of Current Output.

Solution 5 - Ipython

Not exactly what you are after, but the effect might be good enough for your purposes:

Look into the %%capture magic (https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb). It lets you assign that cell output to a variable. By calling that variable later you could see the output.

Solution 6 - Ipython

Based on this, I just came up with this for myself a few minutes ago:



var toggle_button = ("<button id='toggleButton' type='button'>Show Code</button>");

var code_shown = false;

function code_toggle()

    if (code_shown)
        console.log("code shown")
        $('#toggleButton').text('Show Code');
        console.log("code not shown")
        $('#toggleButton').text('Hide Code');

    code_shown = !code_shown;

$('#toggleButton').on('click', code_toggle);

It does have a glitch: each time you run that cell (which I put at the top), it adds a button. So, that is something that needs to be fixed. Would need to check in the maintoolbar-container to see if the button already exists, and then not add it.


I added the necessary piece of code:


Solution 7 - Ipython

You can use the notebook utils from https://github.com/google/etils:

!pip install etils[ecolab]

from etils import ecolab

with etils.collapse():
  print('This content will be hidden by default')

It will capture the stdout/stderr output and display it a some collapsible section.

Internally, this is more or less equivalent to:

import contextlib
import html
import io
import IPython.display

def collapse(name: str = ''):
  f = io.StringIO()
  with contextlib.redirect_stderr(f):
    with contextlib.redirect_stdout(f):
  name = html.escape(name)
  content = f.getvalue()
  content = html.escape(content)
  content = f'<pre><code>{content}</code></pre>'
  content = IPython.display.HTML(

enter image description here

The section is collapsed by default, but I uncollapsed it for the screenshot.

Solution 8 - Ipython

For Windows, in Jupyter Notebook, click the cell whose output you want to hide. Click Esc + o for toggling the output

Solution 9 - Ipython

To prepend a cell from getting rendered in the output, in the notebook, by voilo or voila gridstack, just put in the first line of each cell to hide the output:

%%capture --no-display

reference in ipypthon documentation

Solution 10 - Ipython

If you don't mind a little hacking, then you may write a simple script for inverting the "collapsed" attribute of each cell from false to true in the notebook .ipynb file (which is a simple JSON file). This is however may fail in the future if a the .ipynb format changes.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncqcn1991View Question on Stackoverflow
Solution 1 - IpythonRaniere SilvaView Answer on Stackoverflow
Solution 2 - IpythonSundeepView Answer on Stackoverflow
Solution 3 - IpythonHugo Lemieux-FournierView Answer on Stackoverflow
Solution 4 - IpythonKareem JeiroudiView Answer on Stackoverflow
Solution 5 - IpythonJacobView Answer on Stackoverflow
Solution 6 - IpythonabalterView Answer on Stackoverflow
Solution 7 - IpythonConchylicultorView Answer on Stackoverflow
Solution 8 - IpythonShanice FernandesView Answer on Stackoverflow
Solution 9 - Ipythonuser8504816View Answer on Stackoverflow
Solution 10 - IpythonSamy ZafranyView Answer on Stackoverflow