TQDM多行彩色进度条打印

时间:2019-10-10 17:57:23

标签: python tqdm

我不确定使用bar_format选项添加颜色时,为什么我的TQDM进度条会分成多行。这似乎也与迭代次数有关,因为当我运行相同的代码时,只有10次迭代而不是1389次,它不会拆分(参见图片)。另外,当我运行相同的代码而不修改条形的颜色时,效果很好。

Problem

Fewer iterations

No color

from tqdm import tqdm
from colorama import Fore

dnames = [...]  # List of directories
cmap = [  # List of colors, same length as `dnames`
    '\x1b[38;5;231m',
    '\x1b[38;5;194m',
    '\x1b[38;5;151m',
    '\x1b[38;5;114m',
    '\x1b[38;5;71m',
    '\x1b[38;5;29m',
    '\x1b[38;5;22m',
    '\x1b[38;5;22m',
    '\x1b[38;5;22m',
    '\x1b[38;5;22m'
    # ...may include many more colors
]

# Initialize progress bar and color variable
pbar = tqdm(dnames, unit='dir')
current_color = None

for i, dname in enumerate(dnames):

    # Update color of pbar if different from last iteration
    if current_color != cmap[i]:
        pbar.bar_format = "{l_bar}%s{bar}%s{r_bar}" % (cmap[i], Fore.RESET)
        current_color = cmap[i]

    # For loop body goes here

    # Update pbar
    pbar.update(1)

pbar.close()

1 个答案:

答案 0 :(得分:0)

已修复tqdm>=4.41.1

在相关说明中,最新版本tqdm>=4.50.0添加了一个colour参数来简化此操作