基础:
from tqdm import tqdm
import time
for i in tqdm(range(50)):
time.sleep(0.1)
100%|██████████| 50/50 [00:05<00:00, 9.93it/s]
升级版(在进度条中增加描述信息):
from tqdm import tqdm
import time
from random import random, randint
with tqdm(range(50)) as t:
for i in t:
# Description will be displayed on the left
t.set_description('下载速度 %i' % i)
# Postfix will be displayed on the right,
# formatted automatically based on argument's datatype
# t.set_postfix(loss=random(), gen=randint(1, 999), str='详细信息',lst=[1, 2])
time.sleep(0.1)
下载速度 49: 100%|██████████| 50/50 [00:05<00:00, 9.93it/s]
用字典dict来更新:
print(' ---------------- Test 2 --------------------- ')
time.sleep(2)
d = {}
with tqdm(range(50)) as t:
for i in t:
d['loss'] = i * 2
d['learn'] = i * 10
t.set_description('下载速度 %i' % i)
t.set_postfix(d)
time.sleep(0.1)
下载速度 49: 100%|██████████| 50/50 [00:05<00:00, 9.92it/s, loss=98, learn=490]
import time
from tqdm import tqdm
with tqdm(total=200) as pbar:
pbar.set_description('Processing:')
# total表示总的项目, 循环的次数20*10(每次更新数目) = 200(total)
for i in range(20):
# 进行动作, 这里是过0.1s
time.sleep(0.1)
# 进行进度更新, 这里设置10个
pbar.update(10)
Processing:: 100%|██████████| 200/200 [00:02<00:00, 91.94it/s]