Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Progress bar.py #1950

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Piombacciaio
Copy link
Contributor

Created a simple program that displays a colored progress bar on the terminal window

Progress bar.py Outdated
@@ -0,0 +1,45 @@
import math, colorama
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more deployment ready to a public repository.
Here, is the code

import math
import colorama
from colorama import Fore, Style

colorama.init()

def progress_bar(progress, total, color=Fore.CYAN, complete_color=Fore.GREEN):
    percent = 100 * (progress / total)
    bar_length = 50
    completed_length = int(bar_length * (progress / total))
    bar = '█' * completed_length + '-' * (bar_length - completed_length)

    print(f'\r|{color}{bar}{Style.RESET_ALL}| {color}{percent:.2f}%{Style.RESET_ALL}', end='', flush=True)

    if progress == total:
        print(f'\r|{complete_color}{bar}{Style.RESET_ALL}| {complete_color}{percent:.2f}%{Style.RESET_ALL}')

numbers = [x * 5 for x in range(2000, 3000)]
lenght = len(numbers)

progress_bar(0, total=lenght)

results = []
for i, x in enumerate(numbers):
    results.append(math.factorial(x))
    progress_bar(i + 1, total=lenght)

print("\nFactorials calculated successfully!")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See, this, add this to your code.
Document it more properly if want,
then recommit it.
It will be all good.

# Set the bar progress to 0

for i, x in enumerate(numbers):
#Iterate over the list of ints with enumerate to get both the index "i" (used for progress) and the value of the int "x" to execute the operation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvements made:

Added Style.RESET_ALL after each colored element to ensure the color doesn't carry over to subsequent print statements.
Adjusted the length of the progress bar to a fixed value (50 characters) for better visualization.
Used flush=True in the print statement to ensure real-time printing of the progress bar without buffering.

This improved version provides a clearer and more visually appealing progress bar while calculating factorials for the given numbers.

@Piombacciaio
Copy link
Contributor Author

Piombacciaio commented Jul 30, 2023

Thanks for the improvements and suggestions Nitkarsh, will fix asap

Updated the program with the suggestions from NitkarshChourasia and added less repetitive comments
Copy link
Contributor

@NitkarshChourasia NitkarshChourasia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good implementations while reiterating.

Copy link
Contributor

@OfficialAhmed OfficialAhmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't try it but looks promising

@NitkarshChourasia
Copy link
Contributor

Indeed.

@AvinashMarkad
Copy link

pip install tqdm #

from tqdm import tqdm
import time

Number of iterations for the loop

total_iterations = 100

Create a tqdm instance with the total number of iterations

progress_bar = tqdm(total=total_iterations, desc="Processing")

Simulate a task with a loop

for i in range(total_iterations):
# Perform your task here
time.sleep(0.1) # Simulating some work

# Update the progress bar
progress_bar.update(1)

Close the progress bar

progress_bar.close()

print("Task completed!")

see this program and add in your program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants