Advertisement
Python253

loading_bar

Jun 21st, 2024
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: loading_bar.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script displays a loading progress bar in the terminal.
  10.    - The progress bar shows the progress of an iterable task with a colored visual representation.
  11.    - The `colorama` module is used to display the bar in green while keeping the percentage data in the default terminal color.
  12.  
  13. Requirements:
  14.    - Python 3.x
  15.    - The Following Modules:
  16.        - sys
  17.        - time
  18.        - random
  19.        - colorama
  20.  
  21. Functions:
  22.    - colored_progress_bar(iterable, prefix='', size=42):
  23.        Displays a colored progress bar for an iterable.
  24.  
  25. Usage:
  26.    - Ensure Python 3.x is installed.
  27.    - Install required modules using pip (if not already installed):
  28.      
  29.      EXAMPLE:
  30.          pip install colorama
  31.  
  32.    - Run the script:
  33.      
  34.      EXAMPLE:
  35.          python loading_bar.py
  36.  
  37. Additional Notes:
  38.    - The script includes a short delay before exiting to ensure the completion message is visible.
  39.    - The progress bar simulates varying speeds for each step in the progress.
  40. """
  41.  
  42. import sys
  43. import time
  44. import random
  45. from colorama import init, Fore
  46.  
  47. # Initialize colorama to handle colored text in the terminal
  48. init(autoreset=True)
  49.  
  50. def colored_progress_bar(iterable, prefix='', size=42):
  51.     """
  52.    Displays a colored progress bar for an iterable.
  53.  
  54.    Args:
  55.        iterable (iterable): An iterable object to iterate over.
  56.        prefix (str): Prefix text for the progress bar.
  57.        size (int): Size of the progress bar (number of characters).
  58.    """
  59.     count = len(iterable)  # Total number of items in the iterable
  60.    
  61.     for i, item in enumerate(iterable):
  62.         progress = i + 1  # Current progress count
  63.         percent = progress / count * 100  # Current progress in percentage
  64.         bar_fill = int(size * progress / count)  # Number of filled blocks in the bar
  65.        
  66.         # Construct the progress bar string
  67.         bar = f"{prefix}[{Fore.GREEN}{'█' * bar_fill}{Fore.RESET}{' ' * (size - bar_fill)}]"
  68.         percentage_data = f"{progress}/{count} ({percent:.2f}%)"  # Progress data string
  69.         indent = "\t\t"  # Two tabs for indentation
  70.        
  71.         # Print the progress bar with progress data
  72.         print(f"{bar} {indent}{percentage_data}", end='\r', flush=True)
  73.        
  74.         # Simulate varying speeds for each step in the progress
  75.         time.sleep(random.uniform(0.02, 0.1))
  76.  
  77.     print()  # Move to a new line after completion to clean up the final progress bar
  78.  
  79. if __name__ == '__main__':
  80.     # Run the progress bar for a range of 79 iterations
  81.     colored_progress_bar(range(79), prefix='Loading:', size=42)
  82.    
  83.     # Print completion message after the progress bar finishes
  84.     print("\nAll processes completed!\n\n\t Exiting Program...   GoodBye!", flush=True)
  85.    
  86.     # Short delay before exiting to ensure the message is read
  87.     time.sleep(1)
  88.    
  89.     # Exit the program with status code 0 indicating successful execution
  90.     sys.exit(0)
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement