При нажатии кнопки я хочу использовать значения из нескольких полей ввода в html и использовать их в качестве параметров для загрузки некоторых данных.Затем я пытаюсь преобразовать это в CSV и загрузить его в виде файла CSV.Я обнаружил в javascript, что ответ содержит данные в формате csv, но файл не загружается.Чего мне не хватает?
.html файл:
***html I left out to keep this short***
<input type="button" value="Download CSV" onclick="downloadacsfile()">
.js файл:
function downloadacsfile() {
var acs_variable = document.getElementById('variableid').value;
var state = document.getElementById('stateselection').value;
var acs_type = document.getElementById('acs_type').value;
var year = document.getElementById('years').value;
$.get("/api/acs_download", {acs_variable: acs_variable, state:state, acs_type:acs_type, year:year }, function(response){
console.log(response.exist);
});
}
.py файл:
app.add_url_rule('/api/acs_download', 'acs_download', acs_download, methods=['GET'])
def acs_download():
acs_variable = request.args['acs_variable']
state = request.args["state"]
acs_type = request.args["acs_type"]
year = request.args["year"]
params = {'acs_type': acs_type,
'year': year,
'variable': acs_variable,
'state': state}
###Gets data from 3rd party API to csv format###
datafetch = fetcher.Fetcher(api_key)
data = datafetch.get_census_data(**params)
data = data.to_csv(index=False)
return Response(
data,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=testfile.csv"})