杰瑞科技汇

Python进度条如何实现?

Of course! A process bar (or progress bar) is an essential tool in Python for showing the progress of a long-running task. It makes your scripts more user-friendly by providing visual feedback.

Here’s a comprehensive guide covering the most popular and effective methods, from the standard library to powerful third-party packages.


The Simplest Way: tqdm (Highly Recommended)

tqdm is the most popular and feature-rich progress bar library for Python. It's extremely easy to use and works seamlessly in almost any situation, including notebooks, command lines, and multi-processing.

Installation:

pip install tqdm

Basic Usage

You just need to wrap any iterable (like a list or a range) with tqdm().

from tqdm import tqdm
import time
# A simple list
data = range(100)
# Wrap the iterable with tqdm
for item in tqdm(data, desc="Processing items"):
    # Simulate some work
    time.sleep(0.05) 

Output in a terminal:

Processing items: 100%|██████████| 100/100 [00:05<00:00, 19.98it/s]

Key Features of tqdm

  • desc: A short description for the progress bar.
  • total: The total number of iterations. Useful if the iterable doesn't have a known length.
  • unit: The unit to display (e.g., 'it' for iterations, 'B' for bytes).
  • unit_scale: If True, will scale the units (e.g., 1000 -> 1K).
  • ncols: The width of the progress bar in characters.
  • file: By default, it prints to sys.stderr. You can change it to sys.stdout.

Example with more options:

from tqdm import tqdm
import time
total_items = 500
pbar = tqdm(total=total_items, desc="Downloading Data", unit="B", unit_scale=True, ncols=80)
for i in range(total_items):
    # Simulate downloading a chunk of data
    time.sleep(0.01)
    pbar.update(1) # Manually update the progress
pbar.close() # Important to close the bar

Usage in Pandas

tqdm integrates beautifully with Pandas. You can use it as a context manager to apply a progress bar to any operation.

import pandas as pd
from tqdm import tqdm
# Create a large DataFrame
df = pd.DataFrame({'col': range(1, 100001)})
# Apply a function to each row with a progress bar
tqdm.pandas(desc="Processing DataFrame")
df['processed_col'] = df['col'].progress_apply(lambda x: x ** 2)
print(df.head())

The Built-in tqdm Alternative: rich.progress

The rich library is famous for its beautiful terminal output. Its progress bar module is a fantastic, modern alternative to tqdm.

Installation:

pip install rich

Basic Usage

rich.progress uses a Progress context manager, which gives you more control.

from rich.progress import Progress
import time
tasks = ["Processing...", "Downloading...", "Uploading..."]
with Progress() as progress:
    for task_name in tasks:
        # Add a new task to the progress display
        task = progress.add_task(f"[cyan]{task_name}", total=100)
        # Simulate work
        for i in range(100):
            progress.update(task, advance=1) # Advance the task by 1
            time.sleep(0.02)

Output in a terminal:

Processing...  ████████████████████████████████████████████████████████████ 100/100  [100%]
Downloading... ████████████████████████████████████████████████████████████ 100/100  [100%]
Uploading...   ████████████████████████████████████████████████████████████ 100/100  [100%]

Key Features of rich.progress

  • Beautiful Styling: Colors, bars, and text are automatically styled.
  • Live Statistics: Shows time elapsed, time remaining, and items per second.
  • Multiple Tasks: Designed to track multiple tasks simultaneously, which is great for complex scripts.
  • Tables: Can integrate progress bars into terminal tables.

The "From Scratch" Method: Using sys.stdout

If you want to avoid third-party libraries, you can build a simple progress bar using Python's built-in sys module. This is great for learning the basics but lacks the features and robustness of tqdm or rich.

import sys
import time
total = 100
for i in range(total + 1):
    # Calculate the progress percentage
    percent = (i / total) * 100
    # Create the bar string
    bar_length = 40
    filled_length = int(round(bar_length * i / total))
    bar = '█' * filled_length + '-' * (bar_length - filled_length)
    # The carriage return '\r' moves the cursor to the beginning of the line
    sys.stdout.write(f'\rProgress: |{bar}| {percent:.1f}%')
    sys.stdout.flush() # Ensure it prints immediately
    # Simulate work
    time.sleep(0.05)
# Print a newline at the end to move to the next line
sys.stdout.write('\n')

How it works:

  • sys.stdout.write(): Prints text without a newline at the end.
  • \r: The "carriage return" character moves the cursor to the start of the current line. Overwriting the line creates the illusion of an updating bar.
  • sys.stdout.flush(): Forces Python to print the buffer immediately, so you see the updates in real-time.

Summary and Comparison

Method Pros Cons Best For
tqdm - Extremely easy to use.
- Works with any iterable.
- Great Pandas integration.
- Highly customizable.
- Works in notebooks and terminals.
- Slightly less "flashy" than rich. Almost everything. It's the standard for a reason.
rich.progress - Beautiful, modern terminal output.
- Excellent for tracking multiple tasks.
- Part of a larger, powerful library.
- Slightly more verbose (requires with block).
- Might be overkill for a simple script.
Scripts where visual appeal and multi-task tracking are important.
sys.stdout - No external dependencies.
- Good for learning how progress bars work.
- Manual and error-prone.
- Less flexible (hard to add stats like ETA).
Quick scripts where you can't install packages or for educational purposes.

Final Recommendation

For any serious project, start with tqdm. It's the industry standard for a reason: it's simple, powerful, and "just works". If you find yourself needing more visually appealing output or managing many concurrent tasks, then explore rich.progress.

分享:
扫描分享到社交APP
上一篇
下一篇