在Python中上传文件的进度条

问题描述

我想将一个大的.ndjson上传到Python。有没有添加进度条的方法,以便我知道上传了多少文件

import json
import pandas as pd


df = map(json.loads,open('dump.ndjson'))
df = pd.DataFrame.from_records(records)

这是上传文件的方式。代码很好,因为当我将文件分割成100个片段时,我可以一张一张地上传。但是,有没有一种方法可以添加进度条,以便我可以立即上传文件并查看上传进度?

PS。我没有考虑gui,因此前往tqdm进度栏。我在想这样的事情,以便可以在控制台中看到进度

解决方法

如果问题是关于GUI的,则可以使用PySimpleGUI轻松使用依赖Tk,Wx或Qt框架的进度条。 这适用于Linux / Windows。

他们食谱中的示例

import PySimpleGUI as sg

# layout the window
layout = [[sg.Text('Uploading...')],[sg.ProgressBar(100,orientation='h',size=(20,20),key='progressbar')],[sg.Cancel()]]

# create the window`
window = sg.Window('My Program Upload',layout)
progress_bar = window['progressbar']
# loop that would normally do something useful
for i in range(1000):
    # check to see if the cancel button was clicked and exit loop if clicked
    event,values = window.read(timeout=10)
    if event == 'Cancel'  or event == sg.WIN_CLOSED:
        break
  # update bar with loop value +1 so that bar eventually reaches the maximum
    progress_bar.UpdateBar(i + 1)
  # TODO: Insert your upload code here
# done with loop... need to destroy the window as it's still open
window.close()

我使用这种进度条并将上载功能保留在单独的线程中,以便UI不会阻塞。