Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Visual Progress Indicators in Python with tqdm

Tech 1

Overview of tqdm

tqdm is a utility for Python that enables the integration of smart progress bars within loops. It works by wrapping any iterable, automatically calculating the estimated time of completion and the processing rate while displaying a visual bar in the terminal.

Installation

The libray can be installed via the standard Python package manager:

pip install tqdm

Automatic Progress for Iterables

The simplest way to use tqdm is by wrapping an existing iterable. This requires minimal changes to your existing code structure.

from tqdm import tqdm
import time

# Wrapping a list of elements
data_points = range(50)
for point in tqdm(data_points):
    time.sleep(0.1)

For numerical ranges specifically, trange offers a more concise syntax compared to wrapping range():

from tqdm import trange
import time

for i in trange(40):
    time.sleep(0.05)

Dynamic Description Updates

To provide context during execution, such as showing currrent status messages or metadata, you can modify the progress bar's description dynamically.

from tqdm import trange
import time

progress_bar = trange(10)
for step in progress_bar:
    time.sleep(0.3)
    # Update the text displayed next to the bar
    progress_bar.set_description(f"Currently processing batch {step + 1}")

Manual Control

In scenarios where the progress is not tied to a simple loop or is based on external events like network packets or file chunks, tqdm can be managed manually using a context manager and the update method.

from tqdm import tqdm
import time

total_tasks = 1000
with tqdm(total=total_tasks) as pbar:
    # Simulate processing chunks of work
    for _ in range(10):
        time.sleep(0.2)
        # Increment the progress manually by 100 units
        pbar.update(100)

Command Line Integration

Beyond Python scripts, tqdm can be utilized directly in the shell to monitor piped data streams. This is useful for long-running CLI operaitons.

find . -name '*.py' -type f -exec cat {} + | tqdm --unit lines --unit_scale --total 500000 > /dev/null

Detailed breakdown of the shell command:

  • find . -name '*.py' -type f: Recursively searches for Python source files.
  • -exec cat {} +: Reads and concatenates the content of all found files.
  • |: Pipes the output stream into tqdm.
  • tqdm --unit lines --unit_scale --total 500000: Processes the incoming data. The --unit flag labels the measurements, while --unit_scale formats large numbers (e.g., using 'k' for thousands) for better readability. The --total flag provides the target value needed to calculate the percentage and ETA.
  • > /dev/null: Redirects the standard output to discard the file content, allowing only the progress bar (which is sent to stderr) to be visible in the terminal.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.