Skip to main content
 首页 » 编程设计

python之单元格运行时更改 ipython 中的输出

2025年02月15日11tuyile006

是否可以让 ipython 执行以下操作?

#A cell... 
output("a") # the cell output shows "a" 
change_output("b") # the cell output now shows "b". "a" has been deleted 

我想要这种行为,以便单元格输出可以显示当前运行状态,而无需大量输出行。

请您参考如下方法:

是的,像这样:

import sys 
from IPython.core.display import clear_output 
def change_output(x): 
    clear_output() 
    sys.stdout.write(x) 
    sys.stdout.flush() 
 
change_output("a") 
change_output("b")