Python 覆盖 jupyter notebook 中的先前输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38540395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:06:24  来源:igfitidea点击:

Overwrite previous output in jupyter notebook

pythonjupyterjupyter-notebook

提问by Salvador Dali

Let's assume I have a part of code that runs for some specific amount of time and each 1 second outputs something like this: iteration X, score Y. I will substitute this function with my black box function:

假设我有一个代码一部分的时间一些具体的金额和各1个第二输出端是这样运行的:iteration X, score Y。我将用我的黑盒函数替换这个函数:

from random import uniform
import time

def black_box():
    i = 1
    while True:
        print 'Iteration', i, 'Score:', uniform(0, 1)
        time.sleep(1)
        i += 1

Now when I run it in Jupyter notebook, it output a new line after each second:

现在,当我在Jupyter notebook 中运行它时,它每秒输出一个新行:

Iteration 1 Score: 0.664167449844
Iteration 2 Score: 0.514757592404
...

Yes, after when the output becomes too big, the html becomes scrollable, but the thing is that I do not need any of these lines except of currently the last one. So instead of having nlines after nseconds, I want to have only 1line (the last one) shown.

是的,当输出变得太大后,html 变得可滚动,但问题是除了当前最后一行之外,我不需要任何这些行。因此,我不想nn几秒钟后显示行,而是只显示1行(最后一行)。

I have not found anything like this in documentation or looking through magic. A questionwith almost the same title but irrelevant.

我没有在文档或通过魔法寻找任何类似的东西。一个标题几乎相同但无关紧要的问题。

回答by DangerousDave

@cel is right: ipython notebook clear cell output in code

@cel 是对的:ipython notebook 清除代码中的单元格输出

Using the clear_output() gives makes your Notebook have the jitters, though. I recommend using the display() function as well, like this (Python 2.7):

但是,使用 clear_output() 会使您的笔记本产生抖动。我建议也使用 display() 函数,就像这样(Python 2.7):

from random import uniform
import time
from IPython.display import display, clear_output

def black_box():
i = 1
while True:
    clear_output(wait=True)
    display('Iteration '+str(i)+' Score: '+str(uniform(0, 1)))
    time.sleep(1)
    i += 1

回答by chapelo

The usual (documented) way to do what you describe (that only works with Python 3) is:

执行您所描述的操作(仅适用于 Python 3)的常用(已记录)方法是:

print('Iteration', i, 'Score:', uniform(0, 1), end='\r')

In Python 2 we have to sys.stdout.flush()after the print, as it shows in this answer:

在 Python 2 中,我们必须sys.stdout.flush()在打印之后,如本答案所示

print('Iteration', i, 'Score:', uniform(0, 1), end='\r')
sys.stdout.flush()

Using IPython notebook I had to concatenate the string to make it work:

使用 IPython 笔记本,我必须连接字符串才能使其工作:

print('Iteration ' + str(i) + ', Score: ' + str(uniform(0, 1)), end='\r')

And finally, to make it work with Jupyter, I used this:

最后,为了让它与 Jupyter 一起工作,我使用了这个:

print('\r', 'Iteration', i, 'Score:', uniform(0, 1), end='')

Or you could split the prints before and after the time.sleepif it makes more sense, or you need to be more explicit:

或者printtime.sleep如果更有意义,您可以在s 之前和之后拆分s ,或者您需要更明确:

print('Iteration', i, 'Score:', uniform(0, 1), end='')
time.sleep(1)
print('', end='\r') # or even print('\r', end='')