Categories We Write About

Create progress bars in the terminal

You can create progress bars in the terminal using various programming languages. Here’s how to do it in Python, which is one of the simplest and most commonly used methods for terminal progress bars.

Method 1: Using tqdm (Recommended)

tqdm is a popular and easy-to-use library for progress bars.

Install it:

bash
pip install tqdm

Example usage:

python
from tqdm import tqdm import time for i in tqdm(range(100)): time.sleep(0.05)

This will display a neat progress bar like:

shell
46%|██████████████████▌ | 46/100 [00:02<00:03, 15.39it/s]

Method 2: Manual Progress Bar in Python

If you want to avoid external libraries, you can manually create a progress bar.

Example:

python
import time import sys def progress_bar(progress, total, length=50): percent = 100 * (progress / float(total)) bar = '█' * int(length * progress // total) + '-' * (length - int(length * progress // total)) sys.stdout.write(f'r|{bar}| {percent:.2f}%') sys.stdout.flush() for i in range(101): progress_bar(i, 100) time.sleep(0.05) print() # New line after completion

This prints a smooth, customizable progress bar in the terminal.


Method 3: Using rich for Fancy Progress Bars

The rich library offers beautiful and modern terminal output.

Install it:

bash
pip install rich

Example:

python
from rich.progress import track import time for task in track(range(100), description="Processing..."): time.sleep(0.05)

This shows a stylized progress bar with animations and colors.


Summary

MethodLibrary RequiredLookUse Case
tqdmYesSimpleLightweight and flexible
ManualNoCustomizableMaximum control, no dependencies
richYesFancyStylish UI for terminals

Choose based on your style and dependency preferences.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About