假设您使用 python 开发了一个脚本,或者 bash 与互联网对话并获取文件或废弃数据等,或者只是需要时间来执行。
当您运行该脚本时,您真的不知道它是否正在运行或卡住,或者我需要等待多长时间。 正确的?
如果有加载屏幕或进度条指示剩余时间或使用百分比的剩余进度,这将不是问题。 我们可以使用两者来编写进度条。 在本教程中,我们将了解怎样使用 Python 和 Bash 在终端中创建进度条。
目录
先决条件
对于本教程,我们将使用 Ubuntu 来运行我们的脚本。 Ubuntu是基于Linux的操作系统。 在 Ubuntu Python 和 bash 已安装,因此无需再次安装。 遵循本教程,任何基于 debian 的操作系统都不会出现任何问题。
如果您使用的是 Windows 操作系统,请确保您已安装 python 并设置了环境变量,因为您需要从终端运行脚本。 如果你也想试试 bash 在 Windows 上,您需要 WSL(适用于 Linux 的 Windows 子系统)。
使用 Python 的进度条
Python 是一种高级编程语言。 并且可以用来实现几乎任何事情。 python 中有许多很酷的库来创建进度条。 在本教程中,我们将看到其中的一些:
1) TQDM
Tqdm 是一个易于使用的库。 它尤其适用于循环。 它给出了循环的进度表。 只需 2 行代码,您就完成了。
安装
点击终端并执行以下命令:
❯ pip3 install tqdm
用法
使用 tqdm 非常简单,只需将其添加到您的 for 循环中,如下所示:
from tqdm import tqdm import time for i in tqdm(range(20), desc="tqdm() Progress Bar"): time.sleep(0.1) # some tasks here # some more operations here
这里 desc
是用于描述进度条的众多参数之一。 为了 example获取文件时可以“下载”。
输出
Tqdm 可以在您的代码中以多种方式使用。 你应该关注官方 文件 了解更多片段和功能。
2) ALIVE_PROGRESS
这是另一个带有一些很酷的动画的进度条库。 Alive_progress 简直太棒了,因为在这里我们可以完全控制进度条,并且可以将其设置为动态。 它比 tqdm 好很多,因为它有更多的功能,我们有不同的动画可供选择。
安装
点击终端并执行以下命令:
❯ pip install alive-progress
用法
如果您有从 Internet 下载文件的脚本,则为示例代码:
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
是进度条的标题。
输出

我们还可以更改进度条的主题,如下所示:
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()
它还支持微调器。
输出

您可以从许多可用的主题和微调器中进行选择。 您可以一次显示它们,然后选择最适合您的:
from alive_progress.styles import showtime showtime()
输出

访问 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。
编写 Shell 脚本是为了完成特定任务,例如将大量文件复制到文件夹或克隆 GitHub 存储库并自动在其中安装所有依赖项。 在后台执行操作时显示进度条将是锦上添花。
让我们从创建一个 shell 文件开始。 例如: test.sh
. 我正在使用 gedit 文本编辑器,您可以使用您的选择之一。
用法
复制以下代码以获得简单的进度条
#!/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
输出

这是另一个 example 如果您想要终端中的动画和彩色加载屏幕。
#!/垃圾桶/bash 函数 pro { bar="" for (( x=50; x <= 100; x++ )); do sleep 0.05 bar="${bar}=" echo -ne "$bar ${x}%r" done echo -e "n" } function proo { barr="" for (( y=50; y <= 100; y++ )); 做睡眠 0.05 barr="${barr} " echo -ne "r" echo -ne "e[43m$barre[0m" local left="$(( 100 - $y ))" printf " %${left}s" echo -n "${y}%" done echo -e "n" } pro echo "" echo "This was a progress bar with percentage" echo "" proo echo "This was a progress bar with color"
OUTPUT

If you want to integrate a progress bar in a GUI-based command-line application the following code may be suitable for you.
#!/bin/bash seq 1 100|dialog --title "Downloading Files" --gauge "Please wait ..." 11 60 0 sleep 1
OUTPUT

It can also be modified to show a description of the operations and add an appropriate title:
#!/bin/bash phasess=( 'Ready to perform operations...' 'Performing some operations...' 'Please wait ...' 'almost there...' ) for i in $(seq 1 100); do sleep 0.04 if [ $i -eq 100 ]; 然后 echo -e "XXXn100nDone!nXXX" elif [ $(($i % 25)) -eq 0 ]; 然后让 "phase = $i / 25" echo -e "XXXn$in${phasess[phase]}nXXX" else echo $i fi done |whiptail --title '这是一个 GUI 进度条' --gauge "${phasess[0]}" 6 60 0
输出

总结! 
现在您可以使您的命令行应用程序更加用户友好和灵活。 希望本教程对您有所帮助。
推荐文章
- 用这个简单的技巧为你的提示着色
- 在终端中轻松更改默认 Shell
- 使用 Source 在 Debian/Ubuntu 上安装最新的 Python