Backends¶
The Progress API includes the following backends:
Configuring the Backend¶
By default, the progress API uses the null backend. Actual progress-reporting
backends can be configured with the PROGRESS_BACKEND
environment variable,
or with the set_backend()
function.
- progress_api.set_backend(impl: ProgressBackend) None ¶
- progress_api.set_backend(impl: ~typing.Callable[[~BCP], ~progress_api.backends.ProgressBackend], *args: ~typing.~BCP, **kwargs: ~typing.~BCP) None
- progress_api.set_backend(impl: str, *args: Any, **kwargs: Any) None
Set the progress backend. The backend can be specified in one of several ways:
A string naming a progress backend. For the backends included with Progress API, this name matches the implementing module name (e.g.
"enlighten"
). Other backends can be registered with an entry point (see implementing-backends).An object implementing the
backends.ProgressBackend
interface.A subclass of
backends.ProgressBackend
that to instantiate.
If the backend is a class (ether a class object, or a backend name), then it is instantiated witht he supplied
args
andkwargs
.- Parameters:
impl – The implementation.
*args – Arguments to pass to the implementation constructor.
**kwargs –
Arguments to pass to the implementation constructor.
Provided Backends¶
Enlighten¶
Progress backend using Enlighten to display progress bars. It supports multiple bars (well) and multi-state bars, and interacts well with logging and other output to standard output and error streams.
- class progress_api.backends.enlighten.EnlightenProgressBackend(manager=None, state_colors=None)¶
Progress bar backend that doesn’t emit any progress.
- create_bar(spec)¶
Create a new progress bar from the given specification.
- Parameters:
spec (ProgressBarSpec)
- Return type:
- class progress_api.backends.enlighten.EnlightenProgress(spec, bar, bars)¶
- Parameters:
spec (ProgressBarSpec)
bar (Counter)
- set_metric(label, value, fmt=None)¶
Set an ”metric” on the progress bar. This is a secondary value that will be displayed along with ETA; it is intended for things like a current measurement (e.g. the current training loss for training a machine learning model).
The format specifier is put into a format string that is passed to
str.format()
, and must include the braces. For example, to format a percentage with 2 decimal points:progress.set_meter('buffer', buf_fill_pct, '{:.2f}%')
Only one meter can be set at a time. A new meter will replace any existing meter. This method remembers the label and format, even if
value
isNone
, so the metric value can be supplied inupdate
.
- update(n=1, state=None, src_state=None, metric=None)¶
Update the progress bar.
- finish()¶
Finish and close this progress bar, releasing resources as appropriate.
TQDM¶
Progress bar backend for tqdm. This backend is quite limited; it does not support multiple states, and it does not have good support for multiple progress bars or interaction with logging, unless used in a Jupyter notebook environment.
- class progress_api.backends.tqdm.TQDMProgressBackend(tqdm=<class 'tqdm.asyncio.tqdm_asyncio'>)¶
TQDM progress bar backend implementation.
- Parameters:
tqdm (type[tqdm])
- create_bar(spec)¶
Create a new progress bar from the given specification.
- Parameters:
spec (ProgressBarSpec)
- Return type:
- class progress_api.backends.tqdm.TQDMProgress(spec, tqdm)¶
- Parameters:
spec (ProgressBarSpec)
tqdm (tqdm)
- set_metric(label, value, fmt=None)¶
Set an ”metric” on the progress bar. This is a secondary value that will be displayed along with ETA; it is intended for things like a current measurement (e.g. the current training loss for training a machine learning model).
The format specifier is put into a format string that is passed to
str.format()
, and must include the braces. For example, to format a percentage with 2 decimal points:progress.set_meter('buffer', buf_fill_pct, '{:.2f}%')
Only one meter can be set at a time. A new meter will replace any existing meter. This method remembers the label and format, even if
value
isNone
, so the metric value can be supplied inupdate
.
- update(n=1, state=None, src_state=None, metric=None)¶
Update the progress bar.
- finish()¶
Finish and close this progress bar, releasing resources as appropriate.
Null¶
Null backend that doesn’t supply any progress.
- class progress_api.backends.null.NullProgressBackend¶
Progress bar backend that doesn’t emit any progress.
- create_bar(spec)¶
Create a new progress bar from the given specification.
- Parameters:
spec (ProgressBarSpec)
- Return type:
Implementing Backends¶
Backends are can be implemented with the following:
Implement the
progress_api.Progress
interface for the backend.Implement the
progress.backends.ProgressBackend
interface to create progress bars using the backend.Register the progress backend with the progress_api.backend entry point:
[project.entry-points."progress_api.backend"] enlighten = "progress_api.backends.enlighten:EnlightenProgressBackend"
- class progress_api.backends.ProgressBackend¶
Interface to be implemented by progress API backends.
Note
Progress backends must be thread-safe.
- abstract create_bar(spec)¶
Create a new progress bar from the given specification.
- Parameters:
spec (ProgressBarSpec)
- Return type:
Backend Data Structures¶
These types are used by the progress API to call the backend.
- class progress_api.backends.ProgressBarSpec(logger, label=None, total=None, unit=None, states=<factory>, leave=False)¶
Class encapsulating a progress bar specification to request a new progress bar from the backend.
- Parameters:
- unit: str | None = None¶
The progress bar’s units. Backends that support binary byte counts should recognize the
bytes
unit here.
- states: list[ProgressState]¶
List of progress states. If no states were specified by the caller, this contains one final state
'finished'
.
- class progress_api.backends.ProgressState(name, final=True)¶
Representation of a progress bar state.