progressbar.bar module¶
- class progressbar.bar.ProgressBarMixinBase(**kwargs)[source]¶
Bases:
ABC
- term_width: int = 80¶
The terminal width. This should be automatically detected but will fall back to 80 if auto detection is not possible.
- widgets: MutableSequence[WidgetBase | str]¶
The widgets to render, defaults to the result of default_widget()
- max_error: bool¶
When going beyond the max_value, raise an error if True or silently ignore otherwise
- widget_kwargs: Dict[str, Any]¶
The default keyword arguments for the default_widgets if no widgets are configured
- min_poll_interval: float¶
The minimum interval to poll for updates in seconds even if there are no updates
- num_intervals: int = 0¶
The number of intervals that can fit on the screen with a minimum of 100
- Type:
Deprecated
- next_update: int = 0¶
The next_update is kept for compatibility with external libs: https://github.com/WoLpH/python-progressbar/issues/207
- Type:
Deprecated
- max_value: float | ~typing.Type[<class 'progressbar.base.UnknownLength'>]¶
Maximum (and final) value. Beyond this value an error will be raised unless the max_error parameter is False.
- class progressbar.bar.ProgressBarBase(**kwargs)[source]¶
Bases:
Iterable
,ProgressBarMixinBase
- class progressbar.bar.DefaultFdMixin(fd: ~typing.TextIO = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, is_terminal: bool | None = None, line_breaks: bool | None = None, enable_colors: ~progressbar.env.ColorSupport | None = None, line_offset: int = 0, **kwargs)[source]¶
Bases:
ProgressBarMixinBase
- is_ansi_terminal: bool | None = False¶
Set the terminal to be ANSI compatible. If a terminal is ANSI compatible we will automatically enable colors and disable line_breaks.
- is_terminal: bool | None¶
Whether the file descriptor is a terminal or not. This is used to determine whether to use ANSI escape codes or not.
- line_breaks: bool | None = True¶
Whether to print line breaks. This is useful for logging the progressbar. When disabled the current line is overwritten.
- enable_colors: ColorSupport = 0¶
Specify the type and number of colors to support. Defaults to auto detection based on the file descriptor type (i.e. interactive terminal) environment variables such as COLORTERM and TERM. Color output can be forced in non-interactive terminals using the PROGRESSBAR_ENABLE_COLORS environment variable which can also be used to force a specific number of colors by specifying 24bit, 256 or 16. For true (24 bit/16M) color support you can use COLORTERM=truecolor. For 256 color support you can use TERM=xterm-256color. For 16 colorsupport you can use TERM=xterm.
- class progressbar.bar.ResizableMixin(term_width: int | None = None, **kwargs)[source]¶
Bases:
ProgressBarMixinBase
- class progressbar.bar.StdRedirectMixin(redirect_stderr: bool = False, redirect_stdout: bool = False, **kwargs)[source]¶
Bases:
DefaultFdMixin
- stdout: WrappingIO | IO¶
- stderr: WrappingIO | IO¶
- class progressbar.bar.ProgressBar(min_value: float = 0, max_value: float | ~typing.Type[<class 'progressbar.base.UnknownLength'>] | None = None, widgets: ~typing.Sequence[~progressbar.widgets.WidgetBase | str] | None = None, left_justify: bool = True, initial_value: float = 0, poll_interval: float | None = None, widget_kwargs: ~typing.Dict[str, ~typing.Any] | None = None, custom_len: ~typing.Callable[[str], int] = <function len_color>, max_error=True, prefix=None, suffix=None, variables=None, min_poll_interval=None, **kwargs)[source]¶
Bases:
StdRedirectMixin
,ResizableMixin
,ProgressBarBase
The ProgressBar class which updates and prints the bar.
- Parameters:
min_value (int) – The minimum/start value for the progress bar
max_value (int) – The maximum/end value for the progress bar. Defaults to _DEFAULT_MAXVAL
widgets (list) – The widgets to render, defaults to the result of default_widget()
left_justify (bool) – Justify to the left if True or the right if False
initial_value (int) – The value to start with
poll_interval (float) – The update interval in seconds. Note that if your widgets include timers or animations, the actual interval may be smaller (faster updates). Also note that updates never happens faster than min_poll_interval which can be used for reduced output in logs
min_poll_interval (float) – The minimum update interval in seconds. The bar will _not_ be updated faster than this, despite changes in the progress, unless force=True. This is limited to be at least _MINIMUM_UPDATE_INTERVAL. If available, it is also bound by the environment variable PROGRESSBAR_MINIMUM_UPDATE_INTERVAL
widget_kwargs (dict) – The default keyword arguments for widgets
custom_len (function) – Method to override how the line width is calculated. When using non-latin characters the width calculation might be off by default
max_error (bool) – When True the progressbar will raise an error if it goes beyond it’s set max_value. Otherwise the max_value is simply raised when needed prefix (str): Prefix the progressbar with the given string suffix (str): Prefix the progressbar with the given string
variables (dict) – User-defined variables variables that can be used from a label using format=’{variables.my_var}’. These values can be updated using bar.update(my_var=’newValue’) This can also be used to set initial values for variables’ widgets
line_offset (int) – The number of lines to offset the progressbar from your current line. This is useful if you have other output or multiple progressbars
A common way of using it is like:
>>> progress = ProgressBar().start() >>> for i in range(100): ... progress.update(i + 1) ... # do something ... >>> progress.finish()
You can also use a ProgressBar as an iterator:
>>> progress = ProgressBar() >>> some_iterable = range(100) >>> for i in progress(some_iterable): ... # do something ... pass ...
Since the progress bar is incredibly customizable you can specify different widgets of any type in any order. You can even write your own widgets! However, since there are already a good number of widgets you should probably play around with them before moving on to create your own widgets.
The term_width parameter represents the current terminal width. If the parameter is set to an integer then the progress bar will use that, otherwise it will attempt to determine the terminal width falling back to 80 columns if the width cannot be determined.
When implementing a widget’s update method you are passed a reference to the current progress bar. As a result, you have access to the ProgressBar’s methods and attributes. Although there is nothing preventing you from changing the ProgressBar you should treat it as read only.
- max_value: float | ~typing.Type[<class 'progressbar.base.UnknownLength'>]¶
Maximum (and final) value. Beyond this value an error will be raised unless the max_error parameter is False.
- max_error: bool¶
When going beyond the max_value, raise an error if True or silently ignore otherwise
- widgets: MutableSequence[WidgetBase | str]¶
The widgets to render, defaults to the result of default_widget()
- widget_kwargs: Dict[str, Any]¶
The default keyword arguments for the default_widgets if no widgets are configured
- min_poll_interval: float¶
The minimum interval to poll for updates in seconds even if there are no updates
- property dynamic_messages¶
- property percentage: float | None¶
Return current percentage, returns None if no max_value is given.
>>> progress = ProgressBar() >>> progress.max_value = 10 >>> progress.min_value = 0 >>> progress.value = 0 >>> progress.percentage 0.0 >>> >>> progress.value = 1 >>> progress.percentage 10.0 >>> progress.value = 10 >>> progress.percentage 100.0 >>> progress.min_value = -10 >>> progress.percentage 100.0 >>> progress.value = 0 >>> progress.percentage 50.0 >>> progress.value = 5 >>> progress.percentage 75.0 >>> progress.value = -5 >>> progress.percentage 25.0 >>> progress.max_value = None >>> progress.percentage
- data() Dict[str, Any] [source]¶
- Returns:
max_value: The maximum value (can be None with iterators)
start_time: Start time of the widget
last_update_time: Last update time of the widget
end_time: End time of the widget
value: The current value
previous_value: The previous value
updates: The total update count
total_seconds_elapsed: The seconds since the bar started
seconds_elapsed: The seconds since the bar started modulo 60
minutes_elapsed: The minutes since the bar started modulo 60
hours_elapsed: The hours since the bar started modulo 24
days_elapsed: The hours since the bar started
time_elapsed: The raw elapsed datetime.timedelta object
percentage: Percentage as a float or None if no max_value is available
dynamic_messages: Deprecated, use variables instead.
variables: Dictionary of user-defined variables for the
Variable
’s.
- Return type:
- next()¶
- stdout: WrappingIO | IO¶
- stderr: WrappingIO | IO¶
- is_terminal: bool | None¶
Whether the file descriptor is a terminal or not. This is used to determine whether to use ANSI escape codes or not.
- extra: Dict[str, Any]¶
Extra data for widgets with persistent state. This is used by sampling widgets for example. Since widgets can be shared between multiple progressbars we need to store the state with the progressbar.
- start(max_value=None, init=True, *args, **kwargs)[source]¶
Starts measuring time, and prints the bar at 0%.
It returns self so you can use it like this:
- Parameters:
>>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish()
- finish(end='\n', dirty=False)[source]¶
Puts the ProgressBar bar in the finished state.
Also flushes and disables output buffering if this was the last progressbar running.
- property currval¶
Legacy method to make progressbar-2 compatible with the original progressbar package.
- class progressbar.bar.DataTransferBar(min_value: float = 0, max_value: float | ~typing.Type[<class 'progressbar.base.UnknownLength'>] | None = None, widgets: ~typing.Sequence[~progressbar.widgets.WidgetBase | str] | None = None, left_justify: bool = True, initial_value: float = 0, poll_interval: float | None = None, widget_kwargs: ~typing.Dict[str, ~typing.Any] | None = None, custom_len: ~typing.Callable[[str], int] = <function len_color>, max_error=True, prefix=None, suffix=None, variables=None, min_poll_interval=None, **kwargs)[source]¶
Bases:
ProgressBar
A progress bar with sensible defaults for downloads etc.
This assumes that the values its given are numbers of bytes.
- class progressbar.bar.NullBar(min_value: float = 0, max_value: float | ~typing.Type[<class 'progressbar.base.UnknownLength'>] | None = None, widgets: ~typing.Sequence[~progressbar.widgets.WidgetBase | str] | None = None, left_justify: bool = True, initial_value: float = 0, poll_interval: float | None = None, widget_kwargs: ~typing.Dict[str, ~typing.Any] | None = None, custom_len: ~typing.Callable[[str], int] = <function len_color>, max_error=True, prefix=None, suffix=None, variables=None, min_poll_interval=None, **kwargs)[source]¶
Bases:
ProgressBar
Progress bar that does absolutely nothing. Useful for single verbosity flags.
- start(*args, **kwargs)[source]¶
Starts measuring time, and prints the bar at 0%.
It returns self so you can use it like this:
- Parameters:
>>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish()