When you have Jupyter notebook opened, you can do this by selecting the Cell -> All Output -> Clear menu item.
You can use IPython. display. clear_output to clear the output of a cell.
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
You can use IPython.display.clear_output
to clear the output of a cell.
from IPython.display import clear_output
for i in range(10):
clear_output(wait=True)
print("Hello World!")
At the end of this loop you will only see one Hello World!
.
Without a code example it's not easy to give you working code. Probably buffering the latest n events is a good strategy. Whenever the buffer changes you can clear the cell's output and print the buffer again.
You can use the IPython.display.clear_output to clear the output as mentioned in cel's answer. I would add that for me the best solution was to use this combination of parameters to print without any "shakiness" of the notebook:
from IPython.display import clear_output
for i in range(10):
clear_output(wait=True)
print(i, flush=True)
And in case you come here, like I did, looking to do the same thing for plots in a Julia notebook in Jupyter, using Plots, you can use:
IJulia.clear_output(true)
so for a kind of animated plot of multiple runs
if nrun==1
display(plot(x,y)) # first plot
else
IJulia.clear_output(true) # clear the window (as above)
display(plot!(x,y)) # plot! overlays the plot
end
Without the clear_output call, all plots appear separately.
You can have a better visualization of the function clear_output(wait=True)
with this simple code.
from IPython.display import clear_output
import time
for i in tqdm(range(10)):
clear_output(wait=True)
print(i)
time.sleep(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With