Python show progress bar for looping code

Table of contents

1. tqdm library

2. alive_progress library

3. progressbar library


1. tqdm library

tqdm is a fast, extensible Python progress bar that can add a progress message in a long Python loop

import time
from tqdm import trange

for i in trange(100):
    # do something
    time.sleep(0.5)

insert image description here

2. alive_progress library

alive_progress is a dynamic real-time display progress bar library

import time
from alive_progress import alive_bar

# 假设需要执行100个任务
with alive_bar(100) as bar:
    for item in range(100):  # 遍历任务
        # 假设这代码部分需要0.5s
        time.sleep(0.5)
        bar()  # 显示进度

insert image description here

3. progressbar library

import time
from progressbar import ProgressBar, Percentage, Bar, Timer, ETA, FileTransferSpeed

widgets = ['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ', FileTransferSpeed()]
progress = ProgressBar(widgets=widgets)
for i in progress(range(100)):
    # do something
    time.sleep(0.05)

insert image description here

Guess you like

Origin blog.csdn.net/qq_45100200/article/details/131961224