Python显示循环代码的进度条

目录

1. tqdm库

2. alive_progress库

3. progressbar库


1. tqdm库

tqdm是一个快速,可扩展的Python进度条,可以在Python长循环中添加一个进度提示信息

import time
from tqdm import trange

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

在这里插入图片描述

2. alive_progress库

alive_progress是一个动态的实时显示进度条库

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()  # 显示进度

在这里插入图片描述

3. progressbar库

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)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45100200/article/details/131961224