Track Your Python Task Progress with tqdm
: A Step-by-Step Tutorial
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
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.
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)
- Adjust the Bar Length:
from tqdm import tqdm
import time
for i in tqdm(range(100), desc="Processing", ncols=75):
time.sleep(0.1)
- 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)
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)
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
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)
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)
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.