Python progress bar using tqdm not staying on one line

I am trying to run a script that is trying to install modules on a centos7 system using puppet control. I want to implement a progress bar for an installation that runs while the script is running. For this, I use the tqdm module. this is clarity in how I implemented the module:

from tqdm import tqdm for i in tqdm(commands): res = run_apply(i) 

Here run_apply () is a function that actually handles the execution and applies the puppet configuration.

So far so good, I get a progress bar, but it continues to move around the console when progress messages are written to the console. But I need the progress bar to stay constant at the bottom of the console and dynamically update if the start messages do not interfere with the bar. I want the messages related to execution to continue on the console as they want, but the progress bar should stay there from the beginning to the end of the execution.

Below I see:

  File line: 0.00 Package: 0.05 Service: 0.19 File: 0.23 Exec: 0.23 Last run: 1470308227 Config retrieval: 3.90 Total: 4.60 Version: Config: 1470308220 Puppet: 3.7.3 now here x result: 2 38%|█████████████████████████████████████▋ | 5/13 [00:29<00:51, 6.44s/it]about to: profiles::install::download_packages about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include profiles::install::download_packages" Error: Could not find class profiles::install::download_packages for puppet on node puppet Error: Could not find class profiles::install::download_packages for puppet on node puppet now here x result: 1 46%|█████████████████████████████████████████████▏ | 6/13 [00:32<00:36, 5.27s/it]about to: profiles::install::install about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include profiles::install::install" Error: Could not find class profiles::install::install for puppet on node puppet Error: Could not find class profiles::install::install for puppet on node puppet now here x result: 1 54%|████████████████████████████████████████████████████▊ | 7/13 [00:34<00:26, 4.45s/it]about to: stx_network about to run puppet apply --summarize --detailed-exitcodes --certname puppet -e "include stx_network" Notice: Compiled catalog for puppet in environment production in 0.84 seconds Notice: /Stage[main]/Stx_network/Tidy[purge unused nics]: Tidying File[/etc/sysconfig/network-scripts/ifcfg-lo] ... 

Please let me know how I can achieve what I want.

+6
source share
2 answers

For messages that should be printed above the progress bar, you need to tell tqdm that you are printing messages (otherwise tqdm or any other progress bar may not know that you are displaying messages next to the progress bar).

To do this, you can print your messages using tqdm.write(msg) instead of print(msg) . If you do not want to change run_apply() to use tqdm.write(msg) instead of print(msg) , you can redirect all standard output through tqdm from a top-level script, as described here .

+8
source

Try using: Progressbar

 import Progressbar progress = progressbar.ProgressBar() for i in progress(range(30)): time.sleep(0.1) 

It will look like this: 43% (13 out of 30) | ################################ | Elapsed time: 0:00:01 ETA: 0:00:01

-one
source

All Articles