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

How to realize the cool progress bar in Python

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Python how to achieve cool progress bar related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will gain after reading this Python how to achieve cool progress bar article, let's take a look at it.

1. Custom ProgressBar

The most original way is to write a progress bar function without the help of any third-party tools, and use the time module with the sys module.

Import sysimport timedef progressbar (it, prefix= "", size=60, file=sys.stdout): count = len (it) def show (j): X = int (size*j/count) file.write ("% s [% s% s]% iThue% I\ r"% (prefix, "#" * x, ". * (size-x), j, count)) file.flush () show (0) for I Item in enumerate (it): yield item show (iTun1) file.write ("\ n") file.flush () for i in progressbar (range (15), "Computing:", 40): do_something () time.sleep (0.1)

The advantage of self-definition is that we can define the progress bar in the form we want, for example, the above is to use # and to output, why not print? Because sys.stdout is a default output format for print, and sys.stdout.write () can print without wrapping, sys.stdout.flush () can refresh the output immediately. Of course, it can also be encapsulated into classes for better use, but the effect is similar.

From _ _ future__ import print_functionimport sysimport reclass ProgressBar (object): DEFAULT = 'Progress:% (bar) s% (percent) 3D%%' FULL ='% (bar) s% (current) d total% (% (percent) 3D%%)% (remaining) d to go' def _ init__ (self, total, width=40, fmt=DEFAULT, symbol='=' Output=sys.stderr): assert len (symbol) = = 1 self.total = total self.width = width self.symbol = symbol self.output = output self.fmt = re.sub (r'(? P%\ (. +?)) dwells, r'\ g% dd'% len (str (total)) Fmt) self.current = 0 def _ call__ (self): percent = self.current / float (self.total) size = int (self.width * percent) remaining = self.total-self.current bar ='['+ self.symbol * size +'* (self.width-size) +'] args = {'total': self.total,' bar': bar 'current': self.current,' percent': percent * 100,' remaining': remaining} print ('\ r' + self.fmt% args, file=self.output, end='') def done (self): self.current = self.total self () print ('', file=self.output) from time import sleepprogress = ProgressBar (80 Fmt=ProgressBar.FULL) for x in range (progress.total): progress.current + = 1 progress () sleep (0.1) progress.done ()

2 、 tqdm

As we said before, the advantage of customization is that you can modify it yourself, so the advantage of using a third-party library is that you can be lazy, you don't have to write it yourself, you can use it. For example, when it comes to the Python progress bar, you will definitely think of the commonly used tqdm. It is easy to install and use pip install tqdm. The above progress bar can be realized in a few lines of code.

From tqdm import trangeimport timefor i in trange (10): time.sleep (1)

Of course, tqdm as an old Python progress bar tool, loop processing, multi-process, multi-threading, recursive processing and so on are supported, you can learn more ways to play on the official GitHub.

3 、 Rich

Have you learned the above two methods to achieve the Python progress bar? although it is simple, it does not look beautiful, and the color is relatively monotonous. So the final finale is a relatively niche third-party library Rich. Rich is mainly used to print rich and colorful text in the terminal (up to 16.7 million colors)

So of course you can use Rich to print a progress bar, showing the percentage of completion, remaining time, data transfer speed, and so on. And the style is cooler, and it's highly configurable, so we can customize it to display any information we need. It is also easy to use, for example, we use Rich to implement the simplest progress bar.

From rich.progress import trackimport timefor step in track (range (30)): time.sleep (0.5)

At the same time, Rich supports multiple progress bars, which is useful for monitoring progress in multitasking situations.

This is the end of the article on "how to achieve a cool progress bar in Python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve cool progress bar in Python". If you want to learn more, you are welcome to follow the industry information channel.

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: 238

*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