from alive_progress import alive_bar
import time
for i in range(3):
with alive_bar(100, ctrl_c=False, title=f'Downloading {i}') as bar:
for i in range(100):
time.sleep(0.02)
bar()
在哪裡 100 進程的最大長度。 ctrl_c = False 方法 CTRL + C 在進度條中運行代碼不起作用(使用 CTRL+C 在終端中完成任務)。當重要任務正在運行並且您不希望用戶完成它時,這尤其有用。默認 True.什麼時候 title 進度條的標題。
輸出
alive_progress 庫
您還可以像這樣更改進度條的主題:
from alive_progress import alive_bar
import time
for i in range(3):
with alive_bar(100, ctrl_c=False, title=f'Downloading {i}', bar="halloween", spinner="twirls") as bar:
for i in range(100):
time.sleep(0.02)
bar()
還支持微調器。
輸出
alive_progress 庫
您可以從許多可用的主題和微調器中進行選擇。您可以一次查看所有內容並選擇最佳的。
from alive_progress.styles import showtime
showtime()
輸出
alive_progress 庫
有關更多信息,請訪問我們的 github 存儲庫。
3、你好
Halo 更像是一個微調器,而不是加載屏幕。當操作不需要太多時間時可以使用它。
安裝
訪問您的終端並運行以下命令:
❯ pip install halo
如何使用
from __future__ import unicode_literals
import os
import sys
import time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from halo import Halo
success_message="Loading success"
failed_message="Loading failed"
unicorn_message="Loading unicorn"
spinner = Halo(text=success_message, spinner="dots")
try:
spinner.start()
time.sleep(1)
spinner.succeed()
spinner.start(failed_message)
time.sleep(1)
spinner.fail()
spinner.start(unicorn_message)
time.sleep(1)
spinner.stop_and_persist(symbol="?".encode('utf-8'), text=unicorn_message)
except (KeyboardInterrupt, SystemExit):
spinner.stop()
輸出
光環圖書館
檢查 github 存儲庫以獲取更多詳細信息。
4)阿斯平
另一個用於命令行應用程序的微調器庫。 Yaspin 提供多種選擇。適用於需要比平時更長的操作。
安裝
訪問您的終端並運行以下命令:
❯ pip install yaspin
如何使用
import time
from random import randint
from yaspin import yaspin
from yaspin.spinners import Spinners
with yaspin(text="Loading", color="yellow") as spinner:
time.sleep(1) # time consuming code
success = randint(0, 1)
if success:
spinner.ok("✅ ")
else:
spinner.fail("? ")
with yaspin(Spinners.earth, text="Earth") as sp:
time.sleep(1) # time consuming code
# change spinner
sp.spinner = Spinners.moon
sp.text = "Moon"
time.sleep(1)
with yaspin(text="Colors!") as sp:
# Support all basic termcolor text colors
colors = ("red", "green", "yellow", "blue", "magenta", "cyan", "white")
for color in colors:
sp.color, sp.text = color, color
time.sleep(0.5)
輸出
阿斯平圖書館
官方網站在這裡。
使用 Bash 的進度條
Bash 基本上是基於 GNU 的操作系統的命令語言或命令行解釋器。 您可以在 Linux 操作系統中看到它。 不是Linux。 Linux 是一個內核。 Bash 在基於 GNU 的操作系統上運行。例如:Debian。
#!/bin/bash
bar="####################"
echo ""
for i in {1..20}; do
echo -ne "rDownloading ${bar:0:$i}"
sleep .1
done
echo ""
echo ""
echo "This is a simple progress bar"
您可以使用終端運行代碼:
❯ bash test.sh
輸出
bash 進度條
如果您希望您的設備顯示動畫和彩色加載屏幕,這是另一個示例。
#!/垃圾桶/bash
函数 pro { bar="" for (( x=50; x
OUTPUT
bash progress bar
If you want to integrate a progress bar in a GUI-based command-line application the following code may be suitable for you.