Examples¶
Basic¶
For a basic status bar, invoke the Counter
class directly.
import time
import enlighten
pbar = enlighten.Counter(total=100, desc='Basic', unit='ticks')
for num in range(100):
time.sleep(0.1) # Simulate work
pbar.update()
Advanced¶
To maintain multiple progress bars simultaneously or write to the console, a manager is required.
Advanced output will only work when the output stream, sys.stdout
by default,
is attached to a TTY. get_manager()
can be used to get a manager instance.
It will return a disabled Manager
instance if the stream is not attached to a TTY
and an enabled instance if it is.
import time
import enlighten
manager = enlighten.get_manager()
ticks = manager.counter(total=100, desc='Ticks', unit='ticks')
tocks = manager.counter(total=20, desc='Tocks', unit='tocks')
for num in range(100):
time.sleep(0.1) # Simulate work
print(num)
ticks.update()
if not num % 5:
tocks.update()
manager.stop()
Counters¶
The Counter
class has two output formats, progress bar and counter.
The progress bar format is used when a total is not None
and the count is less than the
total. If neither of these conditions are met, the counter format is used:
import time
import enlighten
counter = enlighten.Counter(desc='Basic', unit='ticks')
for num in range(100):
time.sleep(0.1) # Simulate work
counter.update()
Color¶
The bar component of a progress bar can be colored by setting the color
keyword argument.
See Series Color for more information about valid colors.
import time
import enlighten
counter = enlighten.Counter(total=100, desc='Colorized', unit='ticks', color='red')
for num in range(100):
time.sleep(0.1) # Simulate work
counter.update()
Multicolored¶
The bar component of a progress bar can be multicolored to track multiple categories in a single progress bar.
The colors are drawn from right to left in the order they were added.
By default, when multicolored progress bars are used, additional fields are available for
bar_format
:
When add_subcounter()
is called with all_fields
set to True
,
the subcounter will have the additional fields:
More information about bar_format
can be found in the
Format section of the API.
One use case for multicolored progress bars is recording the status of a series of tests. In this example, Failures are red, errors are white, and successes are green. The count of each is listed in the progress bar.
import random
import time
import enlighten
bar_format = u'{desc}{desc_pad}{percentage:3.0f}%|{bar}| ' + \
u'S:{count_0:{len_total}d} ' + \
u'F:{count_2:{len_total}d} ' + \
u'E:{count_1:{len_total}d} ' + \
u'[{elapsed}<{eta}, {rate:.2f}{unit_pad}{unit}/s]'
success = enlighten.Counter(total=100, desc='Testing', unit='tests',
color='green', bar_format=bar_format)
errors = success.add_subcounter('white')
failures = success.add_subcounter('red')
while success.count < 100:
time.sleep(random.uniform(0.1, 0.3)) # Random processing time
result = random.randint(0, 10)
if result == 7:
errors.update()
if result in (5, 6):
failures.update()
else:
success.update()
A more complicated example is recording process start-up. In this case, all items will start red, transition to yellow, and eventually all will be green. The count, percentage, rate, and eta fields are all derived from the second subcounter added.
import random
import time
import enlighten
services = 100
bar_format = u'{desc}{desc_pad}{percentage_2:3.0f}%|{bar}|' + \
u' {count_2:{len_total}d}/{total:d} ' + \
u'[{elapsed}<{eta_2}, {rate_2:.2f}{unit_pad}{unit}/s]'
initializing = enlighten.Counter(total=services, desc='Starting', unit='services',
color='red', bar_format=bar_format)
starting = initializing.add_subcounter('yellow')
started = initializing.add_subcounter('green', all_fields=True)
while started.count < services:
remaining = services - initializing.count
if remaining:
num = random.randint(0, min(4, remaining))
initializing.update(num)
ready = initializing.count - initializing.subcount
if ready:
num = random.randint(0, min(3, ready))
starting.update_from(initializing, num)
if starting.count:
num = random.randint(0, min(2, starting.count))
started.update_from(starting, num)
time.sleep(random.uniform(0.1, 0.5)) # Random processing time
Additional Examples¶
basic
- Basic progress barcontext manager
- Managers and counters as context managersfloats
- Support totals and counts that arefloats
multicolored
- Multicolored progress barsmultiple with logging
- Nested progress bars and loggingFTP downloader
- Show progress downloading files from FTP