Variable¶
Variable displays a live, named value with custom formatting.
Reach for it to track a value that is not the bar’s own progress (a
loss, a learning rate, a username) updated via
bar.update(name=value). format, width, and precision
control the rendering.
- class progressbar.widgets.Variable(name, format='{name}: {formatted_value}', width=6, precision=3, **kwargs)[source]
Bases:
FormatWidgetMixin,VariableMixin,WidgetBaseDisplays a custom variable.
Create a Variable rendering the bar variable name.
- Parameters:
name – data[‘variables’] key to read (see VariableMixin).
format – str.format() template. Besides the usual keys it gets name, value, width, precision, and formatted_value (value formatted per width/ precision if numeric, else ‘-’ * width if falsy).
width – Minimum field width used to format a numeric value.
precision – Decimal precision used to format a numeric value.
**kwargs – Forwarded to the next class in the cooperative __init__ chain.
Example¶
"""``Variable`` displays a live, named value with custom formatting.
Reach for it to track a value that is not the bar's own progress -- a
loss, a learning rate, a username -- updated via
``bar.update(name=value)``. Compare ``Postfix`` for several values
rendered together as one compact suffix, and ``DynamicMessage`` for the
original name of this same widget.
"""
import random
import time
import progressbar
random.seed(0)
STEPS = 24
def main() -> None:
widgets = [
progressbar.Percentage(),
' ',
progressbar.Bar(),
' ',
progressbar.Variable('loss'),
]
with progressbar.ProgressBar(max_value=STEPS, widgets=widgets) as bar:
loss = 1.0
for step in range(STEPS):
loss *= 0.9 + random.random() * 0.05
bar.update(step + 1, loss=loss)
time.sleep(0.005)
if __name__ == '__main__':
main()
See also¶
Postfix: several values rendered together as one compact suffix.
DynamicMessage: the original name for this same widget.