简体   繁体   English

每次for循环后如何清除jupyter笔记本单元格中的输出?

[英]How to clear output in jupyter notebook cell after each for loop?

So, for each for loop, I generate an image + ask the user to input 'yes' or 'no'.因此,对于每个 for 循环,我生成一个图像 + 要求用户输入“是”或“否”。 I'd like the image & the user input line to be cleared from the cell before I generate a new image & new user input line.在生成新图像和新用户输入行之前,我希望从单元格中清除图像和用户输入行。 Instead, only the image is being cleared/replaced (the user input line doesn't show at all).相反,只有图像被清除/替换(用户输入行根本不显示)。
This is what it looks like now (everything under for loop should be indented to right):这就是现在的样子(for 循环下的所有内容都应该向右缩进): 在此处输入图像描述

for filename in os.listdir(folder):
clear_output(wait=True)
split_filename=os.path.splitext(filename)[0] #splitting filename from .jpg
image_id, age=split_filename.split('_', 2) #saving image_id and age
#print([image_id, age])

if int(image_id)>= starting_image_id: #start at a certain image #, specified above    
    image_ids.append(image_id) 
    ages.append(age)
 
    #This line below displays image in original color:
    #display.display(Image.open(os.path.join(folder,filename)))
    
    #These 5 lines below display image in grayscale:
    image = cv2.imread(os.path.join(folder,filename))
    image1 = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    plt.figure()   # create a new figure
    plt.imshow(image1, cmap='gray')
    plt.show()
                     
    attractive_rating=0 #so that while loop will run until user input = y or n 
    while attractive_rating != 'y' and attractive_rating !='n':
        attractive_rating=input('Do you find this face attractive? Enter y/n:')
    attractive_labels.append(attractive_rating) 
    
    if attractive_labels.count('y') > 2 and attractive_labels.count('n')>2:
        break 

You can use clear_output at the end of the loop.您可以在循环结束时使用clear_output

This code will print out some output with a specified value.此代码将打印出一些具有指定值的输出。 Each iteration the previous output is cleared and a new one is displayed instead.每次迭代都会清除先前的输出并显示新的输出。

from IPython.display import clear_output

value = 1

while value != 'x':
    value = input('Input a value. Type x to quit.')
    print(f'The value was {value}')
    clear_output(wait=True)
print('Script completed')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jupyter 笔记本中清除单元格输出的键盘快捷键 - Keyboard shortcut to clear cell output in Jupyter notebook 在每个数字之后用新行将单元格的输出保存在jupyter笔记本中 - Saving the output of a cell in jupyter notebook with a new line after each number 执行后如何从 memory 中清除 jupyter notebook 单元代码 - how to clear jupyter notebook cell code from the memory after execution 在每个单元格完成后使 jupyter nbconvert 将输出写入笔记本,而不是等到执行结束 - Make jupyter nbconvert write output to notebook after each cell finishes instead of waiting till the end of execution 如何清除Jupyter单元格中的Bokeh输出? - How to clear Bokeh output in Jupyter cell? 如何在每一步在 Jupyter Notebook 中显示 output? - How to display output in Jupyter Notebook at each step? Jupyter Notebook:在循环期间覆盖 output 单元,而不是在 append 中输出 - Jupyter Notebook: overwrite output cell during loop instead of outputting in append 在 Jupyter Notebook 的 for 循环中刷新输出 - Flush output in for loop in Jupyter notebook 如何打印像jupyter笔记本的默认单元格输出 - How to print like jupyter notebook's default cell output 如何同时捕获和显示 ipython (jupyter notebook) 单元格输出? - How to simultaneously capture and display ipython (jupyter notebook) cell output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM