Progress bar (using tqdm) prints in a new line everytime the update is called on the tqdm object (VSCode terminal)
Asked Answered
K

2

6

I am trying to print the status of the progress of an optimization algorithm using the tqdm module available in Python, however, every time I try to update it, it prints the progress in a new line, is there a way I could only update the original tqdm bar which is being instantiated at the beginning? My code is as follows which is based out of backtrader backtesting library:

def optimizer_callbacks(cb):
    pbar.update()

def strategy_optim(**kwargs):

    total = np.prod([len(value) for key,value in kwargs.items()])

    csv_file = FDaxCSVData(---data---)
    cerebro = bt.Cerebro()
    cerebro.adddata(csv_file)    
    cerebro.broker.setcash(500000.0)
    cerebro.broker.setcommission(commission=2.0)

    strats = cerebro.optstrategy(strategy_name, printlog = False, **kwargs)

    global pbar
    pbar = tqdm.tqdm(smoothing=0.05, desc='Optimization Runs', total=total)
    cerebro.optcallback(optimizer_callbacks)

    runnings = cerebro.run(optreturn=False, maxcpus=2)  

if __name__=="__main__":
    strategy_optim(periods = [100, 200, 300],  abs_margin= [25, 50, 75], trail_stop=[10, 20, 30, 40])

Output:

Optimization Runs:   0%|                                                    | 0/12 [00:00<?, ?it/s]
Optimization Runs:   8%|██████▉                                             | 1/12 [00:18<03:21, 18.29s/it]
Optimization Runs:  17%|█████████████▊                                      | 2/12 [00:19<01:35,  9.59s/it]
Optimization Runs:  25%|████████████████████▊                               | 3/12 [00:40<02:19, 15.55s/it]

I did check out the other posts on stackoverflow regarding similar issues(most of them are concentrated on jupyter notebook interface) and they didn't solve my error. Also, this is a multitreading process and the cerebro.optcallback calls the optimizer_callbacks function after each iteration of a unique set of values of the params

Kesterson answered 15/9, 2020 at 19:49 Comment(2)
Instead of tqdm.tqdm use tqdm.notebook.tqdm or tqdm.auto.tqdmLettering
using tqdm.notebook.tqdm prints something as follows: HBox(children=(FloatProgress(value=0.0, description='Optimization Runs', max=16.0, style=ProgressStyle(description_width='initial')), HTML(value='')) if I use tqdm.auto.tqdm, the result the same, it simply prints the status bar multiple timesKesterson
P
9

I faced this problem a lot and sometimes position = 0 & leave = True does not works. So, I found one alternate way.

Instead of tqdm.tqdm you can use tqdm.auto.tqdm
or
instead of

from tqdm import tqdm

try using

from tqdm.auto import tqdm
Pressure answered 3/10, 2020 at 6:25 Comment(3)
This solution is awesome, more elegant, and shows a better progress bar! Thanks.Rooke
please explain why this changes thingsKeshiakesia
Doesn't work for me.Jea
T
5

Use the tqdm.tqdm(epochs, position=0, leave=True) to make sure the progress bar stays at the same place and is not printed on the new line each time.

Cheers

Tarpaulin answered 3/11, 2020 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.