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 use the progress bar properly in Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to properly use the progress bar in Python". In the daily operation, I believe many people have doubts about how to properly use the progress bar in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to properly use the progress bar in Python". Next, please follow the editor to study!

1 introduction

The loop iteration process is often involved in the daily running of the program, which doesn't matter for the program with very short execution time, but for the program involving loop iteration which is obviously time-consuming, adding a progress bar (progress bar) is a very practical skill to help us monitor the progress of code execution and deal with intermediate exception errors.

2. Common methods of tqdm

Tqdm is the most famous of all the progress bar related libraries in Python, and since it is the most famous, it naturally has its uniqueness.

Tqdm can not only generate a basic progress bar that can be displayed in the terminal, but also cooperate with jupyter notebook and jupyter lab to generate a more beautiful progress bar in the form of web interaction components, but also join forces with pandas to provide proprietary progress bar functions for some operations in pandas.

Let's introduce the main functions of tqdm.

2.1 basic usage

Because it is a third-party library, you first need to install it using pip install tqdm or conda install-c conda-forge tqdm. After the installation is complete, let's take a look at its most basic usage:

By using tqdm.tqdm, simply wrapping the iterative objects in the for loop can add a progress bar to the loop process and print practical information such as execution speed, elapsed time and estimated remaining runtime. It can also be used for list derivation:

For cases where the iterative object is range (), tqdm also provides a simplified version of trange () instead of tqdm (range ()):

The accompanying parameter desc can also help us set the description text of the progress bar:

If you want to change the description text during the iteration, you can also instantiate the progress bar object in advance and execute the corresponding program when you need to refresh the description text:

But when the length of the iterative object is unknown at the beginning, such as iterating over DataFrame.itertuples () in pandas, we can only estimate its execution speed and other information, but we cannot see the progress bar increasing, because tqdm does not know the end point of the iteration:

2.2 cooperate with the aesthetic progress bar of jupyter notebook/jupyter lab

Tqdm has special support for jupyter notebook and jupyter lab, and the method is very simple. You only need to modify the import format of the original from tqdm import XXX function to from tqdm.notebook import XXX. Take trange as an example:

2.3 cooperate with apply in pandas

Tqdm provides special support for the apply () procedure in pandas, because apply () in pandas is essentially a serial loop operation, you can replace any apply operation in pandas with progress_apply, and remember to execute tqdm.pandas () before each individual progress_apply, as in the following example:

3. Common methods of alive-progress

Although alive-progress, like tqdm, is a library created to add a progress bar to the loop process, alive-progress adds more variety of dynamic effects than tqdm. We can view all available dynamic progress bar styles by calling its specially provided showtime () function:

Similarly, you can view all progress bar styles:

It is also very easy to use, but it is very different from tqdm usage, and needs to be combined with with keywords. For example, we use alive_bar in alive_progress to generate a dynamic progress bar:

Change the style of the progress bar by modifying the bar parameter:

At this point, the study on "how to make good use of the progress bar in 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