Last updated: Apr 10, 2024
Reading time·4 min
Use the clear_output()
method to clear the output of a cell while running
code.
The method enables you to wait to clear the output until new output is available to replace it.
from IPython.display import clear_output for i in range(50): clear_output(wait=True) print('bobbyhadz.com')
The only argument we passed to the clear_output
method is a boolean.
If set to True
, then the method waits to clear the output until the new output
is available to replace it.
Notice that only a single message is printed after iterating over the range 50 times.
Make sure to specify the message that you want to actually
print after the call to the
clear_output()
method.
If you need to flush the output when calling print()
set the flush
argument
to True
.
from IPython.display import clear_output for i in range(50): clear_output(wait=False) print('bobbyhadz.com', flush=True)
If you need to clear the cell output from the top menu:
Click on Cell and hover over Current Outputs.
Click on Clear.
Here is a short clip that demonstrates how this works.
Note that this only clears the output of the current cell.
If you need to clear the output of all cells:
Click on Cell and hover over All Output.
Click on Clear.
Here is a short GIF that shows how this works.
If you need to set up a keyboard shortcut to clear the output of a cell:
Press Ctrl
+ F
(or Cmd
+ F
on macOS) and search for clear.
If you need to set a keyboard shortcut to clear the output of a single cell, look for the clear cell output command.
Click on the add shortcut input field and specify your preferred keyboard
shortcut, e.g. Ctrl-4
or Ctrl-O
.
If you need to set a keyboard shortcut to clear the output of all cells, look for the clear all cells output command toward the bottom.
Set your preferred keyboard shortcut for the command.
Once you set the keyboard shortcut, click on the OK button.
To use your keyboard shortcuts, click to the left of your cell so that the left border becomes blue and press your keyboard shortcut.
To not have to click with your mouse, you can just press Ctrl
+ Enter
to run
the code in the cell so that the left border becomes blue and then press your
keyboard shortcut.
You can also clear the cell output via the command palette:
Ctrl
+ Shift
+ P
(or Cmd
+ Shift
+ P
on macOS).I've also written a detailed guide on how to wrap code/text into multiple lines in Jupyter Notebook.
You can learn more about the related topics by checking out the following tutorials:
pd.read_json()