Some operations of python's tqdm

The main parameters

iterable: iterable object, no need to set when manually updating
desc: str, descriptive text of the progress bar on the left
total: total number of items
leave: bool, whether to retain the progress bar after execution is completed
file: output pointing position, default It is a terminal, generally there is no need to set
ncols: adjust the width of the progress bar, the default is to automatically adjust the length according to the environment, if set to 0, there will be no progress bar, only output information
unit: text describing the processing project, the default is 'it', For example: 100 it/s, if it is set to 'img' when processing photos, it will be 100 img/sunit_scale: Automatically convert project processing speed units according to international standards, for example 100000 it/s >> 100k it/s colour: progress
bar Color, for example: 'green', '#00ff00'.

Use a green background and white text style in the progress bar

# 迭代 100 次
for i in tqdm(range(100), bar_format='{bar:40}{n}/{total}'):
    # 模拟一些耗时操作
    time.sleep(0.1)

Insert image description here
Note: The bar_format parameter is used to set the style of the progress bar. Among them, {bar} represents the progress bar itself, {n} represents the current progress, and {total} represents the total progress.

Pass in list

for i in tqdm([1, 2, 4, 5, 3]):
    # 模拟一些耗时操作
    time.sleep(0.1)

Insert image description here

Pass in range

for i in tqdm(range(100)):
    # 模拟一些耗时操作
    time.sleep(0.1)

Insert image description here

trange() provided by tqdm instead of tqdm(range())

for i in trange(100):
    # 模拟一些耗时操作
    time.sleep(0.05)

Insert image description here

Use set_description() to add descriptive content in front of the progress bar to make it look more humane.

pbar = tqdm(range(10))
for char in pbar:
    pbar.set_description("进度 %d" % char)
    # 模拟一些耗时操作
    time.sleep(0.05)

Insert image description here

Use the update function to customize the progress bar ratio. For example, we divide the 100 progress task into 4 progress displays.

with tqdm(total=100) as pbar:
    for i in range(1, 5):
        # 模拟一些耗时操作
        time.sleep(0.5)
        pbar.update(10*i)

Customize progress bar color

for i in tqdm(range(100), colour="#00ff00"):
    # 模拟一些耗时操作
    time.sleep(0.05)

Insert image description here

Progress bar for multiple loops

for i in trange(3, colour='WHITE', desc='outer loop'):
    for j in trange(100, colour='green', desc='inner loop', leave=False):
        time.sleep(0.01)

Guess you like

Origin blog.csdn.net/s_daqing/article/details/131317055