-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
base: master
Are you sure you want to change the base?
Create Progress bar.py #1950
Conversation
Progress bar.py
Outdated
@@ -0,0 +1,45 @@ | |||
import math, colorama |
There was a problem hiding this comment.
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!")
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Thanks for the improvements and suggestions Nitkarsh, will fix asap |
Updated the program with the suggestions from NitkarshChourasia and added less repetitive comments
There was a problem hiding this 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.
There was a problem hiding this 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
Indeed. |
pip install tqdm # from tqdm import tqdm Number of iterations for the looptotal_iterations = 100 Create a tqdm instance with the total number of iterationsprogress_bar = tqdm(total=total_iterations, desc="Processing") Simulate a task with a loopfor i in range(total_iterations):
Close the progress barprogress_bar.close() print("Task completed!") see this program and add in your program |
Created a simple program that displays a colored progress bar on the terminal window