Flask Showing Shape of Uploaded CSV File

Question:

I have app.py file:

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

app = 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)

and upload.html file:

<!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>

and dummy.csv file:

id,name,surname
1,John,Doe
2,Jane,Doe

I would like to show shape of the data when I upload the csv file. I cannot get the shape of the data with my implementation. I appreciate any hints/ helps.

Asked By: mhmtzmn

||

Answers:

Here is the solution:

Create a templates folder and put your upload.html file inside the templates folder. Since, you do not have it inside the templates folder, flask is returning you jinga2 template not found error.

Just like all the images need to be inside static folder, all the html templates need to be inside the templates folder inorder for your application to work.

Hope this helps!

Answered By: BishwashK
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.