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 methods of displaying the progress bar in real time by Python?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the methods of Python to achieve real-time display of progress bar". In daily operation, I believe that many people have doubts about the method of real-time display of progress bar in Python. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the methods of real-time display of progress bar by Python?" Next, please follow the editor to study!

I'm sure you are familiar with the progress bar. For example, you can see the progress of the download when we install the python library, and you can also see a similar progress bar when downloading files, such as the following figure:

Application scenarios: download files, task timing, etc.

Today, Brother Chen will share with you six different ways to achieve real-time display of processing progress in Python, each of which is accompanied by a case and official documents for everyone to learn and customize to modify.

Type 1: general progress bar

Real-time refresh display by using printing function print

For i in range (1,101): print ("\ r", end= "") print ("Progress: {}%:" .format (I), "▓" * (I / 2), end= ") sys.stdout.flush () time.sleep

Type 2: ordinary progress bar with time

The second also prints the progress bar through print, but also adds a time time display to show the time required in the progress process.

T = 60print ("* progress bar with time *") start = time.perf_counter () for i in range (t + 1): finsh = "▓" * I need_do = "-" * (t-I) progress = (I / t) * 100 dur = time.perf_counter ()-start print ("\ r {: ^ 3.0f}% [{}-> {}] {: .2f} s ".format (progress Finsh, need_do, dur), end= "") time.sleep

Type 3: tqdm library

Tqdm is a python library dedicated to progress bars. For more information, please refer to the official documentation below.

Https://pypi.org/project/tqdm/#description

From tqdm import tqdmfor i in tqdm (range (1,60)): "Code"# assumes that this part of the code takes 0.05s and executes time.sleep 60 times in a loop.

Type 4: alive_ 's library

Alive_progress is a dynamic real-time display progress bar library. For detailed usage, please refer to the official documentation below.

Https://pypi.org/project/alive-progress/#description

From alive_progress import alive_bar# assumes that 100 tasks need to be performed with alive_bar (len (range)) as bar: for item in range (100): # traversal task bar () # shows progress "" Code "" # suppose this code part requires 0.05s time.sleep

Type 5: PySimpleGUI library

PySimpleGUI is also a dynamic progress bar library, which has its own GUI interface (based on PyQt,Tkinter, etc.). For more information, please refer to the official documentation below.

Https://pypi.org/project/PySimpleGUI/#description

Import PySimpleGUI as sgcount = range (100) for I, item in enumerate (count): sg.one_line_progress_meter ('real-time progress bar', I + 1, len (count),'- key-') "Code"# suppose this code part requires 0.05s time.sleep (0.05s)

Type 6: progressbar library

Https://pypi.org/project/progressbar/#description

Import progressbarp = progressbar.ProgressBar () # # suppose you need to perform 100 tasks, put for i in p (range (100)) in ProgressBar (): "" Code "" # suppose this part of the code requires 0.05s time.sleep

At this point, the study on "what are the methods of displaying the progress bar in real time by Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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