Reputation: 380
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
Upvotes: 6
Views: 18569
Reputation: 827
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
Upvotes: 10
Reputation: 2377
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
Upvotes: 5