'TQDM colored progress bar printing on multiple lines
I'm not sure why my TQDM progress bar is splitting into multiple rows when I add color using the bar_format
option. It seems to have to do with the number of iterations as well, because when I run the same code with only 10 iterations instead of 1389 it does not split (see images). Also, when I run the same code without modifying the color of the bar, it works fine.
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()
Solution 1:[1]
Your code is working perfectly fine for me, the other answer about your tqdm
version might help. TL;DR You need to update
For anyone wondering, here's how to set the color of the bar:
from tqdm import tqdm
from time import sleep
for i in tqdm(range(100), colour="red"):
sleep(0.001)
valid choices of colour: [hex (#00ff00), BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE]
Documentation:
Solution 2:[2]
Fixed in tqdm>=4.41.1.
Also on a related note, the latest version tqdm>=4.50.0 adds a colour
argument to make this easier
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | casper.dcl |