Understanding Python third-party libraries in one article (1)-tqdm library

Introduction

A library that displays a looping progress bar. Taqadum means progress in Arabic. tqdm can add a progress prompt message in a long loop. Users only need to encapsulate any iterator tqdm (iterator), which is a fast and extensible progress bar tool library.

  • First, we import tqdm library, time library, random random number library
from tqdm import tqdm,trange
import time
from random import random,randint

Run based on iterative objects

  • Example 1
for i in trange(100):
    time.sleep(0.05)
  • Example 2
for i in tqdm(range(100),desc='Processing'):
    time.sleep(0.05)
  • Example 3
dic = ['a','b','c','d','e']
pbar = tqdm(dic)
for i in pbar:
    # 通过tqdm提供的set_description方法可以实时查看每次处理的数据
    pbar.set_description('Processing %s' % i)
    time.sleep(0.2)

Set update and total related parameters

# 通过update方法可以控制每次进度条更新的进度
# #total参数设置进度条的总长度
with tqdm(total=100) as pbar:
    for i in range(100):
        time.sleep(0.05)
        # 每次更新进度条的长度
        pbar.update(1)

Custom progress bar display information

with trange(100) as t:
    for i in t:
        # 设置进度条左边显示的信息
        t.set_description("GEN % i" % i)
        # 设置进度条右边显示的信息
        t.set_postfix(loss = random(),gen = randint(1,999),str = 'h',lst = [1,2])
        time.sleep(0.1)

Multi-layer circular progress bar

for i in tqdm(range(20),ascii=True,desc='1st loop'):
    for j in tqdm(range(10),ascii = True,desc='2st loop'):
        time.sleep(0.1)

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/114364464