Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set tqdm.disable from os.environ #1061

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ Parameters
the meter. The fallback is to use ASCII characters " 123456789#".
* disable : bool, optional
Whether to disable the entire progressbar wrapper
Setting os.environ['TQDM_DISABLE'] = 1 will disable the entire
progressbar wrapper. It overwrites this flag.
[default: False]. If set to None, disable on non-TTY.
* unit : str, optional
String that will be used to define the unit of each iteration
Expand Down
34 changes: 34 additions & 0 deletions tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,40 @@ def test_disable():
assert our_file.getvalue() == ''


def test_disable_environ(monkeypatch):
""" Test that disable can be done with environment variable"""

# Sets a os.environ flag during this test. This should silence the logging
monkeypatch.setenv('TQDM_DISABLE', '1')

with closing(StringIO()) as our_file:
instance = tqdm(_range(3), file=our_file)
assert instance.disable is True

for _ in instance:
pass

assert our_file.getvalue() == ''

with closing(StringIO()) as our_file:
instance = tqdm(_range(3), file=our_file, disable=False)
assert instance.disable is True

for _ in instance:
pass

assert our_file.getvalue() == ''

with closing(StringIO()) as our_file:
instance = tqdm(_range(3), file=our_file, disable=True)
assert instance.disable is True

for _ in instance:
pass

assert our_file.getvalue() == ''


def test_infinite_total():
"""Test treatment of infinite total"""
with closing(StringIO()) as our_file:
Expand Down
8 changes: 7 additions & 1 deletion tqdm/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from time import time
from warnings import warn
import sys
import os

__author__ = "https://github.com/tqdm/tqdm#contributions"
__all__ = ['tqdm', 'trange',
Expand Down Expand Up @@ -886,7 +887,9 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
If unspecified or False, use unicode (smooth blocks) to fill
the meter. The fallback is to use ASCII characters " 123456789#".
disable : bool, optional
Whether to disable the entire progressbar wrapper
Whether to disable the entire progressbar wrapper.
Setting os.environ['TQDM_DISABLE'] = 1 will disable the entire
progressbar wrapper. It overwrites this flag.
[default: False]. If set to None, disable on non-TTY.
unit : str, optional
String that will be used to define the unit of each iteration
Expand Down Expand Up @@ -966,6 +969,9 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,

file = DisableOnWriteError(file, tqdm_instance=self)

if 'TQDM_DISABLE' in os.environ:
disable = True

if disable is None and hasattr(file, "isatty") and not file.isatty():
disable = True

Expand Down