python模块| tqdm模块详解
基础应用
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.01)
from tqdm import trange
import time
# trange是同样的效果
for i in trange(100):
time.sleep(0.01)
只需要使用把可迭代对象iterator转化成tqdm对象即可,同样可以进行迭代,同时会在终端打印进度条。
63%|██████▎ | 63/100 [00:00<00:00, 64.55it/s]
100%|██████████| 100/100 [00:01<00:00, 64.49it/s]
参数
desc设置标题内容
from tqdm import tqdm
import time
for i in tqdm(range(100), desc='数字迭代'):
time.sleep(0.01)
数字迭代: 48%|████▊ | 48/100 [00:00<00:00, 58.71it/s]
数字迭代: 49%|████▉ | 49/100 [00:00<00:00, 64.24it/s]
数字迭代: 100%|██████████| 100/100 [00:01<00:00, 63.87it/s]
ncols设置进度条总长度
from tqdm import tqdm
import time
for i in tqdm(range(100), ncols=100):
time.sleep(0.01)
100%|██████████| 100/100 [00:01<00:00, 63.66it/s]
100%|█████████████████████████████████████████████████████████████| 100/100 [00:01<00:00, 63.34it/s]
# 这是默认和设置的对比
postfix设置进度条显示信息
from tqdm import tqdm
import time
for i in tqdm(range(100),postfix='数字进度条'):
time.sleep(0.01)
65%|██████▌ | 65/100 [00:01<00:00, 58.98it/s, 数字进度条]
100%|██████████| 100/100 [00:01<00:00, 60.08it/s, 数字进度条]
total设置总共需要达到的进度
from tqdm import tqdm
import time
for i in tqdm(range(100), total=10000):
time.sleep(0.01)
设置需要达到的进度
0%| | 48/10000 [00:00<02:35, 64.16it/s]
1%| | 100/10000 [00:01<02:35, 63.54it/s]
# 最后即使完成100次,还行没达到100%
update手动更新次数
from tqdm import tqdm
import time
progress = tqdm(range(100), total=10000)
for i in progress:
time.sleep(0.01)
progress.update(10)
设置每次执行增加的进度
4%|▎ | 350/10000 [00:00<00:14, 647.21it/s]
9%|▊ | 855/10000 [00:01<00:11, 766.10it/s]
1%| | 100/10000 [00:01<02:33, 64.66it/s]
# 执行完还是会回到实际的进度占比
unit设置单位
from tqdm import tqdm
import time
progress = tqdm(range(100), unit='次')
for c in progress:
time.sleep(0.2)
26%|██▌ | 26/100 [00:05<00:15, 4.91次/s]
59%|█████▉ | 59/100 [00:12<00:08, 4.81次/s]
67%|██████▋ | 67/100 [00:13<00:06, 4.82次/s]
76%|███████▌ | 76/100 [00:15<00:05, 4.78次/s]
85%|████████▌ | 85/100 [00:17<00:03, 4.88次/s]
方法
可以使用with作为上下文管理器
from tqdm import tqdm
import time
with tqdm(range(100)) as progress:
for i in progress:
time.sleep(0.01)
63%|██████▎ | 63/100 [00:00<00:00, 64.55it/s]
100%|██████████| 100/100 [00:01<00:00, 64.49it/s]
set_description设置查看每次处理的数据
from tqdm import tqdm
import time
progress = tqdm(range(100))
for c in progress:
time.sleep(0.2)
progress.set_description(f"The number is {c} ")
set_description即可在进度条前面展示目前进行操作的数据
The number is 5 : 6%|▌ | 6/100 [00:01<00:19, 4.91it/s]
The number is 51 : 52%|█████▏ | 52/100 [00:10<00:09, 4.84it/s]
The number is 63 : 64%|██████▍ | 64/100 [00:13<00:07, 4.83it/s]
The number is 75 : 76%|███████▌ | 76/100 [00:15<00:04, 4.89it/s]
The number is 99 : 100%|██████████| 100/100 [00:20<00:00, 4.87it/s]
set_postfix设置进度条显示信息
import random
from tqdm import tqdm
import time
progress = tqdm(range(100))
for c in progress:
time.sleep(0.2)
progress.set_postfix(rand=random.random(), title='数字标题栏')
39%|███▉ | 39/100 [00:07<00:12, 4.94it/s, rand=0.282, title=数字标题栏]
51%|█████ | 51/100 [00:10<00:09, 4.94it/s, rand=0.475, title=数字标题栏]
66%|██████▌ | 66/100 [00:13<00:06, 4.92it/s, rand=0.171, title=数字标题栏]
write输出信息
import random
from tqdm import tqdm
import time
progress = tqdm(range(100))
for c in progress:
time.sleep(0.2)
progress.set_postfix(rand=random.random(), str='数字标题栏')
tqdm.write(f'输出信息: {c}')
1%| | 1/100 [00:00<00:20, 4.89it/s, rand=0.4, str=数字标题栏]输出信息: 0
2%|▏ | 2/100 [00:00<00:19, 4.94it/s, rand=0.4, str=数字标题栏]输出信息: 1
4%|▍ | 4/100 [00:00<00:19, 4.90it/s, rand=0.488, str=数字标题栏]输出信息: 3
27%|██▋ | 27/100 [00:05<00:14, 4.94it/s, rand=0.496, str=数字标题栏]输出信息: 26
close 清楚进度条
结束进度条,释放内存,一般用在程序结束
本文详细介绍了Python的tqdm模块如何创建和定制进度条,包括设置标题、进度条长度、后缀信息、总进度、更新频率、单位以及使用上下文管理器等功能。
6万+

被折叠的 条评论
为什么被折叠?



