- Notifications
You must be signed in to change notification settings - Fork102
Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"
License
wolph/python-progressbar
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Build status:
Coverage:
The package can be installed through pip (this is the recommended method):
pip install progressbar2
Or if pip is not available, easy_install should work as well:
easy_install progressbar2
Or 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.gz
A text progress bar is typically used to display the progress of a longrunning 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 lineis given by a number of widgets. A widget is an object that may displaydifferently depending on the state of the progress bar. There are many typesof widgets:
The progressbar module is very easy to use, yet very powerful. It will alsoautomatically enable features like auto-resizing when the system supports it.
- The Jetbrains (PyCharm, etc) editors work out of the box, but for more advanced features such as the MultiBar support 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/issue23220
- Jupyter notebooks buffer sys.stdout which can cause mixed output. This issue can be resolved easily using: import sys; sys.stdout.flush(). Linked issue:#173
- Documentation
- Package homepage
- My blog
There are many ways to use Python Progressbar, you can see a few basic exampleshere but there are many more in the examples file.
importtimeimportprogressbarforiinprogressbar.progressbar(range(100)):time.sleep(0.02)
Progressbars with logging require stderr redirection _before_ theStreamHandler is initialized. To make sure the stderr stream has beenredirected on time make sure to call progressbar.streams.wrap_stderr() beforeyou initialize the logger.
One option to force early initialization is by using the WRAP_STDERRenvironment variable, on Linux/Unix systems this can be done through:
# WRAP_STDERR=true python your_script.py
If 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 theStreamHandler after the wrapping has taken place.
importtimeimportloggingimportprogressbarprogressbar.streams.wrap_stderr()logging.basicConfig()foriinprogressbar.progressbar(range(10)):logging.error('Got %d',i)time.sleep(0.2)
importrandomimportthreadingimporttimeimportprogressbarBARS=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()
importtimeimportprogressbarwithprogressbar.ProgressBar(max_value=10)asbar:foriinrange(10):time.sleep(0.1)bar.update(i)
importtimeimportprogressbarforiinprogressbar.progressbar(range(100),redirect_stdout=True):print('Some text',i)time.sleep(0.1)
importtimeimportprogressbarbar=progressbar.ProgressBar(max_value=progressbar.UnknownLength)foriinrange(20):time.sleep(0.1)bar.update(i)
importtimeimportprogressbarwidgets=[' [',progressbar.Timer(),'] ',progressbar.Bar(),' (',progressbar.ETA(),') ',]foriinprogressbar.progressbar(range(20),widgets=widgets):time.sleep(0.1)
# 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)
importrandomimportsysimporttimeimportprogressbarBARS=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()
About
Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"