我尝试为我的 pandas 数据框(作为 csv)设置下载按钮,但没有取得任何成功。
以下是我的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)
这是 html 页面,我当前在页面 {{download_csv | safe}}
上将 csv 显示为文本:
{% 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 %}
但我想要这个按钮<a class="btn btn-primary" href="" role="button">Download CSV</a>
触发在@app.route("/") def index():
上创建的名为模拟的数据帧的CSV下载我的观点.py
我确实知道我需要在views.py上设置一个新路由才能进行此下载,但我该怎么做?
最佳答案
目前您的按钮下载没有执行任何操作。
1 - 添加 OnClick=someFunction()
到其中,这样当用户单击它时,someFunction()
就会被执行。
<a class="btn btn-primary" onClick="someFunction()" href="" role="button">Download CSV</a>
2 - 在您的 somFunction()
中,将网页重定向到您想要的路线,比如 location.replace("/download");
function someFunction()
{
//..... You can here send user input from HTML to Flask through Ajax POST request if you wish.........//
location.replace("/download");
}
3 - 最后,在您的 Flask 中确保您有一个用于处理 csv 的 /download
路由。
更新如果您不想使用 javascript 函数进行重定向,请尝试以下操作:
1 - 将您的 index() 路由更改为更明显的内容,例如 @app.route("/download")
2 - 将“下载 CSV”按钮的 href 分配给函数的路由
<a class="btn btn-primary" href="{{ url_for('index') }}" role="button">Download CSV</a>
关于python - 如何使用 Flask 为 pandas 数据框设置 "download as csv"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59206548/