Progress Trackers

ProgressBar Class APIs

class okaara.progress.ProgressBar(prompt, width=40, show_trailing_percentage=True, fill='=', left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, render_tag=None)
__init__(prompt, width=40, show_trailing_percentage=True, fill='=', left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, render_tag=None)
Parameters:
  • prompt (okaara.prompt.Prompt) – prompt instance to write to
  • width (int) – number of characters wide the progress bar should be; this includes both the fill and the left/right ticks but does not include the trailing percentage if indicated
  • show_trailing_percentage (bool) – if True, the current percentage complete will be listed after the progress bar; defaults to False
  • fill (str) – character to use as the filled value of the progress bar; this must be a single character or the math gets messed up
  • left_tick (str) – displayed on the left side of the progress bar
  • right_tick (str) – displayed on the right side of the progress bar
  • in_progress_color (str) – color to render the progress bar while it is incomplete (will also be used for completed bar if completed_color isn’t specified)
  • completed_color (str) – color to render the progress bar once it is completely filled
  • render_tag (object) – if specified, when the bar itself is written to the prompt it will pass this tag
clear()

Deletes anything rendered by the bar. This may be called after the long-running task has finished to remove the bar from the screen. This must be called before attemping to write anything new to the prompt.

iterator(iterable, message_func=None)

Wraps an iterator to automatically make the appropriate calls into the progress bar on each iteration. The supplied message_func can be used to derive a message for each step in the iteration. For example:

it = pb.iterator(items, message_func=lambda x : 'Generated message: %s' % x)
for i in it:
  # do stuff
Parameters:
  • iterable (iterator) – iterator to wrap
  • message_func (function) – called on each step of the iteration, passing in the latest item retrieved from the iterator
Returns:

iterator that will draw contents from the supplied iterator and automatically update the progress bar

Return type:

iterator

render(step, total, message=None)

Renders the progress bar. The percentage filled will be calculated using the step and total parameters (step / total).

If message is provided, it will be displayed below the progress bar. The message will be deleted on the next call to update and can be used to provide more information on the current step being rendered.

Spinner Class APIs

class okaara.progress.Spinner(prompt, sequence=['-', '\', '|', '/'], left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, spin_tag=None)
__init__(prompt, sequence=['-', '\', '|', '/'], left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, spin_tag=None)
Parameters:
  • prompt (L{Prompt}) – prompt instance to write to
  • sequence (list) – list of characters to iterate over while spinning
  • left_tick (str) – displayed on the left side of the spinner
  • right_tick (str) – displayed on the right side of the spinner
  • in_progress_color (str) – color to render the spinner while it is incomplete (will also be used for completed bar if completed_color isn’t specified)
  • completed_color (str) – color to render the spinner once it is completely filled
  • spin_tag (object) – if specified, this tag will be passed to the write call each time the spinner is updated
clear()

Deletes anything rendered by the spinner. This may be called after the long-running task has finished to remove the spinner from the screen. This must be called before attemping to write anything new to the prompt.

iterator(iterable)

Wraps an iterator to automatically render the next step in the spinner sequence at each pass through it.

Parameters:iterable (iterator) – iterator to wrap
Returns:iterator that will draw contents from the supplied iterator and automatically update the progress bar
Return type:iterator
next(message=None, finished=False)

Renders the next image in the spinner sequence.

Parameters:
  • finished – if true, the spinner will apply coloring based on the completed_color field; defaults to false
  • finished – bool

ThreadedSpinner Class APIs

class okaara.progress.ThreadedSpinner(prompt, refresh_seconds=0.5, timeout_seconds=30, sequence=['-', '\', '|', '/'], left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, spin_tag=None)

Renders a spinner in a separate thread at a regular interval. This is useful in cases where each step in the actual code executing while the spinner is running takes a variable amount of time; this will mask those differences from the user and result in a smooth spin.

Once instantiated, the start() method is used to begin the rendering. Each step is rendered at a rate specified in refresh_seconds in the constructor. The spinner will continue to render until stop() is called. Callers should be careful to not use the prompt instance while the spinner is running.

Due to its behavior, the iterator() method in the Spinner base class is not supported.

__init__(prompt, refresh_seconds=0.5, timeout_seconds=30, sequence=['-', '\', '|', '/'], left_tick='[', right_tick=']', in_progress_color=None, completed_color=None, spin_tag=None)
Parameters:
  • refresh_seconds (float) – time in seconds between rendering each step in the spinner’s sequence
  • timeout_seconds – time in seconds after which the spinner will automatically stop
start()

Causes the spinner to begin rendering steps. The rendering will be done through the prompt supplied in the constructor however it will be done in a separate thread. This call will immediately return and the spinning will begin.

Callers should be careful to call stop() before attempting to use the prompt again. Bad things would happen if the spinner thread continued to attempt to update while other content was written to the prompt.

If the spinner is already running from a previous call to start(), this call has no effect.

stop(clear=False)

Causes the spinner to stop spinning. The thread is not immediately killed but instead allowed to trigger one more step in the sequence. This call will block until that step has been rendered. This shouldn’t be noticable except in cases of a very high value for refresh_seconds.

Project Versions

Table Of Contents

Previous topic

Input/Output

Next topic

Interactive Shell

This Page