ProgressBar¶
- class astropy.utils.console.ProgressBar(total_or_items, ipython_widget=False, file=None)[source]¶
Bases:
object
A class to display a progress bar in the terminal.
It is designed to be used either with the
with
statement:with ProgressBar(len(items)) as bar: for item in enumerate(items): bar.update()
or as a generator:
for item in ProgressBar(items): item.process()
- Parameters:
- total_or_items
python:int
or python:sequence If an int, the number of increments in the process being tracked. If a sequence, the items to iterate over.
- ipython_widgetbool, optional
If
True
, the progress bar will display as an IPython notebook widget.- filewritable file-like object python:file-like object, optional
The file to write the progress bar to. Defaults to
sys.stdout
. Iffile
is not a tty (as determined by calling itsisatty
member, if any, or special case hacks to detect the IPython console), the progress bar will be completely silent.
- total_or_items
Methods Summary
map
(function, items[, multiprocess, file, ...])Map function over items while displaying a progress bar with percentage complete.
map_unordered
(function, items[, ...])Map function over items, reporting the progress.
update
([value])Update progress bar via the console or notebook accordingly.
Methods Documentation
- classmethod map(function, items, multiprocess=False, file=None, step=100, ipython_widget=False, multiprocessing_start_method=None)[source]¶
Map function over items while displaying a progress bar with percentage complete.
The map operation may run in arbitrary order on the items, but the results are returned in sequential order.
def work(i): print(i) ProgressBar.map(work, range(50))
- Parameters:
- functionpython:function
Function to call for each step
- itemspython:sequence
Sequence where each element is a tuple of arguments to pass to function.
- multiprocessbool,
python:int
, optional If
True
, use themultiprocessing
module to distribute each task to a different processor core. If a number greater than 1, then use that number of cores.- ipython_widgetbool, optional
If
True
, the progress bar will display as an IPython notebook widget.- filewritable file-like object python:file-like object, optional
The file to write the progress bar to. Defaults to
sys.stdout
. Iffile
is not a tty (as determined by calling itsisatty
member, if any), the scrollbar will be completely silent.- step
python:int
, optional Update the progress bar at least every step steps (default: 100). If
multiprocess
isTrue
, this will affect the size of the chunks ofitems
that are submitted as separate tasks to the process pool. A large step size may make the job complete faster ifitems
is very long.- multiprocessing_start_method
python:str
, optional Useful primarily for testing; if in doubt leave it as the default. When using multiprocessing, certain anomalies occur when starting processes with the “spawn” method (the only option on Windows); other anomalies occur with the “fork” method (the default on Linux).
- classmethod map_unordered(function, items, multiprocess=False, file=None, step=100, ipython_widget=False, multiprocessing_start_method=None)[source]¶
Map function over items, reporting the progress.
Does a
map
operation while displaying a progress bar with percentage complete. The map operation may run on arbitrary order on the items, and the results may be returned in arbitrary order.def work(i): print(i) ProgressBar.map(work, range(50))
- Parameters:
- functionpython:function
Function to call for each step
- itemspython:sequence
Sequence where each element is a tuple of arguments to pass to function.
- multiprocessbool,
python:int
, optional If
True
, use themultiprocessing
module to distribute each task to a different processor core. If a number greater than 1, then use that number of cores.- ipython_widgetbool, optional
If
True
, the progress bar will display as an IPython notebook widget.- filewritable file-like object python:file-like object, optional
The file to write the progress bar to. Defaults to
sys.stdout
. Iffile
is not a tty (as determined by calling itsisatty
member, if any), the scrollbar will be completely silent.- step
python:int
, optional Update the progress bar at least every step steps (default: 100). If
multiprocess
isTrue
, this will affect the size of the chunks ofitems
that are submitted as separate tasks to the process pool. A large step size may make the job complete faster ifitems
is very long.- multiprocessing_start_method
python:str
, optional Useful primarily for testing; if in doubt leave it as the default. When using multiprocessing, certain anomalies occur when starting processes with the “spawn” method (the only option on Windows); other anomalies occur with the “fork” method (the default on Linux).