讓你的Python程序在命令行下也有酷炫的進度條
大家好,歡迎來到 Crossin的編程教室 !
今天給大家介紹一個小眾但又非常實用的 Python 三方庫——alive-progress,它可以讓你的Python程序在運行時展示酷炫的進度條效果。
項目地址:https://github.com/rsalmei/alive-progress
不知你是否有過這樣的經(jīng)歷:你寫了一個程序,每次運行都會耗費很長時間。在等待程序運行期間你一次次的按下回車防止程序卡死。亦或者你的任務需要實時掌握程序運行進度但你根本不知道程序執(zhí)行到了哪里...
現(xiàn)在,alive-progress 來了,它是一個 Python 下的進度條庫,不僅使用方便而且支持多種炫酷顯示效果!讓我們先來看看示例效果:

是不是非??犰??
下面讓我們一起玩轉(zhuǎn)這個庫!
一、安裝
在 Python 下使用 pip 進行安裝:
pip install alive-progress
二、快速入門
2.1 直接使用
在循環(huán)中使用 alive-progress 是最常見的用法,腳本可以這樣寫:
# 導入 alive-progress 庫
from alive_progress import alive_bar
import time
# 使用 with 語句創(chuàng)建一個進度條
with alive_bar(100) as bar: # 給 alive_bar 傳入進度條總數(shù)目(這里是 100)
for item in range(100):
# 等待 1s
time.sleep(.1)
#更新進度條,進度 +1
bar()
請注意,如果無法正常顯示動畫則嘗試在 alive_bar 中加上 force_tty=True 參數(shù)。
運行以上代碼我們可以看到在終端中出現(xiàn)了一個還算華麗的動態(tài)進度條:

需要注意的是 alive-progress 并不像 tqdm 等進度條庫一樣會自動更新,只有我們程序調(diào)用了 bar 才會讓進度條 +1.
當然,我們也可以不給進度條傳入總數(shù)目這個參數(shù),此時進度條將不顯示進度,并進入未定義模式:

有時候我們想直接操縱顯示的位置,這時候可以設定alive_bar的manual參數(shù)為True:
from alive_progress import alive_bar
import time
total = 100
with alive_bar(total, manual=True) as bar: # total 可以不指定,這時候只有百分比
bar(0.5) # 進度到 50%
time.sleep(0.5)
bar(0.1) # 進度到 10%
time.sleep(0.5)
bar(0.75) # 進度到 75%
time.sleep(0.5)
bar(1.0) # 進度到 100%
time.sleep(0.5)
bar(10) # 進度到 1000%
for i in range(1,101):
bar(i/100) # 設定進度為 i%
time.sleep(0.05)

當然,在運行過程中我們也需要輸出一些提示信息,直接使用print可以在不破壞進度條的情況下輸出一行提示信息,text方法則可以在進度條尾部添加后綴字符,而title參數(shù)則可以給進度條添加標題(前綴信息),具體使用方法及效果如下:
from alive_progress import alive_bar
import time
# 定義標題(前綴字符)為 HelloGitHub
with alive_bar(10, title="HelloGitHub") as bar:
for i in range(10):
time.sleep(1)
bar() # 讓進度 +1
bar.text("Processing Work #%d"%(i+1)) # 更新進度條后綴
print("Work #%d finished"%i) # 輸出一行信息

2.2 添點花樣
看多了傳統(tǒng)的進度條樣式想換換花樣?沒問題,alive-progress 不僅內(nèi)置了多種進度條樣式,還支持自定義格式。
進度條可以自定義的樣式分為兩種:bar和spinner,只需要在調(diào)用alive_bar的時候傳入對應的參數(shù)即可。

以這個進度條為例,中間最長的是bar,旁邊來回晃動的www.HelloGitHub.com是spinner。
alive-progress 內(nèi)置了多種 bar 和 spinner 樣式,只需要調(diào)用show_bars或者show_spinners即可快速預覽相應的樣式,例如:
from alive_progress import show_bars
show_bars() # 查看內(nèi)置 bar 樣式

from alive_progress import show_spinners
show_spinners() # 查看內(nèi)置 spinner 樣式

默認樣式使用起來非常簡單,例如我想使用bubbles這個 bar 和message_scrolling這個 spinner,直接傳入對應名稱即可:
from alive_progress import alive_bar
import time
# 直接傳入對應名字即可
with alive_bar(
100,
title="HelloGitHub",
bar="bubbles", spinner="message_scrolling"
) as bar:
for i in range(100):
time.sleep(.1)
bar()

