3 ms·
Something like this should work: import os import sys from tqdm import tqdm as _tqdm def tqdm(*args, **kwargs): try: disab
by zbentley 5y ago
Something like this should work:
import os
import sys
from tqdm import tqdm as _tqdm
def tqdm(*args, **kwargs):
try:
disable = bool(int(os.environ['NO_PROGRESSBARS']))
except KeyError:
disable = not sys.stdout.isatty()
except (ValueError, TypeError):
disable = False
kwargs.setdefault('disable', disable)
return _tqdm(*args, **kwargs)
Then import that instead of tqdm.tqdm.
sys.stdout.isatty() isn't a perfect answer to what people ask when they want to know "am I running in an automated environment, or is a human user looking at my output?", but it's close. More nuance is available online.
- enriquto 5y agoThanks for the answer! > import that instead of tqdm.tqdm But it's not me who is importing tqdm to begin with! I call many programs in parallel from shell scripts (out of my control) and they all call tqdm individually. I need to stop tqdm output from outside these programs. Your code should be part of tqdm itself, not written by individual programmers. > am I running in an automated environment, or is a human user looking at my output? But I want to stop tqdm output precisely because I'm a human looking at it. If you have more than one or two progress bars simultaneously, it becomes useless clutter. As much as I like to use tqdm myself for my programs, I'm sad that as tqdm becomes more and more popular, my terminal output becomes more and more cluttered, to an absurd amount. Piping the output to a file does not help and is totally the wrong idea. I'm precisely interested in seeing--in real time--the part of the output that does not come from tqdm, such as warnings and errors.
- zbentley 5y agoThat's a reasonable request. Discussion about a feature along those lines seems to be happening in https://github.com/tqdm/tqdm/issues/614 https://github.com/tqdm/tqdm/issues/614; perhaps you could weigh in there?