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 running Speed of Monitoring Program by Visualization of Python Progress Bar

2025-04-01 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 "how to visualize the Python progress bar to monitor the running speed of the program". 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!

Preface

Today, I would like to share with you a progress bar visualization library called tqdm, which can help us monitor the progress of the program. Users only need to encapsulate iterable objects.

Installation

Install directly from the command line.

Pip install tqdm

Douban image can also be used to install.

Pip install-I https://pypi.douban.com/simple tqdm

After executing the above command, you can check to see if the installation is successful.

How pip show tqdm is used

The following demo runs the environment: jupyter notebook

The use of different operating environments is slightly different, which can be adjusted according to the warning.

There are many optional parameters for tqdm, so let's take a look at some commonly used parameters first.

Main parameters:

Iterable: iterable objects that do not need to be set when manually updated

Desc: str, descriptive text of the progress bar on the left

Total: total number of projects

Leave: bool, whether to keep the progress bar after the execution is completed

File: the output points to the location. The default is the terminal, which generally does not need to be set.

Ncols: adjust the width of the progress bar. The default is to automatically adjust the length according to the environment. If set to 0, there is no progress bar, only the output information.

Unit: describes the text of the project to be processed. The default is' it', for example: 100 it/s, and if it is set to 'img', it is 100 img/s.

Unit_scale: automatically converts project processing speed units according to international standards, such as 100000 it/s > > 100k it/s

Colour: progress bar color, for example: 'green',' # 00ff00'.

Example

Pass the list directly into tqdm ().

From tqdm.notebook import tqdmfrom time import sleepfor char in tqdm (['Python',' Java', 'Clearing']): sleep (0.25)

Use iterable objects.

For i in tqdm (range): sleep

Tqdm provides a trange () method instead of tqdm (range ()).

From tqdm.notebook import trangefor i in trange: sleep

We add descriptive content in front of the progress bar, here write tqdm outside the loop, and use set_description () to add "progress% d" before the progress bar.

Pbar = tqdm (range (5)) for char in pbar: pbar.set_description ("Progress% d"% char) sleep (1)

We can set the interval for updating the progress bar. Next, we set the total to total=100, and then divide it into four times, so that the progress bar is updated at an interval of 10%, 20%, 30%, 40% and 40%.

With tqdm (total=100) as pbar: for i in range (1,5): sleep (1) # Update Progress pbar.update (10Secreti)

Change the progress bar color.

With tqdm (total=100, colour='pink') as pbar: for i in range (1,5): sleep (1) # Update Progress pbar.update (10Secreti)

Note: when using tqdm to display progress bars, you cannot use print if you want to output content. Print will result in the output of multiple progress bars, and you can use tqdm.write ().

For i in tqdm (range (5)): tqdm.write ("come on") sleep (0.1)

For multiple loops, you can specify multiple progress bars. Set leave=False after the second loop is executed, the progress bar will not be saved.

For i in trange (3, desc='1st loop'): for i in trange (100, desc='2nd loop', leave=False): sleep

This is the end of the content of "how to visualize the Python progress bar to monitor the running speed of the program". Thank you for 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