Track Your Python Task Progress with tqdm: A Step-by-Step Tutorial

Mehedi Khan
Django Unleashed
Published in
4 min readAug 13, 2024

--

Track Your Python Task Progress with tqdm

The tqdm package in Python is a great tool for adding progress bars to your loops, making it easy to monitor the progress of your tasks. This tutorial will guide you through the basics of using tqdm and show you some common use cases.

1. Installing tqdm

First, you need to install tqdm if you haven't already. You can install it using pip:

pip install tqdm
Installing tqdm

2. Basic Usage

The most common use of tqdm is to wrap a loop to display a progress bar. Here’s a simple example:

from tqdm import tqdm
import time


for i in tqdm(range(100)):
time.sleep(0.1) # Simulating a task that takes fewpy time

This code will display a progress bar that updates as the loop progresses.

Basic Usage of tqdm

3. Customizing the Progress Bar

You can customize the progress bar to suit your needs:

  • Change the Description:
from tqdm import tqdm
import time


for i in tqdm(range(100), desc="Processing"):
time.sleep(0.1)
Customizing the Progress Bar with Description
  • Adjust the Bar Length:
from tqdm import tqdm
import time

for i in tqdm(range(100), desc="Processing", ncols=75):
time.sleep(0.1)
Customizing the Progress Bar with Bar Length
  • Change the Update Interval:
from tqdm import tqdm
import time

for i in tqdm(range(100), desc="Processing", mininterval=0.5):
time.sleep(0.1)
Customizing the Progress Bar with Update Interval

4. Using tqdm with Functions

You can use tqdm to track the progress of functions that process items in an iterable:

from tqdm import tqdm
import time


def process_item(item):
time.sleep(0.1)

items = range(100)
for item in tqdm(items, desc="Processing Items"):
process_item(item)
Using tqdm with Functions

5. Using tqdm with Pandas

tqdm can be easily integrated with pandas to track the progress of operations:

Note: You need to pandas install it using pip:

pip install pandas
install pandas
import pandas as pd
from tqdm import tqdm


tqdm.pandas()

df = pd.DataFrame({"A": range(100)})
df['B'] = df['A'].progress_apply(lambda x: x**2)
Using tqdm with Pandas

This will show a progress bar as the apply function processes each item.

6. Nested Progress Bars

You can use tqdm to create nested progress bars for more complex tasks:

from tqdm import tqdm
import time


for i in tqdm(range(10), desc="Outer Loop"):
for j in tqdm(range(100), desc="Inner For Loop", leave=False):
time.sleep(0.01)
Nested Progress Bars

7. Using tqdm in Jupyter Notebooks

If you’re working in a Jupyter Notebook, you can use tqdm with the tqdm.notebook module:

Note: Neet to install ipywidgets

pip install ipywidgets
from tqdm.notebook import tqdm
import time


for i in tqdm(range(100)):
time.sleep(0.1)

8. Tracking File Downloads

tqdm is often used to monitor file downloads. Here's an example using requests:

pip install requests
import requests
from tqdm import tqdm

url = "https://example.com/largefile.zip"
response = requests.get(url, stream=True)

total_size = int(response.headers.get('content-length', 0))
block_size = 1024 # 1 Kibibyte

with open("largefile.zip", "wb") as file, tqdm(
desc="Downloading",
total=total_size,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in response.iter_content(block_size):
bar.update(len(data))
file.write(data)

Note: You need to requests install it using pip

9. Conclusion

tqdm is a versatile and easy-to-use tool that can enhance your Python scripts by providing real-time feedback on the progress of loops and other tasks. Whether you're processing large datasets, downloading files, or running complex simulations, tqdm can help you keep track of what's happening in your code.

Thank you for reading! If you notice any mistakes or have suggestions for improvement, please leave a comment below.

If you enjoyed this post, please click the 👏 button to help others discover it. You can also follow me on:

GitHub | daily.dev | LinkedIn | YouTube

More Library

Python

12 stories

--

--

Django Unleashed
Django Unleashed

Published in Django Unleashed

Unleashing the Full Potential of Web Development

Mehedi Khan
Mehedi Khan

Written by Mehedi Khan

I'm a Software engineer. I'm comfortable with Python, Django, and Full-stack Web Development. Follow To Support Me On Medium 🫠

No responses yet