欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python进度条tqdm

程序员文章站 2024-02-03 20:18:58
...

简介

Python 和 CLI(命令行界面)的快速、可扩展的进度条

tqdm 源自阿拉伯语 taqaddum,意为“进步”,同时也是西班牙语“我爱你”的缩写 (te quiero demasiado)。

Python进度条tqdm




安装

pip install tqdm




初试

直接传入一个可迭代对象,如列表

from tqdm import tqdm
from time import sleep

text = ''
l = ['a', 'b', 'c', 'd']
for x in tqdm(l):
    sleep(0.25)
    text = text + x

Python进度条tqdm




主要用法

基于可迭代对象

直接传入一个可迭代对象,如列表

from tqdm import tqdm
from time import sleep

text = ''
l = ['a', 'b', 'c', 'd']
for x in tqdm(l):
    sleep(0.25)
    text = text + x

Python进度条tqdm

trange(i) 相当于 tqdm(range(i))

from time import sleep
from tqdm import trange

for i in trange(100):
    sleep(0.01)

Python进度条tqdm

允许外部实例化

from tqdm import tqdm
from time import sleep

pbar = tqdm(['a', 'b', 'c', 'd'])
for char in pbar:
    sleep(0.25)
    pbar.set_description('Processing %s' % char)

Python进度条tqdm




手动控制

使用 with 语句手动控制 tqdm() 更新

from tqdm import tqdm
from time import sleep

with tqdm(total=100) as pbar:
    for i in range(10):
        sleep(0.1)
        pbar.update(10)

Python进度条tqdm

像操作文件一样,tqdm() 可以赋值给一个变量,别忘了 delclose()

from tqdm import tqdm
from time import sleep

pbar = tqdm(total=100)
for i in range(10):
    sleep(0.1)
    pbar.update(10)
pbar.close()

Python进度条tqdm




自定义信息

设置描述:set_description(),显示在左边

设置后缀:set_postfix(),显示在右边

from time import sleep
from tqdm import tqdm, trange
from random import random, randint

with trange(10) as t:
    for i in t:
        t.set_description('GEN %i' % i)  # 描述显示在左边
        t.set_postfix(loss=random(), gen=randint(1, 999), str='h', lst=[1, 2])  # 后缀显示在右边,根据参数自动格式化
        sleep(0.1)

with tqdm(total=10, bar_format="{postfix[0]} {postfix[1][value]:>8.2g}", postfix=["Batch", dict(value=0)]) as t:
    for i in range(10):
        sleep(0.1)
        t.postfix[1]["value"] = i / 2
        t.update()

Python进度条tqdm

详细阅读:Description and additional stats




嵌套进度条

from time import sleep
from tqdm.auto import trange

for i in trange(4, desc='1st loop'):
    for j in trange(5, desc='2nd loop'):
        for k in trange(50, desc='3rd loop', leave=False):
            sleep(0.01)

Python进度条tqdm




下载文件

import os
import urllib.request
from tqdm import tqdm


class TqdmUpTo(tqdm):
    def update_to(self, b=1, bsize=1, tsize=None):
        '''更新进度条

        :param b: 已转移的块数
        :param bsize: 每个块的大小
        :param tsize: 总大小
        :return:
        '''
        if tsize is not None:
            self.total = tsize
        self.update(b * bsize - self.n)


url = "https://caspersci.uk.to/matryoshka.zip"
with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1, desc=os.path.basename(url)) as t:
    urllib.request.urlretrieve(url, filename=os.devnull, reporthook=t.update_to, data=None)
    t.total = t.n

Python进度条tqdm
详细阅读:Hooks and callbacks




集成

Pandas

调用 tqdm.pandas

import numpy as np
import pandas as pd
from tqdm import tqdm

df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
tqdm.pandas(desc="my bar!")
df.progress_apply(lambda x: x ** 2)
df.groupby(0).progress_apply(lambda x: x ** 2)

Python进度条tqdm


Keras

from tqdm.keras import TqdmCallback

...

model.fit(..., verbose=0, callbacks=[TqdmCallback()])

实例

from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense
from tqdm.keras import TqdmCallback

(X, y), (_, _) = mnist.load_data()
X = X.reshape(len(X), -1)
X = X.astype('float32')
X = (X - 127) / 127
y = np_utils.to_categorical(y, num_classes=10)
model = Sequential()
model.add(Dense(20, input_shape=(784,), activation='relu'))
model.add(Dense(10, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(X, y, epochs=20, batch_size=64, verbose=0, callbacks=[TqdmCallback()])



IPython/Jupyter

from tqdm.notebook import trange, tqdm
from time import sleep

for i in trange(3, desc='1st loop'):
    for j in tqdm(range(100), desc='2nd loop'):
        sleep(0.01)




备注

  1. 某些 IDE 如 PyCharm,一边输出一边更新,进度条将不连续。




参考文献

  1. tqdm GitHub
  2. tqdm 文档
  3. MNIST & Keras保存模型并预测