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

Use the ProgressBar class to display your function's 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):
        pb.count += 1
        # simulate work
        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 alias attribute:

import time, names
from progress1bar import ProgressBar

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

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

Configure ProgressBar to use regular expressions to determine the total, cound and alias from logged messages:

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 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:/progress1bar \
progress1bar:latest \
/bin/sh

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.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

progress1bar-0.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: progress1bar-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for progress1bar-0.1.1.tar.gz
Algorithm Hash digest
SHA256 253e2cfe2a4cfb839d283fa06f6d10df0ca444df2248502b6a1a5748e261d32a
MD5 03004c004bf3be6eb83a0345945702e7
BLAKE2b-256 f66f552591cd40050776c533f5ecd5124e4c3ccbb8d2cf0ca289a8b09dc3e0dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: progress1bar-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for progress1bar-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e66a1dea4f08e690afb201716d1fb427fb533c71097eb467e967339547c8339f
MD5 30dcf5b6aafbcc58a2130027a985d4eb
BLAKE2b-256 fc0f7425ebdec96acfe6f9a528778ad57bbc63056b4e3404c0d48935112d1015

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