Search code examples
pythonjupyter-notebooktqdm

Python: How to replace tqdm progress bar by next one in nested loop?


I use tqdm module in Jupyter Notebook. And let's say I have the following piece of code with a nested for loop.

import time
from tqdm.notebook import tqdm

for i in tqdm(range(3)):
    for j in tqdm(range(5)):
        time.sleep(1)

The output looks like this:

100%|██████████| 3/3 [00:15<00:00, 5.07s/it]
100%|██████████| 5/5 [00:10<00:00, 2.02s/it]

100%|██████████| 5/5 [00:05<00:00, 1.01s/it]

100%|██████████| 5/5 [00:05<00:00, 1.01s/it]

Is there any option, how to show only current j progress bar during the run? So, the final output after finishing the iteration would look like this?

100%|██████████| 3/3 [00:15<00:00, 5.07s/it]
100%|██████████| 5/5 [00:05<00:00, 1.01s/it]

Solution

  • You can use leave param when create progress bar. Something like this:

    import time
    from tqdm import tqdm
    
    for i in tqdm(range(3)):
        for j in tqdm(range(5), leave=bool(i == 2)):
            time.sleep(1)