Jun 01, 2021 Article blog
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
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
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:
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":
In cases where the iterative object is
range()
tqdm
also provides a simplified version of
trange()
instead of
tqdm(range())
The accompanying
desc
can also help us set the caption for the progress bar:
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:
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:
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:
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:
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:
Similarly, you can view all progress bar styles:
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:
Change the style of the progress bar by modifying the
bar
parameter:
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