Python 进度条

tqdm

tqdm 是最常用的进度条库。简单、好用。

image

pip install tqdm
from time import sleep
from tqdm import tqdm

for _ in tqdm(range(100)):
    sleep(0.1)

Rich

rich 能显示美观的彩色进度条。pip 安装时显示的进度条就是这种效果。

pip install rich
from time import sleep
from rich.progress import track

for _ in track(range(100)):
    sleep(0.1)

Alive-Progress

alive-progress 可以以多种花样显示进度条。

image

pip install alive-progress
from time import sleep
from alive_progress import alive_it

for _ in alive_it(range(100)):
    sleep(0.1)

Progress

progress 的显示效果类似 apt 安装软件包。

image

pip install progress
from time import sleep
from progress.bar import Bar

for _ in Bar('Processing').iter(range(100)):
    sleep(0.1)

Halo

halo 的显示方式类似 npm 安装软件包。严格意义上来说 halo 不是进度条。

pip install halo
from time import sleep
from halo import Halo

with Halo(text='Loading', spinner='dots'):
    sleep(5)

Troubleshooting

在 IPython 中使用 Halo 出现错误:

>>> from time import sleep
>>> from halo import Halo
>>>
>>> with Halo(text='Loading', spinner='dots'):
>>>     sleep(5)
>>>
Error in callback <function Halo.__init__.<locals>.clean_up at 0x7f504dbc5e40> (for post_run_cell), with arguments args (<ExecutionResult object at 7f504dffc050, execution_count=1 error_before_exec=None error_in_exec=None info=<ExecutionInfo object at 7f504dffc590, raw_cell="from time import sleep
from halo import Halo

with.." store_history=True silent=False shell_futures=True cell_id=None> result=None>,),kwargs {}:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: Halo.__init__.<locals>.clean_up() takes 0 positional arguments but 1 was given

解决方法:在 .py 文件中运行 halo,不要在 IPython 或 JupyterLab 中运行 halo。

posted @ 2025-05-17 23:42  Undefined443  阅读(14)  评论(0)    收藏  举报