如果不知道total的數(shù)目,可以使用unknown參數(shù)(這時候?qū)⑻鎿Q bar 為 spinner):
from alive_progress import alive_bar
import time
with alive_bar(
title="HelloGitHub",
# 注意:這里 bar 被換成了unknow,內(nèi)置樣式名稱與 spinner 的相同
unknown="stars", spinner="message_scrolling"
) as bar:
for i in range(100):
time.sleep(.1)
bar()

三、私人定制
或許比起直接使用內(nèi)置模板你更喜歡自己定制的進度條,對此 alive-progress 也提供了對應方法。
3.1 定制 bar
使用standard_bar_factory方法可以快速定制 bar,bar 可以設置的參數(shù)有五個:
chars:正在執(zhí)行單元的動畫,按照進度依次顯示。borders:進度條邊界,顯示在左右兩邊。background:未執(zhí)行到單元顯示的內(nèi)容。tip:執(zhí)行單元的前導符號。errors:出錯時(進度未走全,超出 total 值等)時顯示的字符。
例如我們想做一個如圖所示的 bar:

則可以這樣來寫:
from alive_progress import alive_bar, standard_bar_factory
import time
##-------自定義 bar-------##
my_bar = standard_bar_factory( # 以下參數(shù)均有默認值,不必一次全部修改
chars="123456789#", # 加載時根據(jù)進度依次顯示,長度任意
borders="<>", # bar 兩頭的邊界
background=".", # 未加載部分用 "." 填充
tip=">", # 指示進度方向的引導符號(分割 "#" 與 ".")
errors="??" # 發(fā)生錯誤時顯示的內(nèi)容(未完成,溢出)
)
##-------自定義結束-------##
##--------動畫演示-------##
with alive_bar(
10,
title="HelloGitHub",
bar=my_bar, # 這里傳入剛剛自定義的 bar
spinner="message_scrolling",
manual=True
) as bar:
for i in range(50):
time.sleep(.1)
bar(i/100)
bar(.5)
time.sleep(2)
bar(10)
print("上溢")
time.sleep(1)
bar(1)
print("100% 完成")
time.sleep(1)
bar(.1)
print("未完成")
3.2 定制 spinner
對于 spinner,alive-progress 提供了更多種的動畫定義方式:
frame_spinner_factory:將傳入的字符串挨個輸出:
from alive_progress import alive_bar, frame_spinner_factory
import time
my_spinner = my_spinner = frame_spinner_factory(
r'-----',
r'1----',
r'-2---',
r'--3--',
r'---4-',
r'----5'
) # 直接傳入字符串
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)

可以看到字符串挨個循環(huán)輸出。
scrolling_spinner_factory:將字符串滾動播出
from alive_progress import alive_bar, scrolling_spinner_factory
import time
my_spinner = scrolling_spinner_factory(
chars="HelloGitHub", # 想要播放的字符串
length=15, # spinner 區(qū)域?qū)挾?/span>
blank='.' # 空白部分填充字符
)
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)

bouncing_spinner_factory:將兩個字符串交替滾動播出
from alive_progress import alive_bar, bouncing_spinner_factory
import time
my_spinner = bouncing_spinner_factory(
right_chars="I love", # 從左邊進入的字符串
length=15, # spinner 區(qū)域長度
left_chars="HelloGitHub", # 從右邊進入的字符串
blank='.', # 空白區(qū)域填充字符
)
with alive_bar(
title="HelloGitHub",
spinner=my_spinner
) as bar:
while True:
bar()
time.sleep(.1)

當然,也可以省略 left_chars 這個參數(shù),其效果相當于 I love 將會像彈球一樣左右彈動。
unknown_bar_factory:將 spinner 轉(zhuǎn)換為能使用在未定義模式中的格式:
from alive_progress import alive_bar, unknown_bar_factory, bouncing_spinner_factory
import time
my_spinner = bouncing_spinner_factory("www.HelloGitHub.com",15,hiding=False)
my_unknown_bar = unknown_bar_factory(my_spinner) # 傳入定義的 spinner
with alive_bar(
title="HelloGitHub",
unknown=my_unknown_bar
) as bar:
while True:
bar()
time.sleep(.1)

四、結尾
到這里,相信你已經(jīng)掌握了 alive_progress 的基本玩法,alive-progress 還提供了一些在不同場合所需的特殊功能,有興趣的朋友可以通過閱讀官方文檔或源代碼進行更加深入的了解。
如果文章對你有幫助,歡迎轉(zhuǎn)發(fā)/點贊/收藏~
作者:Anthony
_往期文章推薦_
