Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: loading_bar.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script displays a loading progress bar in the terminal.
- - The progress bar shows the progress of an iterable task with a colored visual representation.
- - The `colorama` module is used to display the bar in green while keeping the percentage data in the default terminal color.
- Requirements:
- - Python 3.x
- - The Following Modules:
- - sys
- - time
- - random
- - colorama
- Functions:
- - colored_progress_bar(iterable, prefix='', size=42):
- Displays a colored progress bar for an iterable.
- Usage:
- - Ensure Python 3.x is installed.
- - Install required modules using pip (if not already installed):
- EXAMPLE:
- pip install colorama
- - Run the script:
- EXAMPLE:
- python loading_bar.py
- Additional Notes:
- - The script includes a short delay before exiting to ensure the completion message is visible.
- - The progress bar simulates varying speeds for each step in the progress.
- """
- import sys
- import time
- import random
- from colorama import init, Fore
- # Initialize colorama to handle colored text in the terminal
- init(autoreset=True)
- def colored_progress_bar(iterable, prefix='', size=42):
- """
- Displays a colored progress bar for an iterable.
- Args:
- iterable (iterable): An iterable object to iterate over.
- prefix (str): Prefix text for the progress bar.
- size (int): Size of the progress bar (number of characters).
- """
- count = len(iterable) # Total number of items in the iterable
- for i, item in enumerate(iterable):
- progress = i + 1 # Current progress count
- percent = progress / count * 100 # Current progress in percentage
- bar_fill = int(size * progress / count) # Number of filled blocks in the bar
- # Construct the progress bar string
- bar = f"{prefix}[{Fore.GREEN}{'█' * bar_fill}{Fore.RESET}{' ' * (size - bar_fill)}]"
- percentage_data = f"{progress}/{count} ({percent:.2f}%)" # Progress data string
- indent = "\t\t" # Two tabs for indentation
- # Print the progress bar with progress data
- print(f"{bar} {indent}{percentage_data}", end='\r', flush=True)
- # Simulate varying speeds for each step in the progress
- time.sleep(random.uniform(0.02, 0.1))
- print() # Move to a new line after completion to clean up the final progress bar
- if __name__ == '__main__':
- # Run the progress bar for a range of 79 iterations
- colored_progress_bar(range(79), prefix='Loading:', size=42)
- # Print completion message after the progress bar finishes
- print("\nAll processes completed!\n\n\t Exiting Program... GoodBye!", flush=True)
- # Short delay before exiting to ensure the message is read
- time.sleep(1)
- # Exit the program with status code 0 indicating successful execution
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement