刚使用python的progressbar2,只有官方文档,且文档太多了,平时用到的功能也不多。所以在此记录一般需要的功能。PS:应该会长期更新吧~
github地址
官网文档
官网example
一、安装
pip install progressbar2
1
二、使用
2.1 基础版
会根据for循环的次数来自动生成,只需要在for循环外包裹一层progressbar.progressbar()
import progressbarimport timebar = progressbarfor i in bar.progressbar(range(100)): time.sleep(0.2)
1
2
3
4
5
6
7
结果如下,其中ETA为预测距离结束时间还有X:XX:XX
2.1 稍加进步版
由于我们的代码可能使用enumerate,因此使用此方法可以自定义起止位置。
下方代码只起解释作用,并不可运行
import progressbarbar = progressbar.ProgressBar(start, end)bar.start()for index, data in enumerate(Dataloader): # index取值范围(start, end) bar.update(index) # 其他工作bar.finish()
1
2
3
4
5
6
7
8
9
10
11
2.2 作者回复(enumerate版):
关于使用enumerate的问题,我在github上以及提出了issues并得到了回复,看来这个问题解决了hhhhh
两种自动更新:
import timeimport progressbarfor i, value in enumerate(progressbar.progressbar(range(100))): time.sleep(0.02)# in either order, this works too:for i, value in progressbar.progressbar(enumerate(range(100))): time.sleep(0.02)
1
2
3
4
5
6
7
8
9
手动更新:
import timeimport progressbarbar = progressbar.ProgressBar()for i, value in enumerate(range(100)): time.sleep(0.02) bar.update(i)
1
2
3
4
5
6
7
用到新的内容时再更新~