Skip to main content

Enhance tqdm progress bars by assigning tags and colors to iterations.

Project description

tqdm-tag

tqdm-tag

Color-code individual items in a tqdm progress bar by tagging them with a status.

PyPI Python Tests Docs License: MIT

tqdm-tag extends tqdm so you can call pbar.warn() or pbar.error() on any item inside your loop. The corresponding segment of the progress bar fills with that tag's color, giving you an at-a-glance overview of where issues occurred — without waiting for the loop to finish.


demo

Top: plain tqdm (monochrome). Bottom: tqdm-tag coloring each processed item by outcome.


Installation

pip install tqdm-tag

Usage

TqdmErrorTag is a drop-in replacement for tqdm — swap the class name and you're done. Call .warn() or .error() on any item to color that segment of the bar:

from tqdm_tag import TqdmErrorTag

pbar = TqdmErrorTag(range(100), legend=True, desc="Processing")
for item in pbar:
    result = process(item)
    if result.has_warning: pbar.warn()
    if result.has_error:   pbar.error()

With legend=True, a live second line below the bar shows running counts:

$\texttt{Processing:\ \ 73}\%|\color[RGB]{166,227,161}{\texttt{████████████}}\color[RGB]{249,226,175}{\texttt{███}}\color[RGB]{166,227,161}{\texttt{███}}\color[RGB]{243,139,168}{\texttt{█}}\color[RGB]{170,170,170}{\texttt{████}}\color[RGB]{255,255,255}{\texttt{ |\ 73/100\ [00:03{<}00:01,\ 12.5it/s]}}$

$\color[RGB]{249,226,175}{\texttt{█\ warn:\ 4}}$   $\color[RGB]{243,139,168}{\texttt{█\ error:\ 1}}$

Custom tags with TqdmTag

For full control, use TqdmTag with Tag objects. Each Tag groups name, color, and an optional status integer (auto-assigned if omitted):

from tqdm_tag import Tag, TqdmTag

pbar = TqdmTag(
    range(100),
    tags=[Tag("warn", color="yellow"), Tag("error", color="red")],
    legend=True,
)
for i in pbar:
    if i == 10: pbar.tag("warn")    # reference by name string
    if i == 80: pbar.tag("error")

Add tags on the fly

Pass a Tag object directly to .tag() to register and apply it in one call. Subsequent calls can use the name string:

from tqdm_tag import Tag, TqdmTag

pbar = TqdmTag(range(100))
for i in pbar:
    if i == 10: pbar.tag(Tag("warn",  color="yellow"))  # registers + applies
    if i == 30: pbar.tag("warn")                        # reuse by name
    if i == 80: pbar.tag(Tag("error", color="red"))

Turn the whole bar green on success

import time
from tqdm_tag import TqdmTag

items = range(50)
pbar = TqdmTag(items, colour="red")
for i in pbar:
    time.sleep(0.05)
    if i == len(items) - 1:
        pbar.tag("default", color="green")

Customize the legend

Tags in the legend are ordered by status value. Pass a legend_format callable to take full control — it receives tag_counts (dict of name → count) and tag_to_color (dict of name → color):

def my_legend(counts, colors):
    return "  ".join(f"[{k.upper()}={v}]" for k, v in counts.items())

pbar = TqdmErrorTag(range(100), legend=True, legend_format=my_legend)

Reduce operation for dense bars

When the terminal is narrow, multiple items share one bar segment. Use reduce_op to control which status wins, and reduce_ignore_default to skip untagged items:

from tqdm_tag import Tag, TqdmTag

pbar = TqdmTag(
    range(100),
    tags=[Tag("warn", color="yellow", status=1), Tag("error", color="red", status=2)],
    reduce_op=max,              # highest-severity status wins
    reduce_ignore_default=True, # don't let untagged items dilute the color
)
for i in pbar:
    if i % 10 == 1: pbar.tag("warn")
    if i % 10 == 2: pbar.tag("error")

API

Class Description
TqdmErrorTag Drop-in replacement with pre-wired warn / error tags and .warn() / .error() helpers
TqdmTag Core class for fully custom tags
Tag Dataclass grouping a tag's name, color, and status
ColoredBar Internal Bar subclass that renders ANSI-colored segments

Full API reference: tqdm-tag.readthedocs.io

License

MIT © Colin Moldenhauer

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

tqdm_tag-1.0.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

tqdm_tag-1.0.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file tqdm_tag-1.0.0.tar.gz.

File metadata

  • Download URL: tqdm_tag-1.0.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tqdm_tag-1.0.0.tar.gz
Algorithm Hash digest
SHA256 405273f5a0dc084729164b764da89878f154ab6023aa88fb5f1e32cacc788a5f
MD5 ce5d202a82c7090cff0698a617f9be73
BLAKE2b-256 85ce765f88d33164d25ab879c3a7227f510032b040fff8a51abba1fa9e8e3cfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tqdm_tag-1.0.0.tar.gz:

Publisher: publish.yml on ColinMoldenhauer/tqdm-tag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tqdm_tag-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tqdm_tag-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tqdm_tag-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fae2976d0d25dd040653e07eefbf23496e271bfb29c72321ff1ab847ca4108c9
MD5 861271cbd38980e2d1f42ad65b1ee726
BLAKE2b-256 d3ba1b2f9bf67f646a27daf13272681b422627c3a799f9e45726f9ea1d0838c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tqdm_tag-1.0.0-py3-none-any.whl:

Publisher: publish.yml on ColinMoldenhauer/tqdm-tag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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