You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为已完成脚本添加手动递增的progress bar?求示例

Manual Progress Bar with tqdm (Manual Step Increments)

Hey there! I totally get what you're looking for—most progress bar examples fixate on loops or file transfers, but manually triggering increments at specific script stages is such a common need. Let’s use tqdm (one of Python’s most flexible progress bar libraries) to implement exactly this.

Step 1: Install tqdm (if you haven’t already)

pip install tqdm

Step 2: Core Implementation

The trick is initializing the progress bar with a fixed total number of major script stages, then calling update(1) each time you finish a phase. Here’s a complete, relatable example:

from tqdm import tqdm
import time  # Just simulating work in each stage—replace with your actual code

# Define how many major stages your script has
total_stages = 4

# Initialize the progress bar with a clear label
progress_bar = tqdm(total=total_stages, desc="Script Progress")

# Stage 1: Load raw data
print("Loading dataset from disk...")
time.sleep(2)  # Replace with your data loading code
progress_bar.update(1)  # Push bar forward by 1 step

# Stage 2: Clean and preprocess data
print("Cleaning and transforming data...")
time.sleep(3)  # Replace with your preprocessing logic
progress_bar.update(1)

# Stage 3: Run core computation/model training
print("Training machine learning model...")
time.sleep(4)  # Replace with your core work
progress_bar.update(1)

# Stage 4: Save outputs
print("Saving results to file...")
time.sleep(2)  # Replace with your save logic
progress_bar.update(1)

# Clean up the progress bar (optional but tidy)
progress_bar.close()

print("✅ Script completed successfully!")

How It Works

  • tqdm(total=total_stages) tells the bar exactly how many steps to expect.
  • progress_bar.update(1) is your manual trigger—call it every time you finish a key phase to advance the bar.
  • The desc parameter adds a clear label so users know what the bar tracks.

Customization Tips

You can tweak the bar to fit your workflow:

  • Update the bar’s label mid-script with progress_bar.set_description("Preprocessing")
  • Add real-time details with progress_bar.set_postfix({"Current Task": "Removing Outliers"})
  • Adjust the bar’s appearance using bar_format (e.g., show elapsed time, percentage complete)

Example with customizations:

progress_bar = tqdm(total=total_stages, desc="Starting Up")

# After first stage:
progress_bar.set_description("Data Preprocessing")
progress_bar.set_postfix({"Status": "Cleaning Text"})
progress_bar.update(1)

This approach gives you full control over when the progress bar advances, so you can map it directly to your script’s critical milestones instead of relying on automatic loops or byte counts.

内容的提问来源于stack exchange,提问作者soundtechscott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:08:09