plhn
plhn

Reputation: 5273

tqdm printing a new line when double progress bars are used

How to generate this problem:

Python 2.7.18 |Anaconda, Inc.| (default, Apr 23 2020, 22:42:48) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tqdm import tqdm
>>> for i in tqdm(range(10)):
...     for j in tqdm(range(5)):
...             import time
...             time.sleep(0.5)
... 
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|###################################################################| 5/5 [00:02<00:00,  2.00it/s]
100%|#################################################################| 10/10 [00:25<00:00,  2.51s/it]
>>> 

Is this intended by design?
How can I avoid this problem? (I want to see just two lines which indicate 100%)

Upvotes: 0

Views: 7109

Answers (2)

Kuldeep Singh Sidhu
Kuldeep Singh Sidhu

Reputation: 3856

TWO WAYS

1: Create a label for inner loop (only one bar is shown but you know where your inner loop is)

with tqdm(range(10), desc="outer_loop") as pbar:
    for i in pbar:
        for j in range(5):
            pbar.set_postfix(inner_loop=j, refresh=True)
            import time
            time.sleep(0.5)

2: for 2 bars(only for notebooks): you might need to update your tqdm

In addition to tqdm features, the submodule provides a native Jupyter widget (compatible with IPython v1-v4 and Jupyter), fully working nested bars and colour hints

DOCS

from tqdm.notebook import trange, tqdm
from time import sleep

for i in trange(10, desc='1st loop'):
    for j in tqdm(range(5), desc='2nd loop'):
        sleep(0.5)

Upvotes: 1

Sahith Kurapati
Sahith Kurapati

Reputation: 1715

You should use position = 0 and leave = True parameters.

from tqdm import tqdm
import time
for i in tqdm(range(10), position = 0, leave = True):
    for j in tqdm(range(5), position = 1, leave = True):
        time.sleep(0.5)

Hope it helps :)

Upvotes: 4

Related Questions