How to print single line progress bar using tqdm in Python?

Viewed 196

Usually, in any tqdm example, a new progress bar is printed for every new iteration, such as below:

Loading   :   0%|          | 0/6 [00:09<?, ?it/s]WARNING: Truncating EEG
Loading   :  17%|█▋        | 1/6 [00:39<01:37, 19.54s/it]WARNING: Truncating EEG
Loading   :  33%|███▎      | 2/6 [00:43<01:18, 19.74s/it]WARNING: Truncating EEG
Loading   :  50%|█████     | 3/6 [05:58<00:45, 15.19s/it]WARNING: Truncating EEG
Loading   :  67%|██████▋   | 4/6 [06:04<03:30, 105.31s/it]WARNING: Truncating EEG
Filtering :  83%|████████▎ | 5/6 [06:09<01:15, 75.37s/it]

However, this author's work shows a slightly different presentation. Instead of a new line, he presents a single progress bar for all iterations.

EEG Complete%|███████████████████████████████▉| 17/17 # illustration only

The following figure should help illustrate the two scenarios:

enter image description here

It is important to note that, both figures, left and right are outputted from the same console (i.e., PyCharm)

I extract the relevance code from the repo and hoping it will print a single bar for all iterations. However, a new line is printed with a new iteration.

May I know what am I missing?

The extracted relevance code is as below:

from tqdm import trange
from multiprocessing import Pool

class TqdmHow:
    def __init__(self):
        self.tqdmloop = False
        self.eeg_files = list(range(1, 17))

    def load(self, verbose=1):
        self.verbose = verbose
        # load EEG and adapt Hypno files
        self.tqdmloop = trange(len(self.eeg_files), desc='Loading data')
        # with Pool(3) as p:
        for i in self.tqdmloop:
            self.load_eeg_hypno(self.eeg_files[i])
            self._print('WARNING: Truncating EEG')
        self.tqdmloop = False

    def _progress(self, description):
        self.tqdmloop.set_description(description + ' ' * (10 - len(description)))
        self.tqdmloop.refresh()

    def load_eeg_hypno(self, eeg_file):
        x = eeg_file
        self._progress('Loading')
        self._progress('Filtering')
        self._progress('other procedure')

    def _print(self, statement):
        if 'WARNING' in statement and self.verbose == 0: return
        if self.tqdmloop:
            self.tqdmloop.write(str(statement))
            self.tqdmloop.refresh()
        else:
            print(statement)

Tqdm_how = TqdmHow()
Tqdm_how.load(verbose=1)
0 Answers
Related