简体   繁体   中英

Change color of manual tqdm progress bar when finished in Jupyter notebook

I want to use a manual progress bar from tqdm in a Jupyter notebook with a nested loop. To have an overview over all iterations, I use a manual update of the progress bar as follows:

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()

进度条

However, when the total number of iterations is reached (ie, 100%), the color is still blue. But if I use something like for i in trange(100): ... , the progress bar turns green after finishing.

Can someone tell me how to achieve the same behavior for the manual progress bar? Thanks for help!

I think pbar.close() can do it.

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()

Found this code on stackoverflow:

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)

You can edit this code to this:

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()

This will turn the progress bar to green when it is done. I don't know whether it will help you or not. But, check it out anyway:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM