'Update description to iterable
For debugging, it is useful for me to have the iterable displayed as the description of the progress bar. For example:
import tqdm
mylist = ["this", "is", "a", "sentence"]
fmt = "{:" + str(max(len(i) for i in mylist)) + "s}"
pbar = tqdm.tqdm(mylist)
pbar.set_description(fmt.format(""))
for i in pbar:
pbar.set_description(fmt.format(i), refresh=True)
Now, my question, how can I derive from tqdm.tqdm such that all of this is done 'automatically'? I.e. such that I can simply write
# ... setting up the class ....
for i in mytqdm(mylist):
# ...
(Because I don't think it is possible in the library itself)
Solution 1:[1]
I'm having the same issue today, but I didn't know about the set_description method, so thank you a bunch!
This is one step closer to what you want, I think:
import tqdm
import time
mylist = ["this", "is", "a", "sentence"]
fmt = "{:" + str(max(len(i) for i in mylist)) + "s}"
pbar = tqdm.tqdm(mylist, desc=fmt.format(""))
for i in pbar:
pbar.set_description(fmt.format(i), refresh=True)
time.sleep(1)
I added a sleeper so that you can actually see the progress bar change. By setting desc in the creation of pbar, you can at least skip the first set_description, making it a bit more compact. I'm not sure there's a more compact form, since you'll need to call a function to update the description, so you'll need to have a pbar object rather than using the "for tqdm" syntax.
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 | Tom de Geus |
