Uncategorized

Python Nested Progress Bars for Spherical Harmonic Coefficients using Rich


I have a set of nested for loops for spherical harmonic coefficients, and I’d like to use Rich to show the progress of the nested components.

import numpy as np
from time import sleep

l = 32
for ll in np.arange(l):
    # I want a progress bar for ll here

    for mm in np.arange(-1*ll, ll+1):
        # I want a progress bar for mm here
        sleep(0.01)
        print(f"l: [{ll}], m: [{mm}]")

This github issue provides a partial solution https://github.com/Textualize/rich/discussions/2272, but assumes that each sub loop has the same number of entries:

from rich.progress import Progress
from time import sleep

with Progress() as pb:
    t1 = pb.add_task('inner', total=10)
    t2 = pb.add_task('outer', total=100)

    for i in range(100):
        for j in range(10):
            print(f"Verbose info! {i, j}")
            sleep(0.1)
            pb.update(task_id=t1, completed=j + 1)
        pb.update(task_id=t2, completed=i + 1)

While I love tqdm, and have achieved partial success using the leave=False keyword, I’m not satisfied with how I loose the last output once both loops have been completed

import numpy as np
from tqdm import tqdm
from time import sleep

l = 32
for ll in tqdm(
    np.arange(l),
    desc=r"𝑙 ",
    disable=False,
):

    for mm in tqdm(
        np.arange(-1 * ll, ll + 1),
        leave=False,
        desc=r"𝑚 ",
        disable=False,
    ):
        sleep(0.01)

Any help would be appreciated!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *