我使用这段代码要求用户上传一个文件,我想将其读入数据框。 然后这个数据框应该显示为页面上的输出。
我应该在返回中写什么,以完成这个?
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()
最佳答案
我想您想要的准系统版本就是这个。但这显然需要更多的工作。
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 - 在 Python flask 中上传、读取、写入 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925487/