multiple progress bars when calling function with a loop

64 Views Asked by At

I am looping over several files and performing some data analysis with a function. I would like to have two progress bars which update themselves without printing in new lines: One for the progress within the analyse function and one for the progress with the number of files.

My program looks something like this


def function(data):
   data_analysis

for file in files:
   function(data=file)

The function however is located in a seperate script and consists of several sub functions. Which is why I want to manually update the progress bar for the function after specific calculations.

If I try something like:

from tqdm import tqdm
import time


def function():
    p_bar=tqdm(range(10), desc="function", position=1,  colour="green")
    for ii in range(1,6):
        p_bar.update(2)
        time.sleep(0.5)


p_bar2=tqdm(range(10), desc="files", position=0, colour="red")

for ii in range(1,6):
    function()  
    p_bar2.update(2)
    time.sleep(2)

it updates the bars in new lines as can be seen in the picture: progress bars.

However, I would like to have just 2 lines in total all the time and the bars should refresh in the lines.

Does anyone know how I could do that? It doesn't have to be tqdm. I am using Spyder btw.

0

There are 0 best solutions below