我想在带有嵌套循环的木星笔记本中使用tqdm中的手动进度条。要对所有迭代进行概述,我将使用进度栏的手动更新如下:
from tqdm.notebook import tqdm
a = range(100)
b = range(5)
pbar = tqdm(total=len(a)*len(b))
for a_ in a:
for b_ in b:
pbar.update(1)
pbar.refresh()

但是,当达到总迭代次数(即100%)时,颜色仍然是蓝色。但是,如果我使用类似于for i in trange(100): ...的东西,进度条在完成后会变成绿色。
有人能告诉我如何为手动进度条实现相同的行为吗?谢谢你帮忙!
发布于 2022-08-19 08:07:04
我认为pbar.close()可以做到。
from tqdm.notebook import tqdm
a = range(100)
b = range(5)
pbar = tqdm(total=len(a)*len(b))
for a_ in a:
for b_ in b:
pbar.update(1)
pbar.refresh()
pbar.close()发布于 2022-08-19 08:05:30
在堆栈溢出中找到以下代码:
def progress_function(chunk, file_handling, bytes_remaining):
'''
function to show the progress of the download
'''
global filesize
filesize=chunk.filesize
current = ((filesize - bytes_remaining)/filesize)
percent = ('{0:.1f}').format(current*100)
progress = int(50*current)
status = '█' * progress + '-' * (50 - progress)您可以将此代码编辑为:
def progress_function(chunk, file_handling, bytes_remaining):
'''
function to show the progress of the download
'''
global filesize
filesize=chunk.filesize
current = ((filesize - bytes_remaining)/filesize)
percent = ('{0:.1f}').format(current*100)
progress = int(50*current)
status = '█' * progress + '-' * (50 - progress)
#change the color of the progress bar to green when the download is complete
if bytes_remaining == 0:
status = '\033[92m' + status + '\033[0m'
sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status,
percent=percent))
sys.stdout.flush()这将使进度条在完成后变成绿色。我不知道这对你是否有帮助。但是,无论如何,看看吧:)
https://stackoverflow.com/questions/73413371
复制相似问题