如何在上一行打印Jupyter笔记本的状态?我认为我正在寻找类似于clear_output()但仅适用于单行的内容。
示例代码:
from IPython.display import clear_output
import time
print('This is important info!')
for i in range(100):
print('Processing BIG data file {}'.format(i))
time.sleep(0.1)
clear_output(wait=True)
if i == 50:
print('Something bad happened on run {}. This needs to be visible at the end!'.format(i))
print('Done.')
当这段代码运行时,它会覆盖之前的状态行,但是两个被标记为重要的行(包括感叹号和其他所有内容!)都会丢失。当代码完成后,显示器上只会显示:
Done.
它应该说的是:
这是它应该说的:
This is important info!
Something bad happened on run 50. This needs to be visible at the end!
Done.
这篇文章建议使用clear_output(),然后重新打印所有内容。但是,这似乎不切实际,因为我要显示的数据量很大(有很多图形、数据框等)。
有没有一种方法可以使这个工作不涉及重新打印所有内容呢?