[Python] Progress bar with multiple stats printed in new lines

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    import numpy as np
    from tqdm import tqdm
    import time

    tot_steps = 100

    pbar = tqdm(total=tot_steps)

    for i in range(tot_steps):
    time.sleep(0.1)
    x, y, z, k = np.random.rand(4)
    pbar.update(1)
    pbar.set_description(
    f"error[{x:.3f}] _ "
    f"samples[{y:.3f}] _ "
    f"min[{z:.3f}] _ "
    f"max[{k:.3f}] _ "
    )


    Output

    error[0.145] _ samples[0.255] _ min[0.286] _ max[0.878] _ : 100%|████████████████████| 100/100 [00:13<00:00, 70.96it/s]


    But I would like to have something like this

    100%|████████████████████| 100/100 [00:13<00:00, 70.96it/s]
    error[0.145]
    samples[0.255]
    min[0.286]
    max[0.878]


    With the numbers progressively updated without printing everything everytime.

    That is, I would like to print the logged stats on new lines. However, if I add \n to the print of the progress bar, it doesn't work (it keeps printing new lines).

    Continue reading...

Compartilhe esta Página