Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Refresh your understanding of the progress bar and write a different progress bar with python


Jun 01, 2021 Article blog


Table of contents


1 Introduction

In our daily work, we often use "loop iterations" to run programs, and if this execution time is short, it doesn't matter. H owever, there are some processes that take a long time, and it is useful to add a "progress bar" to help us monitor the progress of code execution and the abnormality of the process. Here's a look at two very practical and very different progress bar-related libraries in Python - the main uses of tqdm and alive-progress

2 tqdm common methods

tqdm is the most famous of all the progress bar-related libraries in Python and since it's best known, it's naturally unique.

tqdm not only generates the underlying progress bar that can be displayed in the terminal, but also works with jupyter notebook and jupyter lab to generate a more beautiful web "interactive" part form of the progress bar, but also pandas with pandas to provide proprietary progress bar functionality for some operations in pandas

Let's take a look at the main features of tqdm

2.1 Basic usage

Because it's a third-party library, you first need to install it with pip install tqdm or conda install -c conda-forge tqdm and let's look at its most basic usage when you're done:

 Refresh your understanding of the progress bar and write a different progress bar with python1

With tqdm.tqdm simply wrapping objects that iterate through for enables the ability to add progress bars to the loop process and print useful information such as execution speed, run time, and estimated remaining run time, as well as for "list derivation":

 Refresh your understanding of the progress bar and write a different progress bar with python2

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

 Refresh your understanding of the progress bar and write a different progress bar with python3

The accompanying desc can also help us set the caption for the progress bar:

 Refresh your understanding of the progress bar and write a different progress bar with python4

If you want to change the description text during the iteration, you can also pre-instantiate the progress bar object and perform the appropriate procedures when you need to refresh the explanatory text:

 Refresh your understanding of the progress bar and write a different progress bar with python5

But when the object length of an iteration is initially unknown, such as an iteration of DataFrame.itertuples() in pandas we can only estimate information such as how fast it executes, but we can't see the progress bar increasing because tqdm doesn't know where the iteration ends:

 Refresh your understanding of the progress bar and write a different progress bar with python6

2.2 Beautiful progress bar with jupyter notebook/jupyter lab

tqdm has special support for jupyter notebook and jupyter lab and is very simple trange use, just modify the original from from tqdm import XXX corresponding feature import format to from tqdm.notebook import XXX for example:

 Refresh your understanding of the progress bar and write a different progress bar with python7

2.3 Match the apply in pandas

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

 Refresh your understanding of the progress bar and write a different progress bar with python8

3-alive-progress common methods

Although, like tqdm libraries are created to add progress bars to the looping process, alive-progress adds more dynamic effects than tqdm and we can see all available dynamic progress bar styles by calling its dedicated showtime() function:

 Refresh your understanding of the progress bar and write a different progress bar with python9

Similarly, you can view all progress bar styles:

 Refresh your understanding of the progress bar and write a different progress bar with python10

It's also very simple to use, but it's very different from tqdm usage and needs to be with the keywords, such as alive_bar we use in the alive_progress below to generate a dynamic progress bar:

 Refresh your understanding of the progress bar and write a different progress bar with python11

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

 Refresh your understanding of the progress bar and write a different progress bar with python12

Unfortunately, the current alive-progress can only run in the terminal, and you haven't developed a more beautiful interactive part for jupyter but you can use it in tasks such as web crawlers, and it works very well. Then students who want to learn python can take a look at the tutorial.

python tutorial: https://www.w3cschool.cn/python/

python3 Basic Microsyscope: https://www.w3cschool.cn/minicourse/play/python3course