ipython notebook clear cell output in code

前端 未结 3 1041
深忆病人
深忆病人 2020-12-02 06:07

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 t

相关标签:
3条回答
  • 2020-12-02 06:26

    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.

    0 讨论(0)
  • 2020-12-02 06:30

    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.

    0 讨论(0)
  • 2020-12-02 06:33

    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)
    
    0 讨论(0)
提交回复
热议问题