Authored by Daan Wynen

A way to use tqdm and a sane python debugger at the same time


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

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().

Edited 612 Bytes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment