How to remove progressbar in tqdm once the iteration is complete

Viewed 11326

How can I archive this?

from tqdm import tqdm    
for link in tqdm(links):
        try:
            #Do Some Stff
        except:
            pass  
print("Done:")  

Result:

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 111.50it/s]
Done:   

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 111.50it/s]
Done:  

Expected Result (Showing the status bar but don't print it after into the console)

Done:  
Done: 
2 Answers

tqdm actually takes several arguments, one of them is leave, which according to the docs:

If [default: True], keeps all traces of the progressbar upon termination of iteration. If None, will leave only if position is 0

So:

>>> for _ in tqdm(range(2)):
...     time.sleep(1)
...
100%|██████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.01s/it]

Whereas setting leave=False yields:

>>> for _ in tqdm(range(2), leave=False):
...     time.sleep(1)
...
>>>

You can pass parameter disable=True.

Source: https://pypi.org/project/tqdm/

disable: bool, optional
Whether to disable the entire progress bar wrapper [default: False]. If set to None, disable on non-TTY.

from tqdm import tqdm    
for link in tqdm(links,disable=True):
        try:
            #Do Some Stff
        except:
            pass  
print("Done:")  

Related