current position:Home>Python - progress bar artifact tqdm usage

Python - progress bar artifact tqdm usage

2022-02-01 06:47:35 Why

This is my participation 11 The fourth of the yuegengwen challenge 22 God , Check out the activity details :2021 One last more challenge

The progress bar display is particularly important during program operation ,Python Use in tqdm Library as a progress bar operation tool , This article briefly introduces tqdm Common functions .

background

tqdm From Arabic taqaddum, It means process ( “progress”);

Also in Spanish “I love you so much” (te quiero demasiado) Abbreviation ( This is a coincidence )

  • The function of this module is to decorate tqdm(iterable) Any iteratable object , Make the loop in the code (loop) Show the progress bar to users during operation .
  • Stolen Official website The picture shows the effect intuitively

preparation

Introduction package

from tqdm import tqdm
 Copy code 

Generate tqdm object

tqdm Class constructor :

__init__(iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, lock_args=None, nrows=None, colour=None, gui=False, **kwargs)
 Copy code 

The parameters are complex , Here are some common parameters and methods , Please refer to Official document

  • iterable: The most common parameters , Indicates that this iteration object is used to initialize tqdm object , If the progress bar is updated manually, this parameter can be None
  • desc: Description of the progress bar
  • total: Total number of progress bars
  • miniters: int, optional. Minimum update interval for progress display during iteration .
  • unit : str, optional. Define the unit of each iteration . The default is "it", That is, each iteration , When downloading or decompressing , Set to "B", For each “ byte ”.
  • unit_scale: bool or int or float, optional. The default is False, If set to 1 perhaps True, It will be automatically converted according to the international system of units (kilo, mega, etc.) . such as , In the example of downloading the progress bar , If False, The data size is displayed in bytes , Set to True Then it's converted to Kb、Mb.

Usage method

Automatically control progress

  • take tqdm() Wrap directly on any iterator :
from tqdm import tqdm
for i in tqdm(range(10000)):
    pass

>> 100%|██████████████████| 10000/10000 [00:00<00:00, 1248304.76it/s]
 Copy code 
  • Add description :
from tqdm import tqdm
for i in tqdm(range(10000), desc='test str:'):
    pass

>> test str:: 100%|█████████████████| 10000/10000 [00:00<00:00, 1666257.75it/s] 
 Copy code 
  • trange(i) It's right tqdm(range(i)) Specially optimized examples :
from tqdm import trange
for i in trange(10000):
    pass

>> 100%|██████████████████| 10000/10000 [00:00<00:00, 1191844.79it/s]
 Copy code 
  • If you want fine control tqdm object , You need to generate objects in advance , And control it in the cycle :

Using external objects, you can create objects directly , At this point, you need to close the object outside the loop :

from tqdm import tqdm
pbar = tqdm(range(10000))
for i in pbar:
    pbar.set_description(str(i))
    pass
pbar.close()

>> 9999: 100%|█████████████████████████| 10000/10000 [00:02<00:00, 4677.99it/s] 
 Copy code 

You can also use with function , Outside the loop body, the compiler automatically ends its life cycle :

from tqdm import tqdm
with tqdm(range(10000)) as pbar:
    for i in pbar:
        pbar.set_description(str(i))
        pass
    
>> 9999: 100%|█████████████████████████| 10000/10000 [00:02<00:00, 4659.45it/s] 
 Copy code 

Manually control the progress

  • When running manually tqdm Object does not require an iterator as an initialization parameter , But you need to specify the maximum length total Value :
from tqdm import tqdm
with tqdm(total=10000) as pbar:
    for i in range(10000):
        pbar.update(1)
        
>> 100%|████████████████████████████| 10000/10000 [00:00<00:00, 99888.16it/s] 
 Copy code 

Reference material

copyright notice
author[Why],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010647325948.html

Random recommended