在tqdm中构建一个带有后缀的嵌套进度条

时间:2018-06-05 13:00:42

标签: python progress-bar tqdm

我知道如何使用tqdm构建嵌套进度条。

from tqdm import trange 
from time import sleep
for i in trange(10, desc='1st loop'):
    for j in trange(5, desc='2nd loop', leave=False):
        for k in trange(100, desc='3nd loop'): sleep(0.01)

我现在还要如何将后缀和描述添加到栏中

from tqdm import trange 
from random import random, randint 
from time import sleep 
with trange(100) as t: 
    for i in t: 
         t.set_description('GEN %i' % i) 
         t.set_postfix(loss=random(),  gen=randint(1,999), str='h', lst=[1, 2])   
         sleep(0.1)

问题

如何在tqdm中为嵌套进度条添加说明和后缀?我想为每个嵌套栏添加独立的后缀。

1 个答案:

答案 0 :(得分:0)

嵌套效果很好

from tqdm import trange
from time import sleep

n_epochs, n_steps = 5, 100
with trange(1, n_epochs + 1, desc="All epochs") as epochs:
    for epoch in epochs:
        with trange(1, n_steps + 1, desc="Epoch {}/{}".format(epoch, n_epochs)) as steps:
            for step in steps:
                epochs.set_postfix(foo=epoch * n_steps + step)
                steps.set_postfix(bar="hello {}".format(step), baz=1 / step)
                sleep(0.01)