192

In a iPython notebook, I have a while loop that listens to a Serial port and print the received data in real time.

What I want to achieve to only show the latest received data (i.e only one line showing the most recent data. no scrolling in the cell output area)

What I need(i think) is to clear the old cell output when I receives new data, and then prints the new data. I am wondering how can I clear old data programmatically ?

aha
  • 3,894
  • 3
  • 21
  • 27

6 Answers6

371

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.

cel
  • 27,769
  • 16
  • 88
  • 113
  • 1
    Simplest ajax interface ever! – Unni Mar 28 '17 at 20:52
  • 67
    Using the `clear_output(wait=True)` will generally make the result nicer if you have `clear_output` inside a loop. – Toke Faurby Jun 08 '17 at 23:49
  • 1
    half of the screen after the print result shakes, but shake less thanks to the wait=True – B.Mr.W. Aug 12 '18 at 04:26
  • 2
    But this clears all the output. Is there a way to clear only the most recent figure? Edit: this is what I was looking for: https://github.com/jupyter-widgets/ipywidgets/issues/1744#issuecomment-335179855 – Solomon Vimal Jul 18 '19 at 00:13
  • 1
    Why are we looping? What's with `i`? – jorijnsmit Dec 18 '19 at 16:38
  • 2
    @jorijnsmit, just to illustrate that "Hello World!" is not printed 10 times, but just 1 time. `i` doesn't really matter. – cel Dec 18 '19 at 17:45
9

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)
hof.and.or
  • 106
  • 1
  • 4
3

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)
2

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.

marzetti
  • 149
  • 1
  • 6
1

If I am not wrong you mean you just need to clear the output part of a cell, which could be a valid output or some error which you don't what to look anymore. If Yes! just go to top ribbon and select Cell > Current Outputs > Clear

VinitV
  • 43
  • 2
  • 3
    That should work for interactive clearing of the cell, but OP asked for a programmatic way to do this. – joanis Aug 05 '21 at 17:17
0

Simple, if you want to clear the output of that current cell only just use this at the end of that cell

from IPython.display import clear_output
clear_output(wait=False)
Deepanshu Mehta
  • 227
  • 2
  • 5