Python:无法显示进度条

问题描述

我正在尝试在我的程序中的进度条中进行编程,以跟踪特定路线的进度。现在,我只是在将实际工作放入之前通过打印命令测试进度条本身。更新:我在 Visual Studio 2019 社区 IDE 中工作。

问题是当我运行我的控制台程序时,进度条似乎没有出现,里面的工作有但进度条本身没有。

这是目前为止的代码这部分代码不是为了让事情正常运行,这是测试阶段。 实际代码: 根据要求,这里是代码的更新: 包:

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

发生的代码

def fetch_data_points(url: str):
    folder_path_reset("api_request_jsons","csv","Geographic_@R_754_4045@ion")
    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")

控制台:

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

这里发生了什么?我的其余代码有效。

更新:我发现进度条确实有效,但在我的 Visual Studio IDE 中,它显示确实显示。在 Visual Studio Code 中,无论如何它显示,但不是我想要的方式。

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

为什么会发生这种情况?

解决方法

这看起来像终端,不在 tty 模式 + 忘记了 flush=True 某处。 尝试将其添加到如下循环中:

    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:正如我在编写答案时注意到的那样,每次循环迭代时,您都在调用 bar.finish() togetherbar.next()。它在打印后立即重绘进度条。 顺便说一句,您根本不必调用完成 - 这是 contextmanagers 用法的常见情况。

(这是我们的 progress.bar


关于您问题的最后一部分:所有基于终端的进度条都在积极使用像“\r”这样的 ASCII 转义序列。但是当 python 进程的输出未附加到终端时,使用此输出的应用程序通常无法像您在 TTY 中看到的那样解释“\r”。

但是:您可以减少心跳间隔或迭代次数(例如每 10 次迭代打印一次或每秒打印一次)。 这肯定会有助于解决速度缓慢的问题,因为大多数现代编辑器的薄弱环节是渲染