6 useful Python tips for progress bars

In an article I wrote before, I introduced tqdmthis progress bar library, which is already very popular in the current Pythoncircle. 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 tqdmthe development and iteration in the past few years, more and better functions have been added. In today's article, I will summarize 6 tqdmfeatures 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

tqdmMost of the friends who have used it know that it can be used in conventional terminals and jupytervarious 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. jupyterThe style editor is used from tqdm.notebook import tqdmto import separately.

In tqdmrecent 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 tqdmimporting them uniformly:tqdm

picture

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 delayto 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:

picture

3 Customize the color of the progress bar

By tqdm()setting parameters colourfor , you can pass in a variety of common color format values, which jupyterare especially effective in the class editor:

picture

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 pandasthe middle data frame itertuples(). In this case, we can use the totalparameters to preset the upper limit:

picture

5 Alternatives to enumerate, zip and map

PythonIn addition to the regular looping process, there are several built-in functions that also have the property of iterative looping. tqdmIn order to facilitate us to add progress bars to these atypical looping processes, we have also developed them separately tenumerate, tzipand tmapthese three APIs are used to replace enumerate, zipand map:

picture

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:

picture

The above is the whole content of this article, welcome to discuss with me in the comment area~

recommended article

Technology Exchange

Welcome to reprint, collect, like and support!

insert image description here

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

long press follow

Guess you like

Origin blog.csdn.net/weixin_38037405/article/details/123956050