In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python progress bar open source library refers to what, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Now, alive-progress is here, it is a progress bar library under Python, which is not only easy to use but also supports a variety of cool display effects! Let's take a look at the example first:
Next, let's play with this library!
I. installation
Use pip to install under Python:
Pip install alive-progress 2.1.Quick start 2.1Direct use
Using alive-progress in a loop is the most common use, and the script can be written as follows:
# Import alive-progress library from alive_progress import alive_bar import time # create a progress bar using the with statement with alive_bar (100) as bar: # pass the total number of progress bars to alive_bar (here is 100) for item in range (100): # wait for 1s time.sleep (.1) # update the progress bar, progress + 1 bar ()
Note that if the animation does not display properly, try to add the force_tty=True parameter to alive_bar.
Running the above code, we can see that a rather gorgeous dynamic progress bar appears in the terminal:
It should be noted that alive-progress does not update automatically like tqdm and other progress bar libraries. Only when our program calls bar will it make the progress bar + 1.
Of course, we can not pass the parameter "total number" to the progress bar. In this case, the progress bar will not show the progress and enter undefined mode:
Sometimes we want to directly manipulate the position of the display, so we can set the manual parameter of alive_bar to True:
From alive_progress import alive_bar import time total = 100with alive_bar (total, manual=True) as bar: # total may not be specified There is only a percentage of 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 (1101): bar (iUnip 100) # set progress to I% time.sleep (0.05)
Of course, we also need to output some prompt information during running. Using print directly, you can output a line of prompt information without destroying the progress bar. The text method can add suffix characters to the end of the progress bar, and the title parameter can add a title (prefix information) to the progress bar. The specific usage and effects are as follows:
From alive_progress import alive_bar import time # defines the title (prefix character) as HelloGitHub with alive_bar (10 Title= "HelloGitHub") as bar: for i in range (10): time.sleep (1) bar () # Let the progress + 1 bar.text ("Doning Work #% d"% (item1)) # update the progress bar suffix print ("Work #% d finished"% I) # output a line of information 2.2 to add a bit of fancy
Want to change the traditional progress bar style after seeing too much? No problem, alive-progress not only has a variety of built-in progress bar styles, but also supports custom formats.
There are two customizable styles for the progress bar: bar and spinner. You only need to pass in the corresponding parameters when calling alive_bar.
Take this progress bar as an example, the longest in the middle is bar, and the www.HelloGitHub.com swinging back and forth next to it is spinner.
Alive-progress has a variety of built-in bar and spinner styles. You only need to call show_bars or show_spinners to quickly preview the corresponding styles, for example:
From alive_progress import show_bars show_bars () # View built-in bar style from alive_progress import show_spinners show_spinners () # View built-in spinner style
The default style is very easy to use. For example, I want to use the bar of bubbles and the spinner of message_scrolling, and pass the corresponding name directly:
From alive_progress import alive_bar import time # pass the corresponding name directly to with alive_bar (100,100, title= "HelloGitHub", bar= "bubbles", spinner= "message_scrolling") as bar: for i in range (100): time.sleep (.1) bar ()
If you don't know the number of total, you can use the unknown parameter (which will replace bar with spinner):
From alive_progress import alive_bar import time with alive_bar (title= "HelloGitHub", # Note: here bar is replaced by unknow, and the built-in style name is the same as spinner's unknown= "stars", spinner= "message_scrolling") as bar: for i in range (100): time.sleep (.1) bar () 3. Private customization
Maybe you prefer your own customized progress bar to using built-in templates directly, and alive-progress also provides a way to do this.
3.1Custom bar
Using the standard_bar_factory method, you can quickly customize five parameters that bar,bar can set:
Chars: the animation of the unit is being executed, displayed in order of progress.
Borders: progress bar boundaries, shown on the left and right sides.
Background: the content displayed by the unit is not executed.
Tip: the leading symbol of the execution unit.
Errors: the character displayed when an error occurs (incomplete progress, exceeding the total value, etc.).
For example, we want to make a bar as shown in the figure:
You can write like this:
From alive_progress import alive_bar, standard_bar_factory import time # #-Custom bar-## my_bar = standard_bar_factory (all of the following parameters have default values, so it is not necessary to modify chars= "12345678" all at once, and # will be displayed according to the progress when loading. Arbitrary length borders= "", # bar boundary background= ".", # unloaded part with "." Fill in the tip= ">", # pilot symbol indicating the direction of progress (split "#" and ".") What is displayed when an error occurs in errors= "⚠❌" # (incomplete (overflow)) # #-Custom end-#-Animation-# # with alive_bar (10, title= "HelloGitHub", bar=my_bar, # here pass in the newly customized bar spinner= "message_scrolling" Manual=True) as bar: for i in range (50): time.sleep (.1) bar (iP100) bar (.5) time.sleep (2) bar (10) print ("overflow") time.sleep (1) bar (1) print ("100% complete") time.sleep (1) bar (.1) Print ("unfinished") 3.2 Custom spinner
There are more ways to define animation for spinner,alive-progress:
Frame_spinner_factory: output the passed strings one by one:
From alive_progress import alive_bar, frame_spinner_factory import time my_spinner = my_spinner = frame_spinner_factory (Renewal Mustang, racy 1 Murray Murray, ritual Mustang 2 Murray, Rumi Muhami 3 Murray' The string with alive_bar (title= "HelloGitHub") # is passed directly into the string Spinner=my_spinner) as bar: while True: bar () time.sleep (.1)
You can see the string output looping one by one.
Scrolling_spinner_factory: play strings scrolling
From alive_progress import alive_bar, scrolling_spinner_factory import time my_spinner = scrolling_spinner_factory (chars= "HelloGitHub", # string length=15 to be played # spinner area width blank='.' # Blank fill character) with alive_bar (title= "HelloGitHub", spinner=my_spinner) as bar: while True: bar () time.sleep (.1)
Bouncing_spinner_factory: alternate rolling of two strings
From alive_progress import alive_bar, bouncing_spinner_factory import time my_spinner = bouncing_spinner_factory (right_chars= "I love", # string length=15 entered from the left, # spinner area length left_chars= "HelloGitHub" # string blank='.', # blank space padding character entered from the right) with alive_bar (title= "HelloGitHub", spinner=my_spinner) as bar: while True: bar () time.sleep (.1)
Of course, you can also omit the parameter left_chars, which means that I love will bounce left and right like a pinball.
Unknown_bar_factory: converts spinner to a format that can be used in an undefined schema:
From alive_progress import alive_bar, unknown_bar_factory, bouncing_spinner_factory import time my_spinner = bouncing_spinner_factory ("www.HelloGitHub.com", 15 shooting 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)
At this point, I believe you have mastered the basic game of alive_progress, alive-progress also provides some special functions needed in different occasions, interested friends can read the official documentation or source code for more in-depth understanding.
After reading the above, have you mastered what the Python progress bar open source library refers to? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.