Skip to main content

Smartbar

Project description

SmartBar

PyPI GitHub Repo

SmartBar is a powerful, automatic, and extensible progress bar library for Python, built with smart I/O tracking and multi-threading support.
It provides real-time progress, ETA, speed tracking, and works seamlessly with file and network I/O using with blocks — no manual .update() needed.


Features

  • Thread-safe and supports concurrent usage
  • Supports multiple bars simultaneously
  • Automatically wraps open(), requests.get(), and aiohttp
  • Tracks both reading and writing operations
  • Smart speed and ETA calculation (combines recent + average)
  • Fully customizable output and bar style
  • Pause, resume, ignore, and manual update support
  • Works with with and async with contexts

Installation

pip install smartbar

Basic Example

from smartbar import SmartBar
import requests

with SmartBar("Downloading", length=40) as bar:
    r = requests.get("https://example.com/file", stream=True)
    for chunk in r.iter_content(1024):
        pass  # progress is auto-tracked

Custom Style & Live Animation

from smartbar import SmartBar
import requests
import time

BAR1 = "%(DESC). \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"
BAR2 = "%(DESC).. \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"
BAR3 = "%(DESC)... \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"

url = "https://download.samplelib.com/mp4/sample-20s.mp4"

with SmartBar("Downloading", custom_bar_output=BAR1) as bar:
    response = requests.get(url, stream=True)
    with open("video.mp4", "wb") as f:
        stime = time.time()
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:
                f.write(chunk)
            if time.time() - stime > 1:
                # Animate bar every second
                bar.custom_bar_output = BAR2 if bar.custom_bar_output == BAR1 else (
                    BAR3 if bar.custom_bar_output == BAR2 else BAR1
                )
                stime = time.time()

Manual Progress (Disable Auto Tracking)

import time
from smartbar import SmartBar

BAR1 = "%(DESC). {} SEC \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"
BAR2 = "%(DESC).. {} SEC \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"
BAR3 = "%(DESC)... {} SEC \033[48;5;214m[%(BAR)]\033[0m %(CUR)/%(TOTAL) (%(PERCENT)) | %(SPEED) | ETA: %(ETA)"

SEC = 10

with SmartBar("Waiting", auto_bar=False, mode="items") as bar:
    bar.total = SEC
    start = time.time()
    last = time.time()
    while True:
        now = time.time()
        if now - last > 1:
            elapsed = int(now - start)
            seconds_left = max(0, SEC - elapsed)

            if bar.custom_bar_output == BAR1:
                bar.custom_bar_output = BAR2.format(seconds_left)
            elif bar.custom_bar_output == BAR2:
                bar.custom_bar_output = BAR3.format(seconds_left)
            else:
                bar.custom_bar_output = BAR1.format(seconds_left)

            bar.add(now - last)
            last = now

        if bar.current >= bar.total:
            break

Async Support

import aiohttp
import asyncio
from smartbar import SmartBar

async def download():
    async with SmartBar("Async Download") as bar:
        async with aiohttp.ClientSession() as session:
            async with session.get("https://example.com") as resp:
                async for chunk in resp.content.iter_chunked(1024):
                    pass  # progress auto-tracked

asyncio.run(download())

Pause, Resume & Ignore

from smartbar import SmartBar
import requests

with SmartBar("Download") as bar:
    r = requests.get("https://example.com/file", stream=True)

    bar.pause()
    # Do something non-tracked...
    bar.resume()

    bar.ignore(r)  # disables tracking for this object

Format Variables

You can customize the output bar using these placeholders:

Placeholder Meaning
%(DESC) Bar description
%(BAR) The visual progress bar
%(CUR) Current value
%(TOTAL) Total expected value
%(PERCENT) Percentage completed
%(SPEED) Transfer speed
%(ETA) Estimated time remaining

Example:
"%(BAR)"[#######.......]

bar_style

with SmartBar("Waiting", auto_bar=False, mode="items", bar_style="simple", style=r"——") as bar: 
    pass

custom_bar_style

example_style = {  # [Foreground, Background]
    "BAR": {
        "+": [0xFFFFFF, 0x000000],
        "-": [0x888888, None],
    },
    "DESC": [0xAAAAAA, None],
    "CUR": [0xAAAAAA, None],
    "TOTAL": [0xAAAAAA, None],
    "PERCENT": [0xAAAAAA, None],
    "SPEED": [0xAAAAAA, None],
    "ETA": [0xAAAAAA, None],
}

with SmartBar("Waiting", auto_bar=False, mode="items", custom_bar_style=example_style, style=r"——") as bar:
    pass

License

MIT


Author

lama2923
GitHub: https://github.com/lama2923


Iterable Mode (for usage)

SmartBar can also be used directly inside a for loop for iterating over an iterable, such as a list, generator, or file.

from smartbar import SmartBar
import time

data = range(100)

for item in SmartBar(data, desc="Processing", length=30):
    time.sleep(0.05)  # simulate work

This usage automatically tracks iteration progress and provides ETA and speed based on iteration rate.


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

smartbar-1.2.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smartbar-1.2.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file smartbar-1.2.1.tar.gz.

File metadata

  • Download URL: smartbar-1.2.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for smartbar-1.2.1.tar.gz
Algorithm Hash digest
SHA256 56b3c66dbc3bea18bf236f92fd3645e3f40bdc8d1e30e3146eef827beed250e5
MD5 1c1ab43288feaa6c185ac7584ecee2f1
BLAKE2b-256 bec1efcf6f6d5f76a99243dacffdc5e1abe8365ea164f02d09fe4a64a3969b57

See more details on using hashes here.

File details

Details for the file smartbar-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: smartbar-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for smartbar-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ca350570f848dac5a1e35ea91427cc5a5ef4b615b260cb3c6e31a943e34f701a
MD5 4e6c2625134739d93c1adde073a0c382
BLAKE2b-256 1bfc1c4a88e5a8761ed4fa1660decc2c363dbe0235daef1a5b6f104f6213d616

See more details on using hashes here.

Supported by

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