Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used progress bar libraries in Python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what progress bar libraries are commonly used in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We all know that the progress bar is an excellent tool for visually showing the time required for the process, lest we worry that the process will suddenly die, and we can use it to predict whether the code is running properly, with the help of the progress bar. everyone can visually see the relatively new progress of the script.

If you haven't used a progress bar before, you probably think it will add unnecessary complexity or be 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 progress bars to command-line scripts as well as to PySimpleGUI UI.

Four common Python progress bar libraries are described below:

Progress

The first Python library to be introduced 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 Countdown', max 2 3 time.sleep 4 5 6 7 8] bar = IncrementalBar ('Countdown', max = len (mylist)) for item in mylist: bar.next () time.sleep (1) bar.finish ()

The effect of the progress bar implemented by Progress is as follows:

Incremental progress bar for Progressbar

If you don't like the format of the progress bar, you can also choose from the following formats:

The type of progress bar supported by ProgressBar.

Related documentation: https://pypi.org/project/progress/1.5/

Tqdm

Let's take a look at the tqdm library.

Similar to the libraries you've seen before, these two lines of code are also very similar, with a slight difference in setup:

Importtimefromtqdmimporttqdmmylist= [1 mylist 2 3 4 5 5 6 7 8] foriintqdm (mylist): time.sleep (1)

The effect of the progress bar implemented by tqdm is as follows:

This progress bar also provides several options.

Related documentation: https://tqdm.github.io/

Alive Progress

As the name implies, this library can make the progress bar lively, and it has some more animation effects than the progress bar we have seen before.

Similar from a code point of view:

From alive_progress import alive_barimport timemylist = [1 len 2 (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 distinctive features that will be interesting to use. For more information, please see the project: https://github.com/rsalmei/alive-progress.

PySimpleGUI

Using PySimpleGUI to get graphical progress bar

We can add a simple line of code to get a graphical progress bar in the command line script.

To achieve the above, the code we need is:

Import PySimpleGUI as sgimport timemylist = [1 len 2, 3 key-' 4 5, 6 7, 8] for I, item in enumerate (mylist): sg.one_line_progress_meter ('This is my progress meterials, item1, len (mylist),'-key-') time.sleep (1)

Progress bar in PySimpleGUI application

The project author has previously discussed "how to start Python UI quickly and then use 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 = [sg.ProgressBar (len (mylist), orientation='h', size= (51,10), key='progressbar')]] outputwin = [[sg.Output (size= (78)]] 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) ifevent==' Cancel' or event is None: break elifevent=='Start': fori,iteminenumerate (mylist): print (item) time.sleep (1) progress_bar.UpdateBar (iTun1) window.close () "what are the commonly used progress bar libraries in Python"? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report