Я кодировал jQuery с флягой, где по щелчку он должен выполнить поиск SQL и экспортировать фрейм данных как Excel, скрипт:
<script type=text/javascript>
$(function () {
$('a#export_to_excel').bind('click', function () {
$.getJSON($SCRIPT_ROOT + ' /api/sanctionsSearch/download', {
nm: $('input[name="nm"]').val(),
searchtype: $('select[name="searchtype"]').val()
}, function (data) {
$("#download_results").text(data.result);
});
return false;
});
});
Однако в браузере не было ответа, мой код на Python выглядит следующим образом:
from io import BytesIO,StringIO
from flask import render_template, request, url_for, jsonify, redirect, request, Flask, send_file
def index():
#get the dataframe ready and define as 'data', parameters obtained from form input in html
name = request.args.get('nm','', type = str)
type = request.args.get('searchtype','Entity',type = str)
#function get_entity() to get the dataframe
#I have checked and the dataframe is functioning properly
data = get_entity(name,type)
#check if the dataframe is empty
if data.empty == True:
print("its not working bruh...")
word = "No results to export! Please try again!"
return jsonify(result = word)
#store the csv to BytesIO
proxy = StringIO()
data.to_csv(proxy)
mem = BytesIO()
mem.write(proxy.getvalue().encode('utf-8'))
mem.seek(0)
proxy.close()
print("download starting....")
#send file
send_file(mem, as_attachment=True,attachment_filename='Exportresults.csv', mimetype='text/csv')
word = "Download starting!"
return jsonify(result = word)
Может кто-нибудь сказать мне, что не так с моим кодом? «Загрузка начинается ...» была правильно напечатана в html, но загрузка не началась вообще.