Search code examples
pythonjupyter-notebooktqdm

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

progress bar

However, when the total number of iterations is reached (i.e., 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!


Solution

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