讓你的程序炫起來!少有人知道但超酷的Python進度條開源庫
現在,alive-progress 來了,它是一個 Python 下的進度條庫,不僅使用方便而且支持多種炫酷顯示效果!讓我們先來看看示例效果:

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

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

有時候我們想直接操縱顯示的位置,這時候可以設定 alive_bar 的 manual 參數為 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 參數則可以給進度條添加標題(前綴信息),具體使用方法及效果如下:
- 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("Doning Work #%d"%(i+1)) # 更新進度條后綴
- print("Work #%d finished"%i) # 輸出一行信息

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

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

- from alive_progress import show_spinners
- show_spinners() # 查看內置 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 的數目,可以使用 unknown 參數(這時候將替換 bar 為 spinner):
- from alive_progress import alive_bar
- import time
- with alive_bar(
- title="HelloGitHub",
- # 注意:這里 bar 被換成了unknow,內置樣式名稱與 spinner 的相同
- unknown="stars", spinner="message_scrolling"
- ) as bar:
- for i in range(100):
- time.sleep(.1)
- bar()

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

則可以這樣來寫:
- from alive_progress import alive_bar, standard_bar_factory
- import time
- ##-------自定義 bar-------##
- my_bar = standard_bar_factory( # 以下參數均有默認值,不必一次全部修改
- chars="123456789#", # 加載時根據進度依次顯示,長度任意
- borders="<>", # bar 兩頭的邊界
- background=".", # 未加載部分用 "." 填充
- tip=">", # 指示進度方向的引導符號(分割 "#" 與 ".")
- errors="⚠❌" # 發生錯誤時顯示的內容(未完成,溢出)
- )
- ##-------自定義結束-------##
- ##--------動畫演示-------##
- 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)

可以看到字符串挨個循環輸出。
scrolling_spinner_factory:將字符串滾動播出
- from alive_progress import alive_bar, scrolling_spinner_factory
- import time
- my_spinner = scrolling_spinner_factory(
- chars="HelloGitHub", # 想要播放的字符串
- length=15, # spinner 區域寬度
- 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 區域長度
- left_chars="HelloGitHub", # 從右邊進入的字符串
- blank='.', # 空白區域填充字符
- )
- with alive_bar(
- title="HelloGitHub",
- spinner=my_spinner
- ) as bar:
- while True:
- bar()
- time.sleep(.1)

當然,也可以省略 left_chars 這個參數,其效果相當于 I love 將會像彈球一樣左右彈動。
unknown_bar_factory:將 spinner 轉換為能使用在未定義模式中的格式:
- 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)

四、結尾
到這里,相信你已經掌握了 alive_progress 的基本玩法,alive-progress 還提供了一些在不同場合所需的特殊功能,有興趣的朋友可以通過閱讀官方文檔或源代碼進行更加深入的了解。