Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep tqdm progress bar on the bottom of the terminal?

Here is the code

def download_from_dict(path_link_dict, folder)
    counter = 0
    for path, link, name in tqdm(path_link_dict):

        counter = counter + 1
        if os.path.isfile(folder + path + name):
            print('[ Already there! ] ' + name)
            continue

        if not os.path.isdir(folder + path):
            os.makedirs(folder + path)

        response = requests.get(link, headers=HEADERS)
        with open(folder + path + name, 'wb') as file:
            file.write(response.content)
        print('[*] Downloaded ' + name)

output is

progress bar..
[*] Downloaded something..
Progress bar..
[*] Downloaded something

desired output( I want the bar to stay on the bottom of the terminal. )

[*] Downloaded something
[*] Downloaded something
[*] Downloaded something
progress bar..

I have tried using the leave=False, position=0, and barfmt.. parameters for the tqdm function, but it didn't work I also have tried using \r before [*] Downloaded, but the bar is longer than the printed statement so it clears only a part of it. I have gone to the tqdm docs, and i couldn't solve this problem can you help?

like image 266
grayhatter Avatar asked Dec 01 '25 16:12

grayhatter


1 Answers

it is not an easy and straightforward solution in here, but there are interesting discussions and solutions on how to do that on a couple of previous questions here in stackoverflow: Redirect print command in python script through tqdm.write() and python progress bar using tqdm not staying on a single line

It is also going to depend if you require it in the standard terminal or jupyter notebook or another output...

like image 163
Wilson Salgado Avatar answered Dec 03 '25 06:12

Wilson Salgado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!