Python进度条-progressbar2模块
来源:互联网 发布:淘宝开店怎么上货 编辑:程序博客网 时间:2024/05/22 00:37
Python Progressbar模块提供基于文本的可视化进度条,通常用在显示下载进度、显示任务的执行进度等等。
安装Progressbar模块:
1
2
3
# pip install progressbar
或安装最新版:
# pip install git+https://github.com/WoLpH/python-progressbar
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__import print_function
import sys
importtime
fromprogressbar importAnimatedMarker,Bar,BouncingBar,Counter,ETA,\
FileTransferSpeed,FormatLabel,Percentage,\
ProgressBar,ReverseBar,RotatingMarker,\
SimpleProgress,Timer,AdaptiveETA,AbsoluteETA,AdaptiveTransferSpeed
examples =[]
defexample(fn):
defwrapped():
try:
sys.stdout.write('Running: %s\n' %fn.__name__)
fn()
sys.stdout.write('\n')
exceptKeyboardInterrupt:
sys.stdout.write('\nSkipping example.\n\n')
examples.append(wrapped)
returnwrapped
@example
defwith_example0():
withProgressBar(max_value=10)as progress:
fori inrange(10):
# do something
time.sleep(0.001)
progress.update(i)
@example
def with_example1():
withProgressBar(max_value=10,redirect_stdout=True)as p:
fori inrange(10):
# do something
p.update(i)
time.sleep(0.001)
@example
defexample0():
pbar= ProgressBar(widgets=[Percentage(),Bar()],max_value=10).start()
fori inrange(10):
# do something
time.sleep(0.001)
pbar.update(i+ 1)
pbar.finish()
@example
defexample1():
widgets= ['Test: ',Percentage(),' ',Bar(marker=RotatingMarker()),
' ',ETA(),' ',FileTransferSpeed()]
pbar= ProgressBar(widgets=widgets,max_value=1000).start()
fori inrange(100):
# do something
pbar.update(10* i+ 1)
pbar.finish()
@example
def example2():
classCrazyFileTransferSpeed(FileTransferSpeed):
"It's bigger between 45 and 80 percent"
defupdate(self,pbar):
if45 <pbar.percentage()< 80:
return'Bigger Now ' + FileTransferSpeed.update(self,pbar)
else:
returnFileTransferSpeed.update(self,pbar)
widgets= [CrazyFileTransferSpeed(),' <<<',Bar(),'>>> ',
Percentage(),' ',ETA()]
pbar= ProgressBar(widgets=widgets,max_value=1000)
# maybe do something
pbar.start()
fori inrange(200):
# do something
pbar.update(5* i+ 1)
pbar.finish()
@example
defexample3():
widgets= [Bar('>'),' ',ETA(),' ',ReverseBar('<')]
pbar= ProgressBar(widgets=widgets,max_value=1000).start()
fori inrange(100):
# do something
pbar.update(10* i+ 1)
pbar.finish()
@example
defexample4():
widgets= ['Test: ',Percentage(),' ',
Bar(marker='0',left='[',right=']'),
' ',ETA(),' ',FileTransferSpeed()]
pbar= ProgressBar(widgets=widgets,max_value=500)
pbar.start()
fori inrange(100,500 +1,50):
time.sleep(0.001)
pbar.update(i)
pbar.finish()
@example
def example5():
pbar= ProgressBar(widgets=[SimpleProgress()],max_value=17).start()
fori inrange(17):
time.sleep(0.001)
pbar.update(i+ 1)
pbar.finish()
@example
defexample6():
pbar= ProgressBar().start()
fori inrange(10):
time.sleep(0.001)
pbar.update(i+ 1)
pbar.finish()
@example
def example7():
pbar= ProgressBar() # Progressbar can guess max_value automatically.
fori inpbar(range(8)):
time.sleep(0.001)
@example
defexample8():
pbar= ProgressBar(max_value=8) # Progressbar can't guess max_value.
fori inpbar((ifor iin range(8))):
time.sleep(0.001)
@example
def example9():
pbar= ProgressBar(widgets=['Working: ', AnimatedMarker()])
fori inpbar((ifor iin range(5))):
time.sleep(0.001)
@example
defexample10():
widgets= ['Processed: ',Counter(),' lines (',Timer(),')']
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(15))):
time.sleep(0.001)
@example
defexample11():
widgets= [FormatLabel('Processed: %(value)d lines (in: %(elapsed)s)')]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(15))):
time.sleep(0.001)
@example
defexample12():
widgets= ['Balloon: ',AnimatedMarker(markers='.oO@* ')]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(24))):
time.sleep(0.001)
@example
defexample13():
# You may need python 3.x to see this correctly
try:
widgets= ['Arrows: ',AnimatedMarker(markers='←↑→↓')]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(24))):
time.sleep(0.001)
exceptUnicodeError:
sys.stdout.write('Unicode error: skipping example')
@example
defexample14():
# You may need python 3.x to see this correctly
try:
widgets= ['Arrows: ',AnimatedMarker(markers='◢◣◤◥')]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(24))):
time.sleep(0.001)
exceptUnicodeError:
sys.stdout.write('Unicode error: skipping example')
@example
defexample15():
# You may need python 3.x to see this correctly
try:
widgets= ['Wheels: ',AnimatedMarker(markers='◐◓◑◒')]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(24))):
time.sleep(0.001)
exceptUnicodeError:
sys.stdout.write('Unicode error: skipping example')
@example
defexample16():
widgets= [FormatLabel('Bouncer: value %(value)d - '),BouncingBar()]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(100))):
time.sleep(0.001)
@example
defexample17():
widgets= [FormatLabel('Animated Bouncer: value %(value)d - '),
BouncingBar(marker=RotatingMarker())]
pbar= ProgressBar(widgets=widgets)
fori inpbar((ifor iin range(18))):
time.sleep(0.001)
@example
defwith_example18():
withProgressBar(max_value=10,term_width=20,left_justify=False)as \
progress:
assertprogress._env_size()is notNone
fori inrange(10):
progress.update(i)
@example
def with_example19():
withProgressBar(max_value=1)as progress:
try:
progress.update(2)
exceptValueError:
pass
@example
defwith_example20():
progress= ProgressBar(max_value=1)
try:
progress.update(1)
exceptRuntimeError:
pass
@example
def with_example21a():
withProgressBar(max_value=1,redirect_stdout=True)as progress:
print('',file=sys.stdout)
progress.update(0)
@example
defwith_example21b():
withProgressBar(max_value=1,redirect_stderr=True)as progress:
print('',file=sys.stderr)
progress.update(0)
@example
def with_example22():
try:
withProgressBar(max_value=-1)as progress:
progress.start()
exceptValueError:
pass
@example
defexample23():
widgets= [BouncingBar(marker=RotatingMarker())]
withProgressBar(widgets=widgets,max_value=20,term_width=10)as progress:
fori inrange(20):
progress.update(i)
widgets= [BouncingBar(marker=RotatingMarker(),fill_left=False)]
withProgressBar(widgets=widgets,max_value=20,term_width=10)as progress:
fori inrange(20):
progress.update(i)
@example
def example24():
pbar= ProgressBar(widgets=[Percentage(),Bar()],max_value=10).start()
fori inrange(10):
# do something
time.sleep(0.001)
pbar+= 1
pbar.finish()
@example
def example25():
widgets= ['Test: ',Percentage(),' ',Bar(marker=RotatingMarker()),
' ',ETA(),' ',FileTransferSpeed()]
pbar= ProgressBar(widgets=widgets,max_value=1000,
redirect_stdout=True).start()
fori inrange(100):
# do something
pbar+= 10
pbar.finish()
@example
def example26():
widgets= [
Percentage(),
' ',Bar(),
' ',ETA(),
' ',AdaptiveETA(),
' ',AdaptiveTransferSpeed(),
]
pbar= ProgressBar(widgets=widgets,max_value=500)
pbar.start()
fori inrange(500):
time.sleep(0.001+ (i< 100)* 0.0001+ (i> 400)* 0.009)
pbar.update(i+ 1)
pbar.finish()
@example
defexample27():
# Testing AdaptiveETA when the value doesn't actually change
pbar= ProgressBar(widgets=[AdaptiveETA(),AdaptiveTransferSpeed()],
max_value=2,poll=0.0001)
pbar.start()
pbar.update(1)
time.sleep(0.001)
pbar.update(1)
pbar.finish()
@example
defexample28():
# Testing using progressbar as an iterator with a max value
pbar= ProgressBar()
forn inpbar(iter(range(100)),100):
# iter range is a way to get an iterator in both python 2 and 3
pass
@example
defexample29():
widgets= ['Test: ',Percentage(),' | ',ETA(),' | ',AbsoluteETA()]
pbar= ProgressBar(widgets=widgets,maxval=500).start()
fori inrange(500):
time.sleep(0.01)
pbar.update(i+1)
pbar.finish()
def test():
forexample inexamples:
example()
if __name__== '__main__':
try:
test()
exceptKeyboardInterrupt:
sys.stdout('\nQuitting examples.\n')
- 参考文档:http://progressbar-2.readthedocs.io/en/latest/
- 还有一个和progressbar类似的模块:tqdm
阅读全文
1 0
- Python进度条-progressbar2模块
- python tqdm进度条模块
- python进度条
- python 模块-sys
- Python 模块-OS
- Python使用一个进度条
- Python简单进度条示例
- python 进度条 tqdm
- Python进度条(tqdm)
- tqdm【Python进度条】
- Python 进度条 tqdm
- python进度条 tqdm 解析
- Python实现进度条总结
- python实现进度条
- Python指南--模块
- Python指南--模块
- Python 模块-操作Redis
- python模块 ---logging模块
- Longest Run on a Snowboard (记忆化搜索)
- 1、DDL(data definition Lanaguage)数据库模式定义语言
- Java多线程(四)之ConcurrentSkipListMap深入分析
- iconfont字体图标渐变
- Kafka 单机和分布式环境搭建与案例使用
- Python进度条-progressbar2模块
- Android Tombstone 分析
- java中ArrayList初始容量和容量分配
- 位图与布隆及大数据处理题型分析
- nyoj 252 01串
- 解决升级到Android Studio 3 Beta版本编译异常
- 【dubbo】宏观总结
- Java中静态代码块的用法
- 后台读取properties配置文件