How to make tqdm play nice with jenkins?

2k views Asked by At

Love tqdm progress bar, but when I use it on jenkins, I keep getting a bunch of weird artifacts and too much bloat in stdout (specifically, omnipresence of [A). Is there a secret mode in tqdm to make it work nicely with jenkins? Bonus points for seamless detection of non-interactive shells like jenkins. Here is what my typical output looks like:

label: 0it [00:00, ?it/s][A
[A
 16%|#6        | 5378/33302 [36:28<2:30:49,  3.09it/s]
[A
 16%|#6        | 5379/33302 [36:29<2:36:46,  2.97it/s]
[A
...
1

There are 1 answers

0
casper.dcl On

I'd go with something like:

from tqdm import tqdm
import os

# try this
for i in tqdm(..., disable=None):
    ...

# alternative if the above doesn't work
for i in tqdm(..., disable=os.environ.get("JENKINS_HOME")):
    ...

# or even...
for i in tqdm(..., disable=os.environ.get("JENKINS_HOME") is not None):
    ...

Note that disable=None should automatically check things such as sys.stdout.isatty().

Unfortunately there's nothing that can be done about Jenkins not supporting CR (\r), a basic requirement of tqdm.

From https://tqdm.github.io/:

tqdm does not require any dependencies (not even curses!), just Python and an environment supporting carriage return \r and line feed \n control characters.