Python USES the progressbar module to display the progress bar

  • 2020-11-03 22:30:39
  • OfStack

This article illustrates Python's use of progressbar module to display progress bar function. To share for your reference, the details are as follows:

progressbar installation:


pip install progressbar

Use 1


# -*- coding=utf-8 -*-
import time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
progress = ProgressBar()
for i in progress(range(1000)):
  dosomework()

Display effect:

[

5% |### |
100% |#########################################################################|

]

Use 2


# -*- coding=utf-8 -*-
from __future__ import division
import sys, time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
pbar = ProgressBar().start()
for i in range(1000):
  pbar.update(int((i / (total - 1)) * 100))
  dosomework()
pbar.finish()

Display effect:

[

39% |############################## |
100% |#############################################################################|

]

Use 3


# -*- coding=utf-8 -*-
import time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
widgets = ['Progress: ',Percentage(), ' ', Bar('#'),' ', Timer(),
      ' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=10*total).start()
for i in range(total):
  # do something
  pbar.update(10 * i + 1)
  dosomework()
pbar.finish()

Display effect:

[

Progress: 3% |### | Elapsed Time: 0:00:15 ETA: 0:09:02 919.67 B/s
Progress: 100% |###################################################################################| Elapsed Time: 0:10:10 Time: 0:10:10 917.42 B/s

]

widgets Optional parameters Meaning:

'Progress: ': Sets the text displayed in front of the progress bar
Percentage() : Displays the percentage
Bar('#') : Sets the progress bar shape
ETA() : Displays estimated remaining time
Timer() : Displays the elapsed time

More about Python related content interested readers to view this site project: "Python mathematical operation skills summary", "Python string skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using skills summary", "introduction to Python and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful for Python programming.


Related articles: