如何在 Jupyter Notebook 中使用 flush 打印计数器?

How to print counter using flush in Jupyter notebook?

以下代码曾经在 Python 2.7 中工作,但在 Python 3.7 jupyter notebook 中似乎不起作用。

import sys
import time
for i in range(100):
    sys.stdout.write(str(i))
    sys.stdout.flush()
    time.sleep(1)

Python:3.7.3 木星实验室:0.35.4

与其打印我想打印的所有数字,不如清除并再次打印。

这似乎在 Python 2.7 和 3.7 内核中都实现了预期的行为。 '\r' 是必要的。我尝试了 '\n' 并在新行上打印了一个数字。

import sys
import time

for i in range (100):  
    sys.stdout.write(f"\r{str(i)}")
    time.sleep(1)