首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在木星笔记本上完成手动tqdm进度条的颜色更改

在木星笔记本上完成手动tqdm进度条的颜色更改
EN

Stack Overflow用户
提问于 2022-08-19 07:40:04
回答 2查看 214关注 0票数 2

我想在带有嵌套循环的木星笔记本中使用tqdm中的手动进度条。要对所有迭代进行概述,我将使用进度栏的手动更新如下:

代码语言:javascript
复制
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): ...的东西,进度条在完成后会变成绿色。

有人能告诉我如何为手动进度条实现相同的行为吗?谢谢你帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-19 08:07:04

我认为pbar.close()可以做到。

代码语言:javascript
复制
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()
票数 2
EN

Stack Overflow用户

发布于 2022-08-19 08:05:30

在堆栈溢出中找到以下代码:

代码语言:javascript
复制
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)

您可以将此代码编辑为:

代码语言:javascript
复制
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()

这将使进度条在完成后变成绿色。我不知道这对你是否有帮助。但是,无论如何,看看吧:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73413371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档