TQDM: 10x The UX of your Python Scripts

The one-line library that’ll change your long-running script game forever.

Adam Marshall
3 min readAug 8, 2018
No, this isn’t TQDM. But yes, it does look really nice!

In machine learning & other data heavy jobs you tend to find yourself in the terminal or in a Jupyter notebook (prefferably a jupyter notebook) a lot waiting for scripts to finish. Huge-ass for loops that take ages, list comprehensions that attempt to calculate the size of the universe with every step — or even just really long ass lists of instructions that individually don’t take a long time but in summation make you wait for eons.

Unless you have the unwavering patience and self belief of a fucking transcendent angelic version of The Rock, the chances are you don’t just run the script without any sort of feedback and sit there waiting for minutes/hours/days while it executes.

So you’ve probably found yourself in this situation before:

I pray for your sanity if you normally have the terminal font at this size. Dear lord. Did it for the shot.

Spam, spam, spam, spam, spam……. Not only does doing this many print statements waste CPU cycles but it feels really, really awkward. And if you have any other contextual print statements appearing during the loop, fat fucking chance of finding it.

What we need is a library that gives us progress bars! But not something that’s going to get in the way; we don’t want to have to change how we write loops just for the sake of a progress bar. When you think about it, we kind of want a progress bar wrapper around iterables.

Perhaps, something like this;

for x in progress(iterable):
print x

Wouldn’t that be amazing?? Then, we could do it in list comprehensions too:

[x for x in progress(iterable)]

….

Oh yeah, that’s what tqdm does!

TQDM, the built in function Python really should have.

TQDM is a really, really neat little python package that’s not only actively developed (by 40+ contributors) but also insanely easy to use. Once installed via pip install tqdm , and included in your project with

from tqdm import tqdm

You simply wrap that tqdm around whatever iterable you want, and you’re off to the races! With just six extra characters, and 60ns of overhead, you get all this:

  • A configurable progress bar (description text! ASCII!)
  • A count down
  • A time estimator
  • A speed tracker
  • Nested progress bars!!! WTF!!!

….. All without changing how your code works at all!

GET TO IT!

--

--