当前位置:首页 » python教程 » 正文

Python进度条的使用

看: 880次  时间:2021-07-24  分类 : python教程

在使用Python处理比较耗时操作的时候,为了便于观察处理进度,就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了解情况。这对于第三方库非常丰富的Python来说,并不是什么难事。

tqdm就能非常完美的支持和解决这个问题,它是一个快速、扩展性强的进度条工具库。用户只需要封装任意的迭代器 tqdm(iterator),就能在 Python 长循环中添加一个进度提示信息。

官网:

https://github.com/tqdm/tqdm

安装:

pip install tqdm

基于迭代器的使用方式

【例子】使用tqdm(iterator)

import time
from tqdm import tqdm

for i in tqdm(range(100)):
    time.sleep(0.05)

for i in tqdm(list('abcdefgh')):
    time.sleep(0.05)

for i in tqdm(range(100), desc='Processing'):
    time.sleep(0.05)

【例子】trange(N)tqdm(range(N))的一种简单写法

import time
from tqdm import tqdm, trange

for i in trange(100):
    time.sleep(0.05)

【例子】循环外的实例化允许手动控制tqdm()

import time
from tqdm import tqdm

pbar = tqdm(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
for i in pbar:
    pbar.set_description('Processing ' + i)
    time.sleep(0.2)

【例子】

import time
from tqdm import tqdm
from random import random, randint

with tqdm(range(100)) as pbar:
    for i in pbar:
        pbar.set_description("GEN %d" % i)
        pbar.set_postfix({'loss': random(), 'gen': randint(1, 999)})
        time.sleep(0.1)

基于手动进行更新

【例子】使用with语句手动控制tqdm()更新

import time
from tqdm import tqdm

with tqdm(total=200) as pbar:
    pbar.set_description("Processing")
    for i in range(20):
        time.sleep(0.1)
        pbar.update(10)

如果提供了可选变量total(或带有len()的iterable),则会显示预测统计信息。

with也是可选的(可以将tqdm()赋值给变量,但在这种情况下,不要忘记在结尾处delclose()

import time
from tqdm import tqdm

pbar = tqdm(total=200)
pbar.set_description("Processing")
for i in range(20):
    time.sleep(0.1)
    pbar.update(10)

pbar.close()

tqdm模块参数说明

class tqdm(Comparable):
    """
    Decorate an iterable object, returning an iterator which acts exactly
    like the original iterable, but prints a dynamically updating
    progressbar every time a value is requested.
    """

    def set_description(self, desc=None, refresh=True):
    def set_postfix(self, ordered_dict=None, refresh=True, **kwargs):
    def update(self, n=1):
    def close(self):
  • set_description()函数:用于设置/修改进度条的说明。
  • set_postfix()函数:用于设置/修改后缀(附加统计信息)。
  • update()函数:手动更新进度条。
  • close()函数:清除并关闭progressbar。
class tqdm(Comparable):
    """
    Decorate an iterable object, returning an iterator which acts exactly
    like the original iterable, but prints a dynamically updating
    progressbar every time a value is requested.
    """

    def __init__(self, iterable=None, desc=None, total=None, leave=False,
           file=sys.stderr, ncols=None, mininterval=0.1,
           maxinterval=10.0, miniters=None, ascii=None,
           disable=False, unit='it', unit_scale=False,
           dynamic_ncols=False, smoothing=0.3, nested=False,
           bar_format=None, initial=0, gui=False):
  • iterable:可迭代的对象,在手动更新时不需要进行设置。
  • desc:字符串,左边进度条描述文字。
  • total:总的项目数。
  • leave:bool值,迭代完成后是否保留进度条。
  • file:输出指向位置,默认是终端, 一般不需要设置。
  • ncols:调整进度条宽度,默认是根据环境自动调节长度,如果设置为0,就没有进度条,只有输出的信息。
  • unit:描述处理项目的文字,默认是'it',例如: 100 it/s,处理照片的话设置为'img' ,则为 100 img/s。
  • unit_scale:自动根据国际标准进行项目处理速度单位的换算,例如 100000 it/s >> 100k it/s。

【例子】

import time
from tqdm import tqdm

with tqdm(total=100000, desc='Example', leave=True, ncols=100, unit='B', unit_scale=True) as pbar:
    for i in range(10):
        time.sleep(0.5)
        pbar.update(10000)

tqdm源自阿拉伯语单词taqaddum,意思是“progress(进展)”,是python中一个快速、扩展性强的进度条工具库,能让我们了解代码的运行进度,也能让我们的运行结果看起来显得更加美观而又高大上!! 喜欢的小伙伴赶紧用起来吧!!

到此这篇关于Python进度条的使用的文章就介绍到这了,更多相关Python进度条内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!