【问题标题】:TQDM colored progress bar printing on multiple lines多行 TQDM 彩色进度条打印
【发布时间】:2019-10-10 17:57:23
【问题描述】:

当我使用bar_format 选项添加颜色时,我不确定为什么我的 TQDM 进度条会分成多行。它似乎也与迭代次数有关,因为当我只用 10 次迭代而不是 1389 次运行相同的代码时,它不会分裂(见图)。此外,当我在不修改条形颜色的情况下运行相同的代码时,它可以正常工作。

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()

【问题讨论】:

    标签: python tqdm


    【解决方案1】:

    固定在tqdm>=4.41.1

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

    【讨论】:

      【解决方案2】:

      您的代码对我来说工作得很好,关于您的 tqdm 版本的其他答案可能会有所帮助。


      对于任何想知道的人,以下是设置条形颜色的方法:

      from tqdm import tqdm
      from time import sleep
      
      for i in tqdm(range(100), colour="red"):
          sleep(0.001)
      

      有效的颜色选择:[hex (#00ff00), BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE]

      文档:

      https://tqdm.github.io/docs/tqdm/

      【讨论】:

        猜你喜欢
        • 2019-08-15
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 2018-10-13
        • 2013-09-10
        • 1970-01-01
        相关资源
        最近更新 更多