Package: progressbar2
Build status:Coverage:InstallThe package can be installed throughpip(this is the recommended method):pip install progressbar2Or ifpipis not available,easy_installshould work as well:easy_install progressbar2Or download the latest release from Pypi (https://pypi.python.org/pypi/progressbar2) or Github.Note that the releases on Pypi are signed with my GPG key (https://pgp.mit.edu/pks/lookup?op=vindex&search=0xE81444E9CE1F695D) and can be checked using GPG:gpg –verify progressbar2-<version>.tar.gz.asc progressbar2-<version>.tar.gzIntroductionA text progress bar is typically used to display the progress of a long
running operation, providing a visual cue that processing is underway.The progressbar is based on the old Python progressbar package that was published on the now defunct Google Code. Since that project was completely abandoned by its developer and the developer did not respond to email, I decided to fork the package. This package is still backwards compatible with the original progressbar package so you can safely use it as a drop-in replacement for existing project.The ProgressBar class manages the current progress, and the format of the line
is given by a number of widgets. A widget is an object that may display
differently depending on the state of the progress bar. There are many types
of widgets:AbsoluteETAAdaptiveETAAdaptiveTransferSpeedAnimatedMarkerBarBouncingBarCounterCurrentTimeDataSizeDynamicMessageETAFileTransferSpeedFormatCustomTextFormatLabelFormatLabelBarGranularBarPercentagePercentageLabelBarReverseBarRotatingMarkerSimpleProgressTimerThe progressbar module is very easy to use, yet very powerful. It will also
automatically enable features like auto-resizing when the system supports it.Security contact informationTo report a security vulnerability, please use theTidelift security contact.
Tidelift will coordinate the fix and disclosure.Known issuesThe Jetbrains (PyCharm, etc) editors work out of the box, but for more advanced features such as theMultiBarsupport you will need to enable the “Enable terminal in output console” checkbox in the Run dialog.The IDLE editor doesn’t support these types of progress bars at all:https://bugs.python.org/issue23220Jupyter notebooks buffersys.stdoutwhich can cause mixed output. This issue can be resolved easily using:import sys; sys.stdout.flush(). Linked issue:https://github.com/WoLpH/python-progressbar/issues/173LinksDocumentationhttps://progressbar-2.readthedocs.org/en/latest/Sourcehttps://github.com/WoLpH/python-progressbarBug reportshttps://github.com/WoLpH/python-progressbar/issuesPackage homepagehttps://pypi.python.org/pypi/progressbar2My bloghttps://w.wol.ph/UsageThere are many ways to use Python Progressbar, you can see a few basic examples
here but there are many more in the examples file.Wrapping an iterableimporttimeimportprogressbarforiinprogressbar.progressbar(range(100)):time.sleep(0.02)Progressbars with loggingProgressbars with logging requirestderrredirection _before_ theStreamHandleris initialized. To make sure thestderrstream has been
redirected on time make sure to callprogressbar.streams.wrap_stderr()before
you initialize thelogger.One option to force early initialization is by using theWRAP_STDERRenvironment variable, on Linux/Unix systems this can be done through:# WRAP_STDERR=true python your_script.pyIf you need to flush manually while wrapping, you can do so using:importprogressbarprogressbar.streams.flush()In most cases the following will work as well, as long as you initialize theStreamHandlerafter the wrapping has taken place.importtimeimportloggingimportprogressbarprogressbar.streams.wrap_stderr()logging.basicConfig()foriinprogressbar.progressbar(range(10)):logging.error('Got%d',i)time.sleep(0.2)Multiple (threaded) progressbarsimportrandomimportthreadingimporttimeimportprogressbarBARS=5N=50defdo_something(bar):foriinbar(range(N)):# Sleep up to 0.1 secondstime.sleep(random.random()*0.1)# print messages at random intervals to show how extra output worksifrandom.random()>0.9:bar.print('random message for bar',bar,i)withprogressbar.MultiBar()asmultibar:foriinrange(BARS):# Get a progressbarbar=multibar[f'Thread label here{i}']# Create a thread and pass the progressbarthreading.Thread(target=do_something,args=(bar,)).start()Context wrapperimporttimeimportprogressbarwithprogressbar.ProgressBar(max_value=10)asbar:foriinrange(10):time.sleep(0.1)bar.update(i)Combining progressbars with print outputimporttimeimportprogressbarforiinprogressbar.progressbar(range(100),redirect_stdout=True):print('Some text',i)time.sleep(0.1)Progressbar with unknown lengthimporttimeimportprogressbarbar=progressbar.ProgressBar(max_value=progressbar.UnknownLength)foriinrange(20):time.sleep(0.1)bar.update(i)Bar with custom widgetsimporttimeimportprogressbarwidgets=[' [',progressbar.Timer(),'] ',progressbar.Bar(),' (',progressbar.ETA(),') ',]foriinprogressbar.progressbar(range(20),widgets=widgets):time.sleep(0.1)Bar with wide Chinese (or other multibyte) characters# vim: fileencoding=utf-8importtimeimportprogressbardefcustom_len(value):# These characters take up more spacecharacters={'进':2,'度':2,}total=0forcinvalue:total+=characters.get(c,1)returntotalbar=progressbar.ProgressBar(widgets=['进度: ',progressbar.Bar(),' ',progressbar.Counter(format='%(value)02d/%(max_value)d'),],len_func=custom_len,)foriinbar(range(10)):time.sleep(0.1)Showing multiple independent progress bars in parallelimportrandomimportsysimporttimeimportprogressbarBARS=5N=100# Construct the list of progress bars with the `line_offset` so they draw# below each otherbars=[]foriinrange(BARS):bars.append(progressbar.ProgressBar(max_value=N,# We add 1 to the line offset to account for the `print_fd`line_offset=i+1,max_error=False,))# Create a file descriptor for regular printing as wellprint_fd=progressbar.LineOffsetStreamWrapper(lines=0,stream=sys.stdout)# The progress bar updates, normally you would do something useful hereforiinrange(N*BARS):time.sleep(0.005)# Increment one of the progress bars at randombars[random.randrange(0,BARS)].increment()# Print a status message to the `print_fd` below the progress barsprint(f'Hi, we are at update{i+1}of{N*BARS}',file=print_fd)# Cleanup the barsforbarinbars:bar.finish()# Add a newline to make sure the next print starts on a new lineprint()Naturally we can do this from separate threads as well:importrandomimportthreadingimporttimeimportprogressbarBARS=5N=100# Create the bars with the given line offsetbars=[]forline_offsetinrange(BARS):bars.append(progressbar.ProgressBar(line_offset=line_offset,max_value=N))classWorker(threading.Thread):def__init__(self,bar):super().__init__()self.bar=bardefrun(self):foriinrange(N):time.sleep(random.random()/25)self.bar.update(i)forbarinbars:Worker(bar).start()print()