在Python烧瓶中上传,读取和写入Excel文件 [英] Upload, read, write excel file in Python flask

查看:526
本文介绍了在Python烧瓶中上传,读取和写入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,要求用户上传文件,我希望将其读取到数据框中. 然后,该数据框应在页面上显示为输出.

Im using this code which asks user to upload a file, which I want to be read into a dataframe. Then this dataframe should be displayed as output on the page.

我应该在退货中写些什么,以完成此任务?

What should I write in the return, so as to accomplish this ?

from flask import Flask, request, jsonify
import flask_excel as excel
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        return jsonify({"result": request.get_array(field_name='file')})
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return 

if __name__ == "__main__":
    app.run()

推荐答案

我想您想要的是准系统版本.但这显然将需要更多的工作.

I guess a barebones version of what you wanted would be this. But this would obviously require more work.

from flask import Flask, request, jsonify
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        print(request.files['file'])
        f = request.files['file']
        data_xls = pd.read_excel(f)
        return data_xls.to_html()
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return 

if __name__ == "__main__":
    app.run()

这篇关于在Python烧瓶中上传,读取和写入Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