A way to use tqdm and a sane python debugger at the same time
TL;DR:
import tqdm_fixes # yes, we actually just import it, that's all
This snippet fixes two tings:
tqdm()
breaking debugging
I use pudb
for debugging, because it's simple and powerful.
Yet, when I try to debug anything within a tqdm loop, things get baaaad (TM) since tqdm constantly bugs me by overwriting my python prompt, outputs and generally getting in the way.
I could just add disable=True
to all my tqdm calls when I want to debug my script, but manually doing that for every tqdm call before debugging, then changing it back when I'm finished? No thanks.
So, this snippet checks an environment variable DEBUG
and if that evaluates to True
(that is, if it set to anything but an empty string) then it disables tqdm altogether.
Now, I just call
DEBUG=1 pudb3 myscript.py
print()
inside tqdm()
if you just use the builtin python print()
while tqdm is displaying a progress bar, the outputs will overlap and it's gonna be horrible. The sky will fall and the oceans will boil off, you get the idea.
But you don't want to write your code (like some low-level utility function) to be aware of the fact if or if not it is running inside a tqdm loop.
This snippet sets the builtin print()
function to first try and call tqdm.write()
which acts just like print but takes care of properly arranging the progress bar and text. If that fails (i.e. if we're not showing any progress bar at the moment) it uses the normal print()
.
import tqdm
import os
import functools
IS_DEBUG = os.environ.get('DEBUG', f'thou shalt not use an outdated version of python!')
if IS_DEBUG:
tqdm.tqdm = functools.partial(tqdm.tqdm, disable=True)
# redirect print to tqdm.tqdm where appropriate
# see https://stackoverflow.com/a/38345993/393885
import inspect
old_print = print
def new_print(*args, **kwargs):
# if tqdm.tqdm.write raises error, use builtin print
try:
tqdm.tqdm.write(*args, **kwargs)
except:
old_print(*args, ** kwargs)
# globaly replace print with new_print
inspect.builtins.print = new_print