Skip to main content

A simple ANSI-based progress bar

Project description

progress1bar

build codecov Code Grade complexity vulnerabilities PyPI version python

A simple ANSI-based progress bar.

Installation

pip install progress1bar

ProgressBar

The ProgressBar class is used to display function execution as a progress bar. Use it as a context manager, and simply set the .total and .count attributes accordingly. Here is an example:

import names, random, time
from progress1bar import ProgressBar

with ProgressBar() as pb:
    pb.alias = names.get_full_name()
    pb.total = random.randint(50, 100)
    for _ in range(pb.total):
        # simulate work
        pb.count += 1
        time.sleep(.09)

Executing the code above (example1) results in the following: example

Examples

Various examples are included to demonstrate the progress1bar package. To run the examples, build the Docker image and run the Docker container using the instructions described in the Development section.

Configure ProgressBar to display the item that is currently being processd by setting the alias attribute, specify fill dictionary parameter to ensure the progress bar digits are displayed uniformly:

import names
from progress1bar import ProgressBar

print('Processing names...')
completed_message = 'Done processing all names'
fill = {'max_index': 9, 'max_total': 999}
with ProgressBar(index=1, total=500, completed_message=completed_message, fill=fill, clear_alias=True) as pb:
    for _ in range(pb.total):
        pb.alias = names.get_full_name()
        # simulate work
        pb.count += 1

Executing the code above (example2) results in the following: example

Configure ProgressBar to use regular expressions to determine the total, count and alias attributes:

import names, random, logging
from progress1bar import ProgressBar

logger = logging.getLogger(__name__)

TOTAL_ITEMS = 325

def process_message(pb, message):
    pb.match(message)
    logger.debug(message)

regex = {
    'total': r'^processing total of (?P<value>\d+)$',
    'count': r'^processed .*$',
    'alias': r'^processor is (?P<value>.*)$'
}
fill = {
    'max_total': TOTAL_ITEMS
}
with ProgressBar(regex=regex, fill=fill) as pb:
    last_name = names.get_last_name()
    process_message(pb, f'processor is {last_name}')
    total = random.randint(50, TOTAL_ITEMS)
    process_message(pb, f'processing total of {total}')
    for _ in range(total):
        process_message(pb, f'processed {names.get_full_name()}')

Executing the code above (example3) results in the following: example

Configure ProgressBar to show and reuse progress for several iterations:

import names, random
from progress1bar import ProgressBar

TOTAL_ITEMS = 325
TOTAL_NAMES = 5

fill = {
    'max_total': TOTAL_ITEMS,
    'max_completed': TOTAL_NAMES
}
with ProgressBar(fill=fill) as pb:
    total_names = 0
    while True:
        pb.alias = names.get_last_name()
        pb.total = random.randint(50, TOTAL_ITEMS)
        for _ in range(pb.total):
            names.get_full_name()
            pb.count += 1
        total_names += 1  
        if total_names == TOTAL_NAMES:
            pb.alias = ''
            break
        pb.reset()

Executing the code above (example4) results in the following: example

Development

Clone the repository and ensure the latest version of Docker is installed on your development server.

Build the Docker image:

docker image build \
-t \
progress1bar:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-v $PWD:/code \
progress1bar:latest \
/bin/bash

Execute the build:

pyb -X

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

progress1bar-0.2.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

progress1bar-0.2.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file progress1bar-0.2.2.tar.gz.

File metadata

  • Download URL: progress1bar-0.2.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for progress1bar-0.2.2.tar.gz
Algorithm Hash digest
SHA256 4d78014a16b514a3d13076ab4dcb560dd7b0fbf1dbe4374f0edde39d9479e5bf
MD5 5a99f2b6ef7dcb69d54245dac996a172
BLAKE2b-256 237c6ba1d4a757619be46223d4d698673557f0e058d9f790d7c64206510fee4d

See more details on using hashes here.

File details

Details for the file progress1bar-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for progress1bar-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99113efa8dc8eb538317af0441952f7c3429b6750b5a3a29a7c8eb15718006fe
MD5 11a7cf10262a66e6c762db1775847635
BLAKE2b-256 03113af5c50d1846bfa32114a57b25530e8f9f4f10edacea30e78dc609582823

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page