How to upload excel or csv file to flask as a Pandas data frame?
You didn't provide a template you're using in your code (upload.html
).
Also return print(...)
returns None
and None
is not a valid response from a Flask view.
Here's a working example:
upload.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload></form>Shape is: {{ shape }}</body></html>
app.py
from flask import Flask, request, render_templateimport pandas as pdapp = Flask(__name__)@app.route('/upload', methods=['GET', 'POST'])def upload(): if request.method == 'POST': df = pd.read_csv(request.files.get('file')) return render_template('upload.html', shape=df.shape) return render_template('upload.html')if __name__ == '__main__': app.run(debug=True)
dummy.csv
id,name,surname1,John,Doe2,Jane,Doe