为生成的 CSV 文件创建下载链接

问题描述 投票:0回答:1

我有一个 Python Web 应用程序,用户上传图像,代码从该图像生成 CSV。

utils.py

def getCsv(output,im_bgr,im_name, output_dir):

    df1 = pd.DataFrame({"class": class_names)
    coords = ['x_start', 'y_start', 'x_end', 'y_end']
    df2 = pd.DataFrame(data = frac_boxes,  columns = coords) 
    df = pd.concat([df1,df2],axis=1)
    
    # Give the filename you wish to save the file to
    filename = output_dir+im_name.split('.')[0] + '.csv'

    # Use this function to search for any files which match your filename
    files_present = glob.glob(filename)

    # if no matching files, write to csv, if there are matching files, print statement

    if not files_present:
        df.to_csv(filename, index = False)
        
    else:
        print ('WARNING:  File ', filename,  ' already exists!')

app.py

@app.route('/view_process', endpoint='view_process')
def process_files():
    
    if not session.get('logged_in'):
        return redirect(url_for('login_user'))
    
    subprocess.run(['python', 'utils.py'])  # call the utils.py that generates the csv

    return render_template('view_process.html', message='Files processed successfully')

如何将生成的 CSV 发送回

app.py
(不保存到计算机)并在前端创建链接供用户下载?

python csv
1个回答
0
投票

您需要创建会话变量来跟踪子流程完成的处理进度。您可以从浏览器中每 1 秒进行一次 ajax 调用,以查明 CSV 是否已被处理。子流程完成后,结果可以存储在会话变量中,并在浏览器请求时提供服务。

© www.soinside.com 2019 - 2024. All rights reserved.