搜索
首页后端开发Python教程如何用Python中progress库实现进度条

如何用Python中progress库实现进度条

Apr 25, 2023 am 09:01 AM
pythonprogress

progress库安装和介绍

1.安装progress库

progress是Python第三方库,在终端执行 pip 命令安装。

pip install progress

2.progress进度条效果展示

在官网可以看到progress能实现的各种进度条效果,如下动图。

如何用Python中progress库实现进度条

progress实现进度条

# coding=utf-8
from progress.bar import Bar
import time

# 创建Bar类的实例
bar = Bar('MyProcess:', max=100)
# 循环处理某业务,调用bar对象的next()方法,循环次数等于max
for _ in range(100):
	# Do some work
    time.sleep(0.05)
    bar.next()
# 循环完成后调用finish()方法
bar.finish()

运行效果:

如何用Python中progress库实现进度条

使用 progress 库实现进度条很简单,从 progress 库的 bar.py 中导入 Bar 类,实例化一个对象,然后循环处理业务和调用 next() 方法,处理完成后调用 finish() 方法结束进度条。

Bar 类的主要参数有:

  • message: 进度条显示的信息,这个参数默认可以用位置参数传给 Bar 。

  • width: 修改进度条的宽度,默认32。

  • max: 进度条被分成几等份,如100。

  • suffix: 进度百分比显示的格式,默认 ‘%(index)d/%(max)d’ 。

  • fill: 进度条用什么填充,默认’#’ 。

  • empty_fill: 未加载的进度条用什么填充,默认空格’ ’ 。

  • bar_prefix: 进度条前缀,默认’ |’ 。

  • bar_suffix: 进度条后缀,默认’| ’ 。

  • color: 进度条的颜色,默认None。

PyCharm进度条显示问题解决

大部分Python程序员都使用PyCharm,如果在PyCharm中运行进度条代码后没有显示进度条效果,可以按照如下步骤设置,再重新运行代码。

Step1. 点击PyCharm上方的 Run 按钮,然后点击 Edit Configurations,进入运行配置页面。

如何用Python中progress库实现进度条

Step2. 将 Emulate terminal in output console 选项打钩,然后重新运行代码。

如何用Python中progress库实现进度条

细心的小伙伴应该发现了,上面的进度条代码在PyCharm中运行完成后,进度条下有一串奇怪的字符: ‘?[?25h’ ,在终端中用脚本方式 python progress_demo.py 运行相同的代码,不会有这串符号。

这串字符表示的是光标,在PyCharm中运行完成后,光标被输出在控制台上了。解决方法为:按住 Ctrl 键,点击导入的 Bar 类名,跳转到源码 bar.py 中,再按住 Ctrl 键点击 Bar 类继承的 Progress 类,进入 progress 库的 __init__.py 文件中,将该文件中的 SHOW_CURSOR 变量改为空字符串。

如何用Python中progress库实现进度条

修改源码后再重新运行代码,光标不显示了,那一串字符也就不显示了。

如何用Python中progress库实现进度条

在PyChram中运行进度条,还可能会遇到其他显示问题,建议出现问题时,用脚本的方式运行,以脚本运行的结果为准,PyChram中运行只用来调试代码。

进度条代码的另外两种写法

1. 上下文管理器: 使用 with … as … 上下文管理器的方式编写进度条,进度条完成后无需调用 finish() 方法。

with Bar('Processing', max=20) as bar:
    for i in range(20):
        time.sleep(0.05)
        bar.next()

2. 使用 iter() 方法: 使用 iter() 方法可以简化迭代器的工作,不需要调用 next() 方法。

for i in Bar('Processing').iter(range(100)):
    time.sleep(0.05)

这两种实现方式执行的结果与最开始的实现方式一样。

实现更多种类的进度条

1. Bar 系列

前文中点进了progress的源码,可以看到 Bar 类继承自 Progress 类,Progress 类继承自 Infinite 类,Progress 类和 Infinite 类实现了进度条的基本功能。

在 bar.py 中,除了 Bar 类,还有6个类,ChargingBar, FillingSquaresBar, FillingCirclesBar, IncrementalBar, PixelBar, ShadyBar,它们分别是 Bar 类的子类或孙类,继承了进度条的功能,在每一个类中只是修改了 suffix, fill, empty_fill, bar_prefix, bar_suffix这些变量的值,修改了进度条的样式。

使用很简单,只需要导入这些类,将上面代码中的 Bar 换成这些类就行了,如 FillingSquaresBar。

from progress.bar import Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar, IncrementalBar, PixelBar, ShadyBar
import time

bar = FillingSquaresBar('MyProcess:', max=100)
for i in range(100):
    time.sleep(0.05)
    bar.next()
bar.finish()

运行效果:

如何用Python中progress库实现进度条

还可以自己修改上面的参数,设置个性化的进度条,本文暂不扩展,以后有空可以专门写文章介绍。

2. Spinner 系列

progress 库的 spinner.py 中实现了 5 个类,Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner。

from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner

for i in Spinner('MySpinner:').iter(range(100)):
    time.sleep(0.01)

运行效果:

如何用Python中progress库实现进度条

3.Counter 系列

progress 库的 counter.py 中实现了 4 个类,Counter, Countdown, Stack, Pie。

from progress.counter import Counter, Countdown, Stack, Pie
import time

for i in Counter('Loading:').iter(range(100)):
    time.sleep(0.01)

运行效果:

如何用Python中progress库实现进度条

汇总

接下来将上面介绍的所有种类的进度条全部放到一起,代码如下:

from progress.bar import Bar, ChargingBar, FillingSquaresBar, \
    FillingCirclesBar, IncrementalBar, PixelBar, ShadyBar
from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner
from progress.counter import Counter, Countdown, Stack, Pie
import time
import random

progess_list = [Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar, IncrementalBar,
                PixelBar, ShadyBar, Spinner, PieSpinner, MoonSpinner, LineSpinner,
                PixelSpinner, Counter, Countdown, Stack, Pie]
for P in progess_list:
    for i in P(P.__name__ + ':').iter(range(100)):
        time.sleep(0.01)

bar = IncrementalBar('Random:', suffix='%(index)d%%')
for i in range(100):
    bar.goto(random.randint(0, 100))
    time.sleep(0.01)
bar.finish()

运行效果:

如何用Python中progress库实现进度条

运行代码后可以得出所有种类的进度条,与文章开头从官网截的图效果相同。

以上是如何用Python中progress库实现进度条的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
可以在Python数组中存储哪些数据类型?可以在Python数组中存储哪些数据类型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?如果您尝试将错误的数据类型的值存储在Python数组中,该怎么办?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python标准库的哪一部分是:列表或数组?Python标准库的哪一部分是:列表或数组?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您应该检查脚本是否使用错误的Python版本执行?您应该检查脚本是否使用错误的Python版本执行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!