84👍
Example using the ‘disable’ parameter:
from tqdm import tqdm
import time
for i in tqdm(range(10), disable=True):
time.sleep(1)
64👍
There is a disable
argument which you can set to True
to silence any tqdm
output (and in fact it will totally skip the progress bar calculations too, not just the display).
To dynamically switch it, you can just add a commandline argument to your script that will define if disable
is set or not. This should work for both unit testing and cron.
- [Django]-Django – Overriding the Model.create() method?
- [Django]-How to display the current year in a Django template?
- [Django]-Paginate relationship in Django REST Framework?
64👍
This is a very common use case when you need to globally disable all tqdm
‘s output, desirably without changing the code in all places where it is used and which you probably do not control.
Starting version 4.66.0
Set environment variable TQDM_DISABLE=1
.
Note that exact value of the variable does not matter, it just should be a non-empty string.
Older versions
Users need to patch tqdm in order to stop polluting logs. One of the shortest ways I found is probably this:
from tqdm import tqdm
from functools import partialmethod
tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)
The idea is defaulting the already supported (but not sufficient) parameter of the initializer. It is not sufficient because you need to add it in each place where tqdm is instantiated, which you don’t want, that’s why we modify __init__
to make such a default.
The patch works independently of the order of imports and will affect all subsequently created tqdm
objects.
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Split views.py in several files
9👍
Use mock.patch
to replace tqdm
in the code that’s using it with something like this:
def notqdm(iterable, *args, **kwargs):
"""
replacement for tqdm that just passes back the iterable
useful to silence `tqdm` in tests
"""
return iterable
and in the test:
import mock
...
@mock.patch('tested_code_module.tqdm', notqdm)
def test_method(self):
...
- [Django]-Good open source django project for learning
- [Django]-What is "load url from future" in Django
- [Django]-Deploying Django with gunicorn and nginx
2👍
Since version 4.66.0, the default tqdm
arguments can be overridden using environment variables with the TQDM_
-prefix followed by the parameter name. This means you can disable tqdm
‘s output by setting the TQDM_DISABLE
environment variable to 1
before importing tqdm
or any modules that use it:
import os
os.environ["TQDM_DISABLE"] = "1"
# import tqdm and/or modules that use it here
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-Readonly models in Django admin interface?
- [Django]-How can I upgrade specific packages using pip and a requirements file?
0👍
Riffing off of this answer (thank you!), if you also use tqdm.write
, this code should work to suppress those as well:
MODULE_TQDM = "tested_code_module.tqdm"
class NoTQDM:
def __init__(self, iterable, *args, **kwargs):
self.iterable = iterable
def __iter__(self):
for item in self.iterable:
yield item
def write(self, *args, **kwargs):
pass
And then in the test:
from unittest import mock
...
@mock.patch(MODULE_TQDM, NoTQDM)
def test_method(self):
...
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-CharField with fixed length, how?
- [Django]-Django dynamic forms – on-the-fly field population?