Jupyter Notebook: overwrite output cell during loop instead of outputting in append

Seymour :

This question is different from others because I need the output cell to be overwritten and updated many times throughout the process, instead, other questions just care about outputting the final value when the entire loop completes.

Problem

I am executing some actions within a loop and every 100th iteration I need to track the update of the process using a visualization (plotly). The problem is that instead of keeping only one plot in the output cell, they are accumulated and soon I need to scroll down 20 pages to find the latest plot in the output cell.

Example

For the sake of simplicity, in this question let's assume I just want to visualize some text.

for i in range(10000):
    if i%100 == 0:
        print('This is an update for iteration number {:d}'.format(i))

Expected solution

Instead of visualizing in the cell:

100
200
300
...

I would like the output cell to be overwritten every time a new output is visualized throughout the loop.

a.costa :

You can use IPython.display.clear_output to clear the output of a cell:

from IPython.display import clear_output

for i in range(10000):
if i%100 == 0:
    clear_output()
    print('This is an update for iteration number {:d}'.format(i))

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=404716&siteId=1