Python 中的進度條
進度條在 Python 中是視覺化的指示器,用於提供任務或操作進度的反饋。它們對於長時間執行的程序或迭代非常有用,因為它們可以顯示已完成的工作量以及剩餘的工作量。
進度條通常包含一個視覺化表示,例如水平條或文字表示,它會動態更新以反映任務的進度。它還包括其他資訊,如完成百分比、剩餘估計時間以及任何相關的訊息或標籤。
以下是 Python 中進度條所起的作用。
視覺反饋
時間估計
使用者體驗
以下是一些在 Python 中實現進度條的方法。讓我們詳細瞭解每種方法。
使用 tqdm 庫
tqdm 庫是 Python 中建立進度條的常用選擇。它提供了一個簡單靈活的 API,只需少量程式碼即可建立進度條。tqdm 庫會根據可迭代物件的長度自動計算並顯示迴圈的進度。它還提供了其他功能,如經過時間、剩餘估計時間以及可自定義的外觀。
要使用 tqdm,我們需要先使用 Python 環境中的 pip 安裝它。
示例
pip install tqdm
輸出
F:\>pip install tqdm Defaulting to user installation because normal site-packages is not writeable Collecting tqdm Downloading tqdm-4.65.0-py3-none-any.whl (77 kB) ---------------------------------------- 77.1/77.1 kB 1.1 MB/s eta 0:00:00 Requirement already satisfied: colorama in c:\users\krishna\appdata\roaming\python\python311\site-packages (from tqdm) (0.4.6) Installing collected packages: tqdm WARNING: The script tqdm.exe is installed in 'C:\Users\Krishna\AppData\Roaming\Python\Python311\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed tqdm-4.65.0
示例
在這個示例中,我們迭代資料範圍,tqdm 包裝了迴圈,並在每次迭代完成後即時顯示一個更新的進度條。time.sleep(0.5) 行模擬了每次迭代中完成的一些工作。
from tqdm import tqdm import time data = range(10) for item in tqdm(data): time.sleep(0.5)
輸出
100%|██████████████████████████████████████████| 10/10 [00:05<00:00, 1.99it/s]
手動更新進度條
在這種方法中,我們手動計算進度百分比,並透過使用回車符 (\r) 字元列印到控制檯來更新進度條,以覆蓋前一行。以下是使用 print 語句手動更新進度條的示例。
示例
import time
total_iterations = 10
for i in range(total_iterations):
time.sleep(0.5)
progress = (i + 1) / total_iterations * 100
print(f"Progress: {progress:.1f}%", end="\r")
輸出
Progress: 10.0% Progress: 20.0% Progress: 30.0% Progress: 40.0% Progress: 50.0% Progress: 60.0% Progress: 70.0% Progress: 80.0% Progress: 90.0% Progress: 100.0%
使用第三方庫
除了 tqdm 之外,還有其他第三方庫,如 progressbar2 和 alive_progress,它們為進度條提供了更多功能和自定義選項。
progressbar2
progressbar2 是另一個流行的庫,它提供了各種進度條樣式和選項。要使用 progressbar2 庫,我們首先必須使用 pip 安裝它。
pip install progressbar2
示例
在這裡,我們使用 widgets 列表建立了一個帶有自定義部件的進度條。我們使用 max_value 引數將進度條的最大值指定為 10。bar.update(i + 1) 行更新每次迭代的進度條。
from progressbar import ProgressBar
import time
total_iterations = 10
with ProgressBar(max_value=total_iterations) as bar:
for i in range(total_iterations):
time.sleep(0.5)
bar.update(i)
輸出
100% (10 of 10) |########################| Elapsed Time: 0:00:04 Time: 0:00:04
Alive-progress
Alive-progress 是一個現代的、功能豐富的庫,用於建立具有高階自定義選項的互動式進度條。要使用 alive-progress,首先必須使用 pip 安裝庫。
pip install alive-progress
Defaulting to user installation because normal site-packages is not writeable
Collecting alive-progress
Downloading alive_progress-3.1.4-py3-none-any.whl (75 kB)
-------------------------------------- 75.9/75.9 kB 842.2 kB/s eta 0:00:00
Collecting about-time==4.2.1 (from alive-progress)
Downloading about_time-4.2.1-py3-none-any.whl (13 kB)
Collecting grapheme==0.6.0 (from alive-progress)
Downloading grapheme-0.6.0.tar.gz (207 kB)
-------------------------------------- 207.3/207.3 kB 4.2 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: grapheme
Building wheel for grapheme (setup.py) ... done
Successfully built grapheme
示例
在這個示例中,我們使用 alive_bar 上下文管理器來建立進度條。len(data) 指定迭代的總數。bar() 函式在迴圈中被呼叫以更新進度條。
from alive_progress import alive_bar
import time
data = range(10)
with alive_bar(len(data)) as bar:
for item in data:
time.sleep(0.5)
bar()
輸出
|████████████████████████████████████████| 10/10 [100%] in 5.1s (1.91/s) ←[K ←[?25h←[J
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP