API Reference
Tag
A named status tag with an optional color and status value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tag identifier, also used as display label in the legend. |
required |
color
|
str
|
Color for bar segments and legend swatch. Accepts named colors
( |
None
|
status
|
int
|
Integer used to rank tags when multiple items share one bar segment.
Higher values take priority with |
None
|
TqdmTag
Bases: tqdm
format_dict
property
Public API for read-only member access.
__init__(iterable=None, total=None, colour=None, default_status=0, default_tag_name='default', tags=None, initial_status=None, reduce_op=max, reduce_ignore_default=False, legend=False, legend_format=None, legend_show_default=False, prune_legend=False, **kwargs)
A tqdm progress bar whose segments are colored by per-item tags.
Each iteration starts at default_status; calling tag() during the
loop body records a different status for the current item. When the
bar is rendered, each displayed segment's color is derived from the
statuses of the items it represents (via reduce_op), using the
color registered for the corresponding tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
iterable
|
Iterable to decorate, same as in |
None
|
total
|
int
|
Expected number of iterations, same as in |
None
|
colour
|
str
|
Default bar colour, used for items still at |
None
|
default_status
|
int
|
Status value assigned to every item before |
0
|
default_tag_name
|
str
|
Name of the implicit tag representing |
'default'
|
tags
|
list of Tag
|
Tags to pre-register, in addition to the implicit
|
None
|
initial_status
|
dict, array-like, or None
|
Restores per-item status for a resumed run (paired with
|
None
|
reduce_op
|
callable
|
Reduction applied to the statuses within a bar segment to pick
the status (and therefore color) representing that segment
(default |
max
|
reduce_ignore_default
|
bool
|
If |
False
|
legend
|
bool
|
If |
False
|
legend_format
|
callable or str
|
Customizes the legend line. A callable is invoked as
|
None
|
legend_show_default
|
bool
|
If |
False
|
prune_legend
|
bool
|
If |
False
|
**kwargs
|
Forwarded to |
{}
|
__iter__()
Iterate over the wrapped iterable, resetting each item's status.
Before yielding each item, _current_item_idx is set and its status
is reset to default_status, so a fresh item that isn't explicitly
tag()-ed still renders with the default color. _current_item_idx
is kept as its own counter - distinct from self.n - since it must
always be exactly the 0-based position of the item currently being
tagged, regardless of self.n's own semantics (which can start at a
non-zero initial, be mutated by user code, or lag behind n for
display-throttling reasons).
The reset is skipped for a slot that's already been explicitly
written (i.e. isn't still _UNSET_STATUS) - which includes a slot
deliberately tagged default_status, not just a non-default one.
This matters for out-of-order iterables (e.g.
concurrent.futures.as_completed), where i is completion order,
not the item's true position - the loop body tags the true slot via
tag(..., index=true_idx) instead of relying on i. Since i and
true_idx are drawn from the same range(total), i will
eventually collide with some earlier item's true_idx; without the
guard, that coincidence would reset an already-tagged item back to
default_status for no reason related to the item at slot i
itself.
Drives self.iterable directly and manages self.n via explicit
update() calls, rather than delegating to tqdm.__iter__'s
generator, so that breaking out of the loop right after tagging the
current item still counts it. tqdm.__iter__ only advances n in
code positioned after its own yield, which a break skips
entirely (Python throws GeneratorExit at the suspended yield);
worse, its finally unconditionally does self.n = n, overwriting
self.n with that stale, un-incremented count on close - so even
wrapping super().__iter__() and patching self.n up afterwards
would just get clobbered. Owning the try/finally around our own
yield and calling update(1) unconditionally - on normal
resumption and on early break alike - avoids that entirely and
keeps the just-tagged item's colored segment visible on the final
bar.
__iter__old()
Iterate over the wrapped iterable, resetting each item's status.
Before yielding each item, its status is set to default_status
so that a fresh item that isn't explicitly tag()-ed still
renders with the default color.
Manages self.n via explicit update() calls (rather than
delegating to tqdm.__iter__'s generator) so that breaking out of
the loop right after tagging the current item still counts it:
tqdm.__iter__ only advances n in code positioned after its
yield, which a break skips entirely (Python throws
GeneratorExit at the suspended yield) - leaving the just-tagged
item's colored segment past the render boundary and invisible on
the final bar, even though its status was already recorded.
tag(tag, color='none', status=None, index=None)
Record an item's status.
In manual (non-iterable) usage - i.e. no for x in pbar loop driving
__iter__ - there's no notion of a "current item" for tag() to
target, and nothing that would otherwise advance the bar. tag()
falls back to self.n (which, absent an __iter__-driven counter,
correctly identifies the item about to complete) and calls
update(1) itself, so calling tag() alone both records the status
and advances the bar by one - do not also call .update() yourself
for that same item, or it will be counted twice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str or Tag
|
A |
required |
color
|
str
|
Override the tag's color for this call. Ignored when |
'none'
|
status
|
int
|
Override the tag's status integer. Only used the first time a new tag name is registered. |
None
|
index
|
int
|
Item slot to tag, overriding the current item ( Caveat: this first-touch detection only sees slots that went
through Works the same whether or not |
None
|
format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False, unit='it', unit_scale=False, rate=None, bar_format=None, postfix=None, unit_divisor=1000, initial=0, colour=None, **extra_kwargs)
staticmethod
Return a string-based progress bar given some parameters.
Overrides tqdm.format_meter to render {bar} as a ColoredBar
with segments colored by item status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int or float
|
Number of finished iterations. |
required |
total
|
int or float
|
Expected total number of iterations. |
required |
elapsed
|
float
|
Seconds elapsed since start. |
required |
ncols
|
int
|
Total output width. Dynamically resizes |
None
|
prefix
|
str
|
Prefix message; available as |
''
|
ascii
|
bool or str
|
Use ASCII characters instead of Unicode smooth blocks. |
False
|
unit
|
str
|
Iteration unit label (default |
'it'
|
unit_scale
|
bool or int or float
|
If |
False
|
rate
|
float
|
Manual iteration rate override (default: |
None
|
bar_format
|
str
|
Custom bar string. Default: |
None
|
postfix
|
str
|
Additional stats appended after the bar. |
None
|
unit_divisor
|
float
|
Divisor used when |
1000
|
initial
|
int or float
|
Initial counter value (default: |
0
|
colour
|
str
|
Bar colour, e.g. |
None
|
**extra_kwargs
|
Forwarded from |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted meter string ready for display. |
display(msg=None, pos=None)
Render the bar, then print the legend line beneath it if enabled.
Overrides tqdm.display. The legend is only written to a TTY
(it would corrupt output otherwise) and the cursor is returned
to the bar's line afterward so subsequent bar updates overwrite
in place.
msg="" is the sentinel tqdm.close (with leave=False) uses to
blank the bar's line instead of redrawing it - handled the same way
here (blank the legend line rather than redraw it), otherwise a
leave=False bar would still leave its legend behind. See
_render_legend_line for the shared blank-or-draw logic also used
by clear().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
Pre-formatted status message, same as in |
None
|
pos
|
int
|
Line offset for multi-bar displays, same as in |
None
|
clear(nolock=False)
Clear the bar, then blank this bar's legend line too, if drawn.
Overrides tqdm.clear. tqdm.write() clears every live bar (via
this method) before printing, then redraws them - without this
override, the legend line (drawn outside of tqdm's own
line-tracking by display()) would survive that clear, causing the
redraw to land one row off and leave a stale legend line behind on
every tqdm.write() call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nolock
|
bool
|
If |
False
|
close()
Close the bar, then move past the legend line if one was drawn.
Overrides tqdm.close.
TqdmErrorTag
Bases: TqdmTag
A TqdmTag with "warn" and "error" tags pre-registered.
Adds convenience warn()/error() methods on top of the generic
tag() API. "error" has a higher status than "warn", so with the
default reduce_op=max a segment containing both is shown as an error.
__init__(*args, tags=None, **kwargs)
Initialize with the warn/error tags prepended to tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Forwarded to |
()
|
|
tags
|
list of Tag
|
Additional tags to register alongside |
None
|
**kwargs
|
Forwarded to |
{}
|
warn(**kwargs)
Mark the current item with the "warn" tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Forwarded to |
{}
|
error(**kwargs)
Mark the current item with the "error" tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Forwarded to |
{}
|
ColoredBar
Bases: Bar
Drop-in replacement for tqdm.std.Bar supporting per-segment color.
Behaves like Bar when formatted (supports the same format_spec
suffixes a/u/b and numeric width), but colors each rendered
block according to segment_colours using ANSI escape codes.
__init__(frac, default_len=10, charset=UTF, segment_colours=None)
Create a bar renderer for the given fraction complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frac
|
float
|
Fraction of the bar that is filled, in |
required |
default_len
|
int
|
Number of segments to render when |
10
|
charset
|
str
|
String of characters from emptiest to fullest block, used
to render partial segments (default |
UTF
|
segment_colours
|
sequence
|
Color (name, hex string, or |
None
|
__format__(format_spec)
Render the bar to a string, coloring each segment.
Supports the same format_spec as tqdm.std.Bar: an optional
trailing a/u/b selects the ASCII/UTF/BLANK charset, and a
leading integer sets the bar width (negative values subtract
from default_len).
A segment whose segment_colours entry is a (winner_color,
runner_up_color, winner_fraction) tuple - see
TqdmTag._get_colors - renders as a single glyph split
proportionally between the two colors (foreground = winner,
background = runner-up), using the UTF charset's eighths-block
staircase. Only available with the UTF charset; ASCII/BLANK fall
back to the winner color alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_spec
|
str
|
Format specification, e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The rendered, ANSI-colored bar. |