27

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: 
yatu
  • 86,083
  • 12
  • 84
  • 139
request
  • 450
  • 1
  • 4
  • 10

3 Answers3

42

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)
...
>>>
yatu
  • 86,083
  • 12
  • 84
  • 139
3

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:")  

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
vishal vanpariya
  • 101
  • 2
  • 13
0

What i did was just using the .close() function that comes with tqdm. Example for a usecase could be an unzipping process. In this example it would be from a download and in form of raw bytes. Tho that doesn't matter because as you can see i stop the pbar with pbar.close() what would be another correct answer to your problem :)

z = zipfile.ZipFile(io.BytesIO(dat), 'r')
total_size = len(dat)
extracted_size = 0
with z as zip_ref:
    file_list = zip_ref.namelist()
    total_files = len(file_list) + 1

    with tqdm.tqdm(total=total_size, unit='B', unit_scale = True, ascii=' █', desc='Extracting ' + name_of_lanModel[choosen_model]["name"],
                   colour='#b7d121', smoothing=0.01) as pbar:
        
        for file in file_list:
            extracted_file_path = zip_ref.extract(file, path)
            extracted_file_size = zip_ref.getinfo(file).file_size
            extracted_size += extracted_file_size
            pbar.update(extracted_file_size)
            if extracted_size >= total_size:
                break
    pbar.close()
morigan
  • 23
  • 9