Im trying to set a download button for my pandas dataframe (as csv) but I didn't get any success.
Here are the relevant functions of my views.py:
from flask import render_template, make_response
from flask import send_file
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
import seaborn as sns
@app.route("/jinja")
def jinja():
my_name = "Testes"
return render_template("/public/jinja.html", my_name=my_name)
@app.route("/about")
def about():
return render_template("/public/about.html")
#defining parameters
bast_param = [0,5] #ba prefix for below average student
avst_param = [5,7] #av prefix for average student
aast_param = [7,10] #aa prefix for above average student
min_bast = bast_param[0]
max_bast = bast_param[1]
min_avst = avst_param[0]
max_avst = avst_param[1]
min_aast = aast_param[0]
max_aast = aast_param[1]
@app.route("/")
def index():
#students quantities per parameter
bast_qtd = 5
avst_qtd = 3
aast_qtd = 8
st_total = bast_qtd + avst_qtd + aast_qtd
#Defining Subjects
subjects = ["Disciplina 1", "Disciplina 2", "Disciplina 3", "Disciplina 4", "Disciplina 5"]
students = []
#counter for students ids creation
i = 0
#counters and variable for grades creation
a = 0
b = 0
newgradeline = []
grade = []
#creating students and grades
while(i < st_total):
newstudent = random.randint(100000,199999)
#excluding duplicates
if newstudent not in students:
students.append(newstudent)
i = i+1
# In[3]:
#below averagge students
while (a < bast_qtd):
b = 0
newgradeline = []
grade.append(newgradeline)
while (b<len(subjects)):
gen_grade = round(random.uniform(bast_param[0],bast_param[1]),2)
newgradeline.append(gen_grade)
b = b+1
a = a +1
a = 0
#average students
while (a < avst_qtd):
b = 0
newgradeline = []
grade.append(newgradeline)
while (b<len(subjects)):
gen_grade = round(random.uniform(avst_param[0],avst_param[1]),2)
newgradeline.append(gen_grade)
b = b+1
a = a +1
a = 0
#above average students
while (a < aast_qtd):
b = 0
newgradeline = []
grade.append(newgradeline)
while (b<len(subjects)):
gen_grade = round(random.uniform(aast_param[0],aast_param[1]),2)
newgradeline.append(gen_grade)
b = b+1
a = a +1
# In[4]:
#generating table
simulation = pd.DataFrame (grade,index=students, columns=subjects)
return render_template('/public/index.html',table=simulation.to_html(),download_csv=simulation.to_csv(index=True, sep=";"))
@app.route("/parameters")
def parameters():
return render_template('/public/parameters.html', min_bast=min_bast, max_bast=max_bast, min_avst=min_avst,max_avst=max_avst, min_aast=min_aast, max_aast=max_aast)
And this is the html page where I'm currently displaying the csv as a text on the page {{download_csv | safe}}
:
{% extends "/public/templates/public_template.html" %}
{% block title %}Simulador{% endblock%}
{% block main %}
<div class="container">
<div class="row">
<div class="col">
</br>
<h1>Simulador</h1>
<hr/>
{{table| safe}}
<br />
<a class="btn btn-primary" href="/" role="button">New simulation</a>
<a class="btn btn-primary" href="/parameters" role="button">Edit Parameters</a>
</div>
</div>
<div class="row">
<div class="col">
</br>
<hr/>
<h1>CSV File</h1>
{{download_csv | safe}}
</br></br>
<a class="btn btn-primary" href="" role="button">Download CSV</a>
<hr/>
</div>
</div>
</div>
{% endblock %}
but I would like this button <a class="btn btn-primary" href="" role="button">Download CSV</a>
to trigger a CSV download of the dataframe called simulation created on @app.route("/") def index():
of my views.py
I do understand that I need to set up a new route on my views.py to make this download happen, but how do I do that?