Detailed explanation of Python progress bar library tqdm

The tqdm module is a python progress bar library, which is mainly divided into two operating modes

Run on an iterable object: tqdm(iterator)

importtimefromtqdmimporttqdm,trange#trange(i)是tqdm(range(i))的一种简单写法foriintrange(100):time.sleep(0.05)foriintqdm(range(100),desc='Processing'):time.sleep(0.05)dic=['a','b','c','d','e']pbar=tqdm(dic)foriinpbar:pbar.set_description('Processing '+i)time.sleep(0.2)100%|██████████|100/100[00:06<00:00,16.04it/s]Processing:100%|██████████|100/100[00:06<00:00,16.05it/s]Processinge:100%|██████████|5/5[00:01<00:00,4.69it/s]

Update manually

importtimefromtqdmimporttqdmwithtqdm(total=200)aspbar:pbar.set_description('Processing:')# total表示总的项目, 循环的次数20*10(每次更新数目) = 200(total)foriinrange(20):# 进行动作, 这里是过0.1stime.sleep(0.1)# 进行进度更新, 这里设置10个pbar.update(10)Processing::100%|██████████|200/200[00:02<00:00,91.94it/s]

tqdm module parameter description

classtqdm(object):"""
  Decorate an iterable object, returning an iterator which acts exactly
  like the original iterable, but prints a dynamically updating
  progressbar every time a value is requested.
  """def__init__(self,iterable=None,desc=None,total=None,leave=False,file=sys.stderr,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,nested=False,bar_format=None,initial=0,gui=False):

iterable: iterable object, no need to set when updating manually

desc: String, description text of the left progress bar

total: the total number of items

leave: bool value, whether to keep the progress bar after the iteration is completed

file: The output points to the location, the default is the terminal, and generally does not need to be set

ncols: Adjust the width of the progress bar, the default is to automatically adjust the length according to the environment, if it is set to 0, there will be no progress bar, only the output information

unit: The text describing the processing item, the default is 'it', for example: 100 it/s, if it is set to 'img' for processing photos, it is 100 img/s

unit_scale: Automatically convert project processing speed units according to international standards, such as 100000 it/s >> 100k it/s

The following is an example show

importtimefromtqdmimporttqdm# 发呆0.5sdefaction():time.sleep(0.5)withtqdm(total=100000,desc='Example',leave=True,ncols=100,unit='B',unit_scale=True)aspbar:foriinrange(10):# 发呆0.5秒action()# 更新发呆进度pbar.update(10000)Example:100%|███████████████████████████████████████████████████|100k/100k[00:05<00:00,19.6kB/s]

Can python's tqdm adjust the color of the progress bar?

It can be adjusted by the parameter bar_format, for example:

from tqdm import trange
from colorama import Fore

for i in trange(int(1e7), bar_format='{l_bar}%s{bar}%s{r_bar}' % (Fore.BLUE, Fore.RESET)):
    pass

This turns the progress bar blue.

Guess you like

Origin blog.csdn.net/liuyukuan/article/details/128758647