progressbars#

class progressbars(n=1, total=1, label=None, leave=False, **kwargs)[source]#

Bases: prettyobj

Create multiple progress bars

Useful for tracking the progress of multiple long-running tasks. Unlike regular tqdm instances, this uses a pickable version so it can be used directly in multiprocessing instances.

Parameters:
  • n (int) – number of progress bars to create

  • total (float) – length of the progress bars

  • label (str/list) – an optional prefix for the progress bar, or list of all labels

  • leave (bool) – whether to remove the progress bars when they’re done

  • kwargs (dict) – passed to tqdm.tqdm()

Note: bars are supposed to update in-place, but may appear on separate lines instead if not run in the terminal (e.g. if run in IPython environments like Spyder or Jupyter).

Example:

import sciris as sc
import random

def run_sim(index, ndays, pbs):
    for i in range(ndays):
        val = random.random()
        sc.timedsleep(val*5/ndays)
        pbs.update(index) # Update this progress bar based on the index
    return

nsims = 5
ndays = 365

# Create progress bars
pbs = sc.progressbars(nsims, total=ndays, label='Sim')

# Run tasks
sc.parallelize(run_sim, iterarg=range(nsims), ndays=ndays, pbs=pbs)

# Produces output like:
# Sim 0:  39%|███████████████████████████▊           | 143/365 [00:01<00:01, 137.17it/s]
# Sim 1:  42%|████████████████████████████▉          | 154/365 [00:01<00:01, 148.70it/s]
# Sim 2:  45%|████████████████████████████████       | 165/365 [00:01<00:01, 144.19it/s]
# Sim 3:  44%|███████████████████████████████        | 160/365 [00:01<00:01, 151.22it/s]
# Sim 4:  42%|████████████████████████████▏          | 145/365 [00:01<00:01, 136.75it/s]

New in version 3.0.0.

Methods