I don't know if you have ever had the experience that you have written a program and it takes a long time to run each time. While waiting for the program to run you press enter again and again to prevent the program from getting stuck. Or maybe your task requires you to keep track of the program's progress in real time, but you don't know where the program has reached...
Now, here comes five-progress, a progress bar library for Python that's easy to use and supports a variety of cool display effects! Let's take a look at the sample effect:
1.Installation
Install with pip under Python:
pip install alive-progress
2.Quick Start
2.1 Direct use
Using alive-progress in a loop is the most common use, and the script can be written like this:
from alive_progress import alive_bar
import time
# Create a progress bar using the with statement
with alive_bar(100) as bar: # Pass in the total number of progress bars to alive_bar (100 here)
for item in range(100):
# Wait 1s
time.sleep(.1)
#Update progress bar, progress +1
bar()
Note that if the animation does not display properly try adding force_tty=True to the alive_bar parameter.
Running the above code we can see a fairly flashy dynamic progress bar appear in the terminal:.
Note that alive-progress does not update automatically like progress bar libraries such as tqdm, but only when the bar is called by our application.
Of course, we can also not pass the total number parameter to the progress bar, in which case the bar will not show progress and will enter undefined mode: the
Sometimes we want to manipulate the position of the display directly, so we can set the manual parameter of alive_bar to True.
from alive_progress import alive_bar
import time
total = 100
with alive_bar(total, manual=True) as bar: # total Can be unspecified, at this point only the percentage
bar(0.5) # Progress to 50%
time.sleep(0.5)
bar(0.1) # Progress to 10%
time.sleep(0.5)
bar(0.75) # Progress to 75%
time.sleep(0.5)
bar(1.0) # Progress to 100%
time.sleep(0.5)
bar(10) # Progress to 1000%
for i in range(1,101):
bar(i/100) # Set the progress to i%
time.sleep(0.05)
Of course, we also need to output some hint information during the run, directly using print can output a line of hint information without destroying the progress bar, the text method can add suffix characters at the end of the progress bar, and the title parameter can add a title (prefix information) to the progress bar, the specific use and effect are as follows.
from alive_progress import alive_bar
import time
# Define the title (prefix character) as HelloGitHub
with alive_bar(10, title="HelloGitHub") as bar:
for i in range(10):
time.sleep(1)
bar() # Make progress +1
bar.text("Processing Work #%d"%(i+1)) # Update progress bar suffix
print("Work #%d finished"%i) #Output a line of information
2.2 Add some fancy
Have seen too much traditional progress bar style and want to change it up? No problem, alive-progress not only has a variety of built-in progress bar styles, but also supports custom formatting.
There are two customizable styles for the progress bar: bar and spinner, just pass in the corresponding parameters when calling alive_bar.
Take this progress bar for example, the longest one in the middle is the bar and the one next to it, www.HelloGitHub.com, is the spinner.
There are various bar and spinner styles built into alive-progress, just call show_bars or show_spinners to get a quick preview of the corresponding style, for example:
from alive_progress import show_bars
show_bars() # View built-in bar styles
from alive_progress import show_spinners
show_spinners() #View built-in spinner styles
The default style is very simple to use, for example, I want to use bubbles the bar and message_scrolling the spinner, directly pass the corresponding name can be:
from alive_progress import alive_bar
import time
# Just pass in the corresponding name directly
with alive_bar(
100,
title="HelloGitHub",
bar="bubbles", spinner="message_scrolling"
) as bar:
for i in range(100):
time.sleep(.1)
bar()
If the number of total is not known, you can use the unknown parameter (which will then replace bar with spinner).
from alive_progress import alive_bar
import time
with alive_bar(
title="HelloGitHub",
# Note: Here the bar is replaced with unknow, and the built-in style name is the same as that of spinner
unknown="stars", spinner="message_scrolling"
) as bar:
for i in range(100):
time.sleep(.1)
bar()
3.Personalized
Perhaps you prefer your own custom progress bar to using the built-in template, for which alive-progress also provides a method.
3.1 Customized bar
The bar can be quickly customized using the standard_bar_factory method, and there are five parameters that can be set for the bar.
-
chars
:The animation of the unit being executed is displayed in order of progress. -
borders
:Progress bar borders, shown on the left and right sides. -
background
:Not executed to the content displayed in the unit. -
tip
:The leading symbol of the execution unit. -
errors
:The character to be displayed in case of errors (progress is not complete, total value is exceeded, etc.).
For example, we want to make a bar as shown in the figure.
Then you can write it like this:
from alive_progress import alive_bar, standard_bar_factory
import time
##-------Customize bar-------##
my_bar = standard_bar_factory( # The following parameters have default values, so you don't have to change them all at once
chars="123456789#", # Displayed sequentially according to the progress when loading, any length
borders="<>", # bar Both ends of the border
background=".", # The unloaded part is filled with "." fill
tip=">", # Leading symbols indicating the direction of progress (splitting "#" and ".")
errors="⚠❌" # What is displayed when an error occurs (incomplete, overflow)
)
##-------End of customization-------##
##--------Animation Demo-------##
with alive_bar(
10,
title="HelloGitHub",
bar=my_bar, # Here you pass in the bar you just customized
spinner="message_scrolling",
manual=True
) as bar:
for i in range(50):
time.sleep(.1)
bar(i/100)
bar(.5)
time.sleep(2)
bar(10)
print("Overflow")
time.sleep(1)
bar(1)
print("100% complete")
time.sleep(1)
bar(.1)
print("Incomplete")
3.2 Custom spinner
For spinner, five-progress provides more ways to define animations:
frame_spinner_factory
:Output the incoming strings one by one:
from alive_progress import alive_bar, frame_spinner_factory
import time
my_spinner = my_spinner = frame_spinner_factory(
r'-----',
r'1----',
r'-2---',
r'--3--',
r'---4-',
r'----5'
) # Pass in the string directly
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)
You can see that the strings are output one by one in a loop.
scrolling_spinner_factory
:Scroll the string to broadcast
from alive_progress import alive_bar, scrolling_spinner_factory
import time
my_spinner = scrolling_spinner_factory(
chars="HelloGitHub", # The string you want to play
length=15, # spinner Area width
blank='.' # Blank section fill character
)
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)
bouncing_spinner_factory
:Scroll the two strings alternately
from alive_progress import alive_bar, bouncing_spinner_factory
import time
my_spinner = bouncing_spinner_factory(
right_chars="I love", # String entering from the left
length=15, # spinner Length of area
left_chars="HelloGitHub", # String entering from the right
blank='.', # Blank area fill characters
)
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)
Of course, it is possible to omit the left_chars parameter, which has the effect that I love will bounce left and right like a pinball.
unknown_bar_factory
:Convert spinner to a format that can be used in undefined schemas.
from alive_progress import alive_bar, unknown_bar_factory, bouncing_spinner_factory
import time
my_spinner = bouncing_spinner_factory("www.HelloGitHub.com",15,hiding=False)
my_unknown_bar = unknown_bar_factory(my_spinner) # Pass in the defined spinner
with alive_bar(
title="HelloGitHub",
unknown=my_unknown_bar
) as bar:
while True:
bar()
time.sleep(.1)