Welcome to focus on Python, data analysis, data mining and fun tools!
If you haven't used the progress bar before, you probably think it will increase unnecessary complexity or difficult to maintain, but it's not. It only takes a few lines of code to add a progress bar. In these lines of code, we can see how to add a progress bar to the command-line script and PySimpleGUI UI.
Four common Python progress bar libraries are described below.
1,Progress
The first Python library to introduce is Progress.
You only need to define the number of iterations, the type of progress bar, and inform the progress bar at each iteration.
import timefrom progress.bar import IncrementalBarmylist = [1,2,3,4,5,6,7,8] bar = IncrementalBar( Countdown , max = len(mylist)) for item in mylist: bar.next() time.sleep(1) bar.finish()
Progress supports several types of progress bars, as shown below:
2,tqdm
Let's take a look at the tqdm library.
Similar to the library seen before, these two lines of code are also very similar, with a little difference in settings:
import timefrom tqdm import tqdmmylist = [1,2,3,4,5,6,7,8] for i in tqdm(mylist): time.sleep(1)
The progress bar effect achieved by tqdm is as follows:
This progress bar also provides several options.
Related documents: https://tqdm.github.io/
3,Alive Progress
As the name suggests, this library can make the progress bar more vivid. It has more animation effects than the progress bar we have seen before.
Similar from a code perspective:
from alive_progress import alive_barimport timemylist = [1,2,3,4,5,6,7,8] with alive_bar(len(mylist)) as bar: for i in mylist: bar() time.sleep(1)
The appearance of the progress bar is similar to that expected:
This progress bar has some unique functions, which will be interesting to use. See the project for function details: https://github.com/rsalmei/alive-progress
4,PySimpleGUI
Get graphical progress bar with PySimpleGUI
We can add a simple line of code to get a graphical progress bar in the command-line script.
In order to achieve the above, the code we need is:
import PySimpleGUI as sgimport timemylist = [1,2,3,4,5,6,7,8] for i, item in enumerate(mylist): sg.one_line_progress_meter( This is my progress meter! , i+1, len(mylist), -key- ) time.sleep(1)
The author of the progress bar project in the PySimpleGUI application previously discussed "how to quickly start the Python UI and then use the UI to create a comparison tool" on GitHub. In this project, the author also discusses how to integrate the progress bar.
The code is as follows:
import PySimpleGUI as sgimport timemylist = [1,2,3,4,5,6,7,8] progressbar = [ [sg.ProgressBar(len(mylist), orientation= h , size=(51, 10), key= progressbar )]] outputwin = [ [sg.Output(size=(78,20))]] layout = [ [sg.Frame( Progress ,layout= progressbar)], [sg.Frame( Output , layout = outputwin)], [sg.Submit( Start ),sg.Cancel()]] window = sg.Window( Custom Progress Meter , layout) progress_bar = window[ progressbar ] while True: event, values = window.read(timeout=10) if event == Cancel or event is None: break elif event == Start : for i,item in enumerate(mylist): print(item) time.sleep(1) progress_bar.UpdateBar(i + 1)window.close()
Yes, using the progress bar in Python scripts requires only a few lines of code and is not complicated at all. With the progress bar, you don't have to guess how the script runs in the future.
Reference link: https://towardsdatascience.com/learning-to-use-progress-bars-in-python-2dc436de81e5
Technical exchange
Welcome to reprint, collect, gain, praise and support!
At present, a technical exchange group has been opened, with more than 2000 friends. The addition methods are as follows:
The following methods can be used. The best way to add is: source + Interest direction, which is convenient to find like-minded friends
- Method 1: send the following pictures to wechat for long press recognition and reply to group addition;
- Mode 2: directly add a small assistant micro signal: Python 666. Remarks: from CSDN
- Mode three, WeChat search official account: Python learning and data mining, background reply: add group