2

I'm working on a machine learning project, and I want to display the accuracy and progress of my model as a constantly updating statistic in the stdout. However, instead of updating the same line, the statistics are being printed on a new line after a (seemingly) random number of iterations.

I've tested it on some much simpler code.

for i in range(100000):
    print(str(i), end="\r")

The output I get in the Jupyter Notebook is the following:

3181

6180

9180

12501

...

99999

The total output is printed over many many lines.

from time import sleep

for i in range(100):
    sleep(0.1)
    print(str(i), end="\r")

The above code works fine and the number is constantly updated on the same line.

for i in range(100000):
    print(str(i), end="\r")
    sys.stdout.flush()

The above code also works, however, it slows down the output significantly. The number is updated on the same line, however, it updates at a slower rate which I assume has something to do with a buffer speed limitation.

Therefore, I would like the stdout to update on the same line, but also as fast as it can update as the two code snippets that do work significantly reduce the speed of the output being displayed.

I don't have much experience with the sys module, but it seems to me this has something to do with my local setup. I run Windows 10, python 3.7.1, and the code is being run on a Jupyter notebook.

Farzad
  • 21
  • 2
  • Have a look at https://stackoverflow.com/questions/42855972/printing-on-the-same-line-on-a-jupyter-notebook – altunyurt Aug 15 '19 at 06:14

0 Answers0