[英]How to clear output in jupyter notebook cell after each for loop?
因此,對於每個 for 循環,我生成一個圖像 + 要求用戶輸入“是”或“否”。 在生成新圖像和新用戶輸入行之前,我希望從單元格中清除圖像和用戶輸入行。 相反,只有圖像被清除/替換(用戶輸入行根本不顯示)。
這就是現在的樣子(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
您可以在循環結束時使用clear_output
。
此代碼將打印出一些具有指定值的輸出。 每次迭代都會清除先前的輸出並顯示新的輸出。
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.