Progress bar and status logging in python with tqdm

Photo by Chris Ried on Unsplash

When you write write code, especially in machine learning, there will be inevitably the situation where you loop over a larger amount of data, for example when you train your CNN. Progress bars are helpful in showing you how much work has been completed and how much longer you have to wait. My favorite progress bar for python is tqdm, which lets you create simple progress bars with just a few lines of code. The github page gives a very good introduction into the basic use of tqdm. Here a simple example:

The problem with tqdm I had was that I want a progress bar for my neural network training but also log some statistics of the last iteration to the console. tqdm does not work nicely with print() , but tqdm offers a couple of ways to work around that.

A) Add information to the progress bar description

Each progress bar has a description field in the beginning of the line where you can add a short text, for example Training or Epochs. But since the line length is limited and the actual progress tracking with remaining time and iterations per second needs some space, too.

B) Use write()

For longer texts or text not related to a specific progress bar tqdm offers write() which logs text safely to the console. To be precise, it adds the text above the progress bars, so each line you ever logged using write() will be visible.

This is nice and all but not what I want from a compact progress and status logging. Fortunately tqdm can be hacked to display text in fixed positions.

C) Convert progress bars to status bars

The idea behind this “hack” is to use progress bars for the status logging by removing everything but the description field. This way we can specify the exact location of the status bar using the position argument when we create the progress bar. To remove all fields except the description field bar_format='{desc}' needs to be set. Now the progress bar shows only the description. To set the value of the description we need to use set_description_str() instead of set_description() as the latter adds a colon after the description text. Below is a toy example of a video to image converter that reads all videos from a folder and converts them to images. It uses progress bars to log the filename of video and frame as well as some data about the video file.

There you get a nice and compact overview on your status and progress:


  • Use position=n to give progress bars a fixed position
  • Use bar_format='{desc}' to remove all but the description from progress bars you want to use for logging
  • Set the logging content using set_description_str() to avoid the colon after the text

Machine learning and neuroscience | Coding python (and recently JS and Kotlin) | Building apps you love

Machine learning and neuroscience | Coding python (and recently JS and Kotlin) | Building apps you love