In an article I wrote before, I introduced tqdm
this progress bar library, which is already very popular in the current Python
circle. It can help us add a progress bar to any code logic with loop iteration process, so as to help us perceive the process of code running.
With tqdm
the development and iteration in the past few years, more and better functions have been added. In today's article, I will summarize 6 tqdm
features that are very worth learning. If you like this article, remember to bookmark, like, and follow.
[Note] More technical exchanges can be obtained at the end of the article
6 useful features of tqdm
1 autonotebook automatically switches the progress bar style
tqdm
Most of the friends who have used it know that it can be used in conventional terminals and jupyter
various editors of the style, and in the latter, it will be rendered in a more beautiful form. In the past, we usually needed to use it in conventional terminals from tqdm import tqdm
. jupyter
The style editor is used from tqdm.notebook import tqdm
to import separately.
In tqdm
recent versions, new experimental features have been introduced, so that we can adaptively detect different operating environments and automatically control the display only by from tqdm.autonotebook import tqdm
importing them uniformly:tqdm
2 Delay rendering progress bar
Sometimes we hope that when the loop process is executed soon, the progress bar can not be printed. After all, the main purpose of the progress bar is to monitor the long-running process. At this time, we can tqdm()
add parameters delay
to set the delay time in seconds. If the actual running time of the loop process is less than that delay
, there is no need to print the redundant iterative process:
3 Customize the color of the progress bar
By tqdm()
setting parameters colour
for , you can pass in a variety of common color format values, which jupyter
are especially effective in the class editor:
4 Progress ceiling for autonomous control
In some cases, tqdm()
the objects we pass in cannot be pre-calculated to get the upper limit of progress rounds during the iteration process, such as pandas
the middle data frame itertuples()
. In this case, we can use the total
parameters to preset the upper limit:
5 Alternatives to enumerate, zip and map
Python
In addition to the regular looping process, there are several built-in functions that also have the property of iterative looping. tqdm
In order to facilitate us to add progress bars to these atypical looping processes, we have also developed them separately tenumerate
, tzip
and tmap
these three APIs are used to replace enumerate
, zip
and map
:
6 Set the progress bar to "run out"
When we want to add progress bar monitoring to the multi-layer loop process, it is conventional to use it directly for each layer tqdm()
, which will result in too many progress bars being printed, which is not conducive to our observation of the progress process.
And through the use tqdm.auto
, trange()
we can set the parameters leave=False
, so that our corresponding progress bar will automatically disappear when loaded, such as the example shown in the following animation:
The above is the whole content of this article, welcome to discuss with me in the comment area~
recommended article
-
Li Hongyi's "Machine Learning" Mandarin Course (2022) is here
-
Someone made a Chinese version of Mr. Wu Enda's machine learning and deep learning
-
I'm addicted, and recently I gave the company a big visual screen (with source code)
-
So elegant, 4 Python automatic data analysis artifacts are really fragrant
Technology Exchange
Welcome to reprint, collect, like and support!
At present, a technical exchange group has been opened, and the group has more than 2,000 members . The best way to remark when adding is: source + interest direction, which is convenient to find like-minded friends
- Method 1. Send the following picture to WeChat, long press to identify, and reply in the background: add group;
- Method ②, add micro-signal: dkl88191 , note: from CSDN
- Method ③, WeChat search public account: Python learning and data mining , background reply: add group