tqdm: simple visual progress for python

tqdm: simple visual progress for python

illustrate

​ The main purpose of this article is to get started quickly, not to parse the source code.

Directory Structure

1. Application scenarios

​ There are many application scenarios of the progress bar, but I have been studying the content of the CV direction recently, so the progress bar is mainly used to visualize the process of network training.

2. Library installation

​tqdm is a third-party library of python, which can quickly and easily realize the visual display of the progress bar.

​ The installation method is very simple, Windows can run the following command in the cmd command line window:

pip install tqdm

3. Quick overview of the method

tqdm object creation

​ The method we mainly use here is the method with the same name as the library, ie tqdm.

from tqdm import tqdm

​ The function of this method is to create a "progress bar object", which needs to pass in a parameter, which is usually an iterable object. for example:

# 1. range函数
bar = tqdm(range(10))
# 2. 列表
bar = tqdm(['a',1,2,3,4])
# 3. 字典
bar = tqdm({
    
    'k':1,'b':2})  # 不过迭代访问时访问的是key
# 4. torch里面的数据加载器(这也是我需要的)
train_bar = tqdm(train_loader)

​ After the tqdm object is created, we no longer use the iterable object just now when iterating, but directly use the tqdm object , as shown below:

# 导包
from tqdm import tqdm
# 1. 创建进度条对象
bar = tqdm(range(10))
# 2. 迭代访问
for i in bar:
	print(i)

​ The result of running the above code is:

insert image description here

Important methods of tqdm

​ This object has a very important method, that is tqdm.desc, this method can realize the content of custom display progress bar.

​ The code demonstrated above just displays the progress bar. If we want the content format of the progress bar to be : 你好,这是我的进度条{}/{}, the contents of the two square brackets are the current index and the total number respectively.

​ Then you can modify the code like this:

# 导包
from tqdm import tqdm
import  time
# 1. 创建进度条对象
bar = tqdm(range(10))
# 2. 显示
for e in bar:
    bar.desc = '你好,这是我的进度条 {}/{}'.format(e+1, 10)

​ The result of the operation is as follows:

insert image description here

​ It is worth reminding again: the iterative object must use the tqdm object, otherwise it will have no effect .

important point

​ Since the above code is very short, it will run in a very short time, so we can't see the transformation of the progress bar. At this time, we can use the time.sleep(0.5)method to increase the delay.

4. Case

Here is a complete demo code for you:

# 导包
from tqdm import tqdm
import  time
# 1. 创建进度条对象
bar = tqdm(range(10))
# 3. 访问
for e in bar:
    bar.desc = 'hello,test {}/{}'.format(e+1, 10)
    time.sleep(0.5)

​ The result is shown in the gif image below: (I recorded the screen using debugging)

insert image description here

5. Summary

​ In fact, there are many progress bar display libraries, and the progress bars implemented by many libraries are very cool, you can try it yourself.

​ But generally speaking, a simple progress bar like tqdm is enough for us to use when writing scripts.

Guess you like

Origin blog.csdn.net/weixin_46676835/article/details/131393311