iPython/ Jupyter notebook clear only one line of output

Cas*_*sey 6 python-3.x jupyter-notebook

How can I print the status of a Jupyter notebook on the previous line? I think I'm looking for something like clear_output(), but for only a single line.

Sample code:

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.')
Run Code Online (Sandbox Code Playgroud)

When this runs it gives the behavior of over-writing the previous status line, but both of the lines marked as important (with exclamation points and everything!) are lost. When this is finished the display just says:

Done. 
Run Code Online (Sandbox Code Playgroud)

What it should say is:

This is important info!
Something bad happened on run 50.  This needs to be visible at the end!
Done.
Run Code Online (Sandbox Code Playgroud)

这篇文章建议使用 clear_output() 然后重新打印所有内容。这似乎不切实际,因为我真正倾向于显示的数据量很大(大量图形、数据框……)。

这是关于 clear_output()的两个 SO链接。

有没有一种方法可以使这项工作不涉及重印所有内容?

小智 5

我使用显示句柄的更新功能唤醒了它:

from IPython.display import display
from time import sleep

print('Test 1')
dh = display('Test2',display_id=True)
sleep(1)
dh.update('Test3')
Run Code Online (Sandbox Code Playgroud)


Art*_*tem 2

这将做到这一点:

import IPython

IPython.display.display_javascript(r'''
    var el = document.querySelector('.output_text:last-of-type > pre');
    el.innerHTML = el.innerHTML.replace(/(\n.*$)/gm,""); ''', raw=True)
Run Code Online (Sandbox Code Playgroud)