App下載

Python怎么使用進度條?

夢夢貝莉雅 2021-08-09 15:43:23 瀏覽數(shù) (2571)
反饋

我們在使用Python處理一些比較耗時的操作的時候,如果想要觀察處理進度,可以通過進度條將處理情況可視化展示,以便我們能夠及時了解情況。這對于第三方庫非常豐富的Python來說,并不是什么難事。那么python怎么使用進度條呢?接下來的這篇文章帶你了解。

tqdm就能非常完美的支持和解決這個問題,它是一個快速、擴展性強的進度條工具庫。用戶只需要封裝任意的迭代器 tqdm(iterator),就能在 Python 長循環(huán)中添加一個進度提示信息。

官網(wǎng):

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)

進度條

【例子】循環(huán)外的實例化允許手動控制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),則會顯示預測統(tǒng)計信息。

with也是可選的(可以將tqdm()賦值給變量,但在這種情況下,不要忘記在結(jié)尾處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模塊參數(shù)說明

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()函數(shù):用于設(shè)置/修改進度條的說明。
  • set_postfix()函數(shù):用于設(shè)置/修改后綴(附加統(tǒng)計信息)。
  • update()函數(shù):手動更新進度條。
  • close()函數(shù):清除并關(guān)閉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:可迭代的對象,在手動更新時不需要進行設(shè)置。
  • desc:字符串,左邊進度條描述文字。
  • total:總的項目數(shù)。
  • leave:bool值,迭代完成后是否保留進度條。
  • file:輸出指向位置,默認是終端, 一般不需要設(shè)置。
  • ncols:調(diào)整進度條寬度,默認是根據(jù)環(huán)境自動調(diào)節(jié)長度,如果設(shè)置為0,就沒有進度條,只有輸出的信息。
  • unit:描述處理項目的文字,默認是'it',例如: 100 it/s,處理照片的話設(shè)置為'img' ,則為 100 img/s。
  • unit_scale:自動根據(jù)國際標準進行項目處理速度單位的換算,例如 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中一個快速、擴展性強的進度條工具庫,能讓我們了解代碼的運行進度,也能讓我們的運行結(jié)果看起來顯得更加美觀而又高大上??! 喜歡的小伙伴趕緊用起來吧?。?/p>


到此這篇python怎么使用進度條的文章就介紹到這里了,希望能給各位小伙伴帶來幫助,也希望大家以后多多支持W3Cschool!


0 人點贊