ThunderSpark
ThunderSpark

Reputation: 109

Python: Cannot get the progress Bar to appear

I am trying to program in a progress bar in my program to keep track of progress of a certain route. Now I am just testing the progress bar itself, by the print command before I put the actual work in. UPDATE: I am working in the Visual Studio 2019 Community IDE.

The thing is when I run my console program, the progress bar does not seem to appear, the work inside does but the progress bar itself does not.

Here is the code so far. Not this part of the code just to make things work, and this is the testing phase. Actual code: As requested here is an update of the code: Packages:

import requests
import json
import os
import time
from datetime import timedelta
from datetime import datetime
from datetime import date
import pandas as pd
import shutil
import zipfile
import smtplib, ssl
from progress.bar import Bar
from time import sleep

Code that happens:

def fetch_data_points(url: str):
    folder_path_reset("api_request_jsons","csv","Geographic_information")
    total_start_time = start_time_measure()
    start_time = start_time_measure(
        'Starting Phase 1: First request from API: Data Points')
    # Placeholder for limit: please do not remove = 1000000000 -JJ
    response = requests.get(url,params={"limit": 100})
    API_status_report(response)
    end_time_measure(total_start_time, "Request completed: ")
    end_time_measure(total_start_time, "End of Phase 1, completed in: ")
    return response.json()


def fetch_details_of_data_points(url: str):
    input_json = fetch_data_points(url)
    fetch_points_save(input_json)
    all_location_points_details = []
    amount_of_objects = len(input_json)
    i = 0
    total_start_time = start_time_measure()
    start_time = start_time_measure(f'Starting Phase 2: Second request from API: {str(amount_of_objects)} requested')
    with Bar('Loading', fill='@', suffix='%(percent).1f%% - %(eta)ds') as bar:
        for i in range(amount_of_objects):
            print("progres bar cycle")
            bar.next()
            bar.finish()
    print("progress bar test complete")

Console:

Starting Phase 1: First request from API: Data Points
Successfull connection!
Request completed: 0:00:00.219000
End of Phase 1, completed in: 0:00:00.219000
Saving points
Exported_data\api_request_jsons\Fetch_points\Points.json saved
Point saved: 0:00:00.016000
Data saved. Total time of program run: 0:00:00.016000
Starting Phase 2: Second request from API: 100 requested
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progres bar cycle
progress bar test complete

enter image description here

What is going on here? The rest of the code I have works.

Update: I have found a confirmation that the Progress bar does work, but in my Visual Studio IDE, it show does show. In Visual Studio Code, however it shows, but not exacly the way I want.

Loading |                                | 0.0% - 0sprogress bar cycle
Loading |                                | 1.0% - 0s
progress bar cycle
Loading |                                | 2.0% - 0s
progress bar cycle
Loading |                                | 3.0% - 0s
progress bar cycle
Loading |@                               | 4.0% - 0s
progress bar cycle
Loading |@                               | 5.0% - 0s
progress bar cycle
Loading |@                               | 6.0% - 0s
progress bar cycle
Loading |@@                              | 7.0% - 0s
progress bar cycle
Loading |@@                              | 8.0% - 0s
progress bar cycle
Loading |@@                              | 9.0% - 0s
progress bar cycle
Loading |@@@                             | 10.0% - 0s
progress bar cycle
Loading |@@@                             | 11.0% - 0s
progress bar cycle
Loading |@@@                             | 12.0% - 0s
progress bar cycle
Loading |@@@@                            | 13.0% - 0s
progress bar cycle
Loading |@@@@                            | 14.0% - 0s
progress bar cycle
Loading |@@@@                            | 15.0% - 0s
progress bar cycle
Loading |@@@@@                           | 16.0% - 0s
progress bar cycle
Loading |@@@@@                           | 17.0% - 0s
progress bar cycle
Loading |@@@@@                           | 18.0% - 0s
progress bar cycle
Loading |@@@@@@                          | 19.0% - 0s
progress bar cycle
Loading |@@@@@@                          | 20.0% - 0s
progress bar cycle
Loading |@@@@@@                          | 21.0% - 0s
progress bar cycle
Loading |@@@@@@@                         | 22.0% - 0s
progress bar cycle
Loading |@@@@@@@                         | 23.0% - 0s
progress bar cycle
Loading |@@@@@@@                         | 24.0% - 0s
progress bar cycle
Loading |@@@@@@@@                        | 25.0% - 0s
progress bar cycle
Loading |@@@@@@@@                        | 26.0% - 0s
progress bar cycle
Loading |@@@@@@@@                        | 27.0% - 0s
progress bar cycle
Loading |@@@@@@@@                        | 28.0% - 0s
progress bar cycle
Loading |@@@@@@@@@                       | 29.0% - 0s
progress bar cycle
Loading |@@@@@@@@@                       | 30.0% - 0s
progress bar cycle
Loading |@@@@@@@@@                       | 31.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@                      | 32.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@                      | 33.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@                      | 34.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@                     | 35.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@                     | 36.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@                     | 37.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@                    | 38.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@                    | 39.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@                    | 40.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@                   | 41.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@                   | 42.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@                   | 43.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@                  | 44.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@                  | 45.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@                  | 46.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@                 | 47.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@                 | 48.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@                 | 49.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@                | 50.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@                | 51.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@                | 52.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@                | 53.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@               | 54.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@               | 55.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@               | 56.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@              | 57.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@              | 58.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@              | 59.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@             | 60.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@             | 61.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@             | 62.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@            | 63.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@            | 64.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@            | 65.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@           | 66.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@           | 67.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@           | 68.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@          | 69.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@          | 70.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@          | 71.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@         | 72.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@         | 73.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@         | 74.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@        | 75.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@        | 76.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@        | 77.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@        | 78.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@       | 79.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@       | 80.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@       | 81.0% - 0s
progress bar cycle
25hLoading |@@@@@@@@@@@@@@@@@@@@@@@@@@      | 82.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@      | 83.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@      | 84.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@     | 85.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@     | 86.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@     | 87.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@    | 88.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@    | 89.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@    | 90.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@   | 91.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@   | 92.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@   | 93.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  | 94.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  | 95.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  | 96.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | 97.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | 98.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | 99.0% - 0s
progress bar cycle
Loading |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| 100.0% - 0s
progress bar test complete
Request completed in: 0:00:12

enter image description here

Why does this happen?

Upvotes: 0

Views: 627

Answers (1)

madbird
madbird

Reputation: 1379

This looks like terminal in that not in tty mode + forgotten flush=True somewhere. Try addin it in a loop like below:

    with Bar('Loading', fill='@', suffix='%(percent).1f%% - %(eta)ds') as bar:
        for i in range(amount_of_objects):
            print("progres bar cycle")
            bar.next()
            # bar.finish()  << This should be removed
    print("progress bar test complete")

UPD: As I noticed while writing the answer, you're calling bar.finish() together with bar.next(), per each loop iteration. It redraws progress bar immediately after it was printed. BTW you don't have to call finish at all - that's pretty common case of contextmanagers usage.

(And here's our progress.bar)


About the last part of your question: all terminal-based progress bars are actively using an ASCII escape sequences like "\r". But when the output of the python process is not attached to a terminal, the application consuming this output usually cant interpret "\r" exactly like you would see it in a TTY.

But: you may decrease the heartbeat interval or number of iterations (like print once per 10 iterations or once per second). This will surely helps with slowdown issue, because the weak part of most of modern editors is rendering.

Upvotes: 1

Related Questions