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
import
time
from
progressbar
import
AnimatedMarker
,
Bar
,
BouncingBar
,
Counter
,
ETA
,
\
FileTransferSpeed
,
FormatLabel
,
Percentage
,
\
ProgressBar
,
ReverseBar
,
RotatingMarker
,
\
SimpleProgress
,
Timer
,
AdaptiveETA
,
AbsoluteETA
,
AdaptiveTransferSpeed
examples
=
[
]
def
example
(
fn
)
:
def
wrapped
(
)
:
try
:
sys
.
stdout
.
write
(
'Running: %s\n'
%
fn
.
__name__
)
fn
(
)
sys
.
stdout
.
write
(
'\n'
)
except
KeyboardInterrupt
:
sys
.
stdout
.
write
(
'\nSkipping example.\n\n'
)
examples
.
append
(
wrapped
)
return
wrapped
@
example
def
with_example0
(
)
:
with
ProgressBar
(
max_value
=
10
)
as
progress
:
for
i
in
range
(
10
)
:
# do something
time
.
sleep
(
0.001
)
progress
.
update
(
i
)
@
example
def
with_example1
(
)
:
with
ProgressBar
(
max_value
=
10
,
redirect_stdout
=
True
)
as
p
:
for
i
in
range
(
10
)
:
# do something
p
.
update
(
i
)
time
.
sleep
(
0.001
)
@
example
def
example0
(
)
:
pbar
=
ProgressBar
(
widgets
=
[
Percentage
(
)
,
Bar
(
)
]
,
max_value
=
10
)
.
start
(
)
for
i
in
range
(
10
)
:
# do something
time
.
sleep
(
0.001
)
pbar
.
update
(
i
+
1
)
pbar
.
finish
(
)
@
example
def
example1
(
)
:
widgets
=
[
'Test: '
,
Percentage
(
)
,
' '
,
Bar
(
marker
=
RotatingMarker
(
)
)
,
' '
,
ETA
(
)
,
' '
,
FileTransferSpeed
(
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
,
max_value
=
1000
)
.
start
(
)
for
i
in
range
(
100
)
:
# do something
pbar
.
update
(
10
*
i
+
1
)
pbar
.
finish
(
)
@
example
def
example2
(
)
:
class
CrazyFileTransferSpeed
(
FileTransferSpeed
)
:
"It's bigger between 45 and 80 percent"
def
update
(
self
,
pbar
)
:
if
45
<
pbar
.
percentage
(
)
<
80
:
return
'Bigger Now '
+
FileTransferSpeed
.
update
(
self
,
pbar
)
else
:
return
FileTransferSpeed
.
update
(
self
,
pbar
)
widgets
=
[
CrazyFileTransferSpeed
(
)
,
' <<<'
,
Bar
(
)
,
'>>> '
,
Percentage
(
)
,
' '
,
ETA
(
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
,
max_value
=
1000
)
# maybe do something
pbar
.
start
(
)
for
i
in
range
(
200
)
:
# do something
pbar
.
update
(
5
*
i
+
1
)
pbar
.
finish
(
)
@
example
def
example3
(
)
:
widgets
=
[
Bar
(
'>'
)
,
' '
,
ETA
(
)
,
' '
,
ReverseBar
(
'<'
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
,
max_value
=
1000
)
.
start
(
)
for
i
in
range
(
100
)
:
# do something
pbar
.
update
(
10
*
i
+
1
)
pbar
.
finish
(
)
@
example
def
example4
(
)
:
widgets
=
[
'Test: '
,
Percentage
(
)
,
' '
,
Bar
(
marker
=
'0'
,
left
=
'['
,
right
=
']'
)
,
' '
,
ETA
(
)
,
' '
,
FileTransferSpeed
(
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
,
max_value
=
500
)
pbar
.
start
(
)
for
i
in
range
(
100
,
500
+
1
,
50
)
:
time
.
sleep
(
0.001
)
pbar
.
update
(
i
)
pbar
.
finish
(
)
@
example
def
example5
(
)
:
pbar
=
ProgressBar
(
widgets
=
[
SimpleProgress
(
)
]
,
max_value
=
17
)
.
start
(
)
for
i
in
range
(
17
)
:
time
.
sleep
(
0.001
)
pbar
.
update
(
i
+
1
)
pbar
.
finish
(
)
@
example
def
example6
(
)
:
pbar
=
ProgressBar
(
)
.
start
(
)
for
i
in
range
(
10
)
:
time
.
sleep
(
0.001
)
pbar
.
update
(
i
+
1
)
pbar
.
finish
(
)
@
example
def
example7
(
)
:
pbar
=
ProgressBar
(
)
# Progressbar can guess max_value automatically.
for
i
in
pbar
(
range
(
8
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example8
(
)
:
pbar
=
ProgressBar
(
max_value
=
8
)
# Progressbar can't guess max_value.
for
i
in
pbar
(
(
i
for
i
in
range
(
8
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example9
(
)
:
pbar
=
ProgressBar
(
widgets
=
[
'Working: '
,
AnimatedMarker
(
)
]
)
for
i
in
pbar
(
(
i
for
i
in
range
(
5
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example10
(
)
:
widgets
=
[
'Processed: '
,
Counter
(
)
,
' lines ('
,
Timer
(
)
,
')'
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
15
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example11
(
)
:
widgets
=
[
FormatLabel
(
'Processed: %(value)d lines (in: %(elapsed)s)'
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
15
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example12
(
)
:
widgets
=
[
'Balloon: '
,
AnimatedMarker
(
markers
=
'.oO@* '
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
24
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example13
(
)
:
# You may need python 3.x to see this correctly
try
:
widgets
=
[
'Arrows: '
,
AnimatedMarker
(
markers
=
'←
![]() ![]() ![]() ![]()
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
24
)
)
)
:
time
.
sleep
(
0.001
)
except
UnicodeError
:
sys
.
stdout
.
write
(
'Unicode error: skipping example'
)
@
example
def
example14
(
)
:
# You may need python 3.x to see this correctly
try
:
widgets
=
[
'Arrows: '
,
AnimatedMarker
(
markers
=
'◢◣◤◥'
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
24
)
)
)
:
time
.
sleep
(
0.001
)
except
UnicodeError
:
sys
.
stdout
.
write
(
'Unicode error: skipping example'
)
@
example
def
example15
(
)
:
# You may need python 3.x to see this correctly
try
:
widgets
=
[
'Wheels: '
,
AnimatedMarker
(
markers
=
'◐◓◑◒'
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
24
)
)
)
:
time
.
sleep
(
0.001
)
except
UnicodeError
:
sys
.
stdout
.
write
(
'Unicode error: skipping example'
)
@
example
def
example16
(
)
:
widgets
=
[
FormatLabel
(
'Bouncer: value %(value)d - '
)
,
BouncingBar
(
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
100
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
example17
(
)
:
widgets
=
[
FormatLabel
(
'Animated Bouncer: value %(value)d - '
)
,
BouncingBar
(
marker
=
RotatingMarker
(
)
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
)
for
i
in
pbar
(
(
i
for
i
in
range
(
18
)
)
)
:
time
.
sleep
(
0.001
)
@
example
def
with_example18
(
)
:
with
ProgressBar
(
max_value
=
10
,
term_width
=
20
,
left_justify
=
False
)
as
\
progress
:
assert
progress
.
_env_size
(
)
is
not
None
for
i
in
range
(
10
)
:
progress
.
update
(
i
)
@
example
def
with_example19
(
)
:
with
ProgressBar
(
max_value
=
1
)
as
progress
:
try
:
progress
.
update
(
2
)
except
ValueError
:
pass
@
example
def
with_example20
(
)
:
progress
=
ProgressBar
(
max_value
=
1
)
try
:
progress
.
update
(
1
)
except
RuntimeError
:
pass
@
example
def
with_example21a
(
)
:
with
ProgressBar
(
max_value
=
1
,
redirect_stdout
=
True
)
as
progress
:
print
(
''
,
file
=
sys
.
stdout
)
progress
.
update
(
0
)
@
example
def
with_example21b
(
)
:
with
ProgressBar
(
max_value
=
1
,
redirect_stderr
=
True
)
as
progress
:
print
(
''
,
file
=
sys
.
stderr
)
progress
.
update
(
0
)
@
example
def
with_example22
(
)
:
try
:
with
ProgressBar
(
max_value
=
-
1
)
as
progress
:
progress
.
start
(
)
except
ValueError
:
pass
@
example
def
example23
(
)
:
widgets
=
[
BouncingBar
(
marker
=
RotatingMarker
(
)
)
]
with
ProgressBar
(
widgets
=
widgets
,
max_value
=
20
,
term_width
=
10
)
as
progress
:
for
i
in
range
(
20
)
:
progress
.
update
(
i
)
widgets
=
[
BouncingBar
(
marker
=
RotatingMarker
(
)
,
fill_left
=
False
)
]
with
ProgressBar
(
widgets
=
widgets
,
max_value
=
20
,
term_width
=
10
)
as
progress
:
for
i
in
range
(
20
)
:
progress
.
update
(
i
)
@
example
def
example24
(
)
:
pbar
=
ProgressBar
(
widgets
=
[
Percentage
(
)
,
Bar
(
)
]
,
max_value
=
10
)
.
start
(
)
for
i
in
range
(
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
(
)
for
i
in
range
(
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
(
)
for
i
in
range
(
500
)
:
time
.
sleep
(
0.001
+
(
i
<
100
)
*
0.0001
+
(
i
>
400
)
*
0.009
)
pbar
.
update
(
i
+
1
)
pbar
.
finish
(
)
@
example
def
example27
(
)
:
# 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
def
example28
(
)
:
# Testing using progressbar as an iterator with a max value
pbar
=
ProgressBar
(
)
for
n
in
pbar
(
iter
(
range
(
100
)
)
,
100
)
:
# iter range is a way to get an iterator in both python 2 and 3
pass
@
example
def
example29
(
)
:
widgets
=
[
'Test: '
,
Percentage
(
)
,
' | '
,
ETA
(
)
,
' | '
,
AbsoluteETA
(
)
]
pbar
=
ProgressBar
(
widgets
=
widgets
,
maxval
=
500
)
.
start
(
)
for
i
in
range
(
500
)
:
time
.
sleep
(
0.01
)
pbar
.
update
(
i
+
1
)
pbar
.
finish
(
)
def
test
(
)
:
for
example
in
examples
:
example
(
)
if
__name__
==
'__main__'
:
try
:
test
(
)
except
KeyboardInterrupt
:
sys
.
stdout
(
'\nQuitting examples.\n'
)
|
- 参考文档:http://progressbar-2.readthedocs.io/en/latest/
- 还有一个和progressbar类似的模块:tqdm