使用python Tqdm 进度条库让你的python进度可视化

使用python Tqdm进度条库让你的python进度可视化

Tqdm在阿拉伯语表示进步,在西班牙语中表示我非常爱你。是一个快速,可扩展的Python进度条,可以在Python长循环中添加一个进度提示信息,用户只需要封装任意的迭代器tqdm(iterator)即可完成进度条。相比ProgressBar来说Tqdm的开销非常低,同时Tqdm可以在任何环境中不需要任何依赖运行。

Tqdm安装

可以使用pip安装稳定版本

pip install tqdm

也可以使用conda来安装最新的版本

conda install -c conda-forge tqdm

你可以使用如下代码测试是否安装成功:

from tqdm import tqdm
for i in tqdm(range(10000)):
    pass

同时你也可以通过shell命令操作

$ seq 9999999 | tqdm --unit_scale | wc -l
10.0Mit [00:02, 3.58Mit/s]
9999999

$ 7z a -bd -r backup.7z docs/ | grep Compressing | tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log
100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s]

Tqdm的用法

tqdm是非常通用的,并且可以以多种方式使用。下面给出三个主要部分。

1、基于可迭代

总结tqdm()各地可迭代:

import tqdm

text = ""
for char in tqdm(["a", "b", "c", "d"]):
    text = text + char

trange(i)是一个特殊的优化实例tqdm(range(i)):

for i in trange(100):
    pass

在循环外的实例化允许手动​​控制tqdm():

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    pbar.set_description("Processing %s" % char)

2、手册

tqdm()通过使用with语句对更新进行手动控制:

with tqdm(total=100) as pbar:
    for i in range(10):
        pbar.update(10)

如果提供了可选变量total(或可迭代len()),则显示预测统计信息。

with也是可选的(你可以分配tqdm()给一个变量,但在这种情况下,不要忘记del或close()在最后:

pbar = tqdm(total=100)
for i in range(10):
    pbar.update(10)
pbar.close()

3、Module

也许最好的用法tqdm是在脚本或命令行中。简单地插入tqdm(或python -m tqdm)之间的管道将穿过所有stdinstdout在打印进度stderr

下面的例子演示了当前目录中所有Python文件中的行数,包括时序信息。

$ time find . -name '*.py' -exec cat {} ; | wc -l
857365

real    0m3.458s
user    0m0.274s
sys     0m3.325s

$ time find . -name '*.py' -exec cat {} ; | tqdm | wc -l
857366it [00:03, 246471.31it/s]
857365

real    0m3.585s
user    0m0.862s
sys     0m3.358s

请注意,tqdm也可以指定通常的参数。

$ find . -name '*.py' -exec cat {} ; |
    tqdm --unit loc --unit_scale --total 857366 >> /dev/null
100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]

备份一个大目录?

$ 7z a -bd -r backup.7z docs/ | grep Compressing |
    tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log
100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s]

tqdm项目地址
原文出处: https://ptorch.com/news/170.html
 

关注公众号 海量干货等你
原文地址:https://www.cnblogs.com/sowhat1412/p/12734206.html