Надеюсь, я правильно вас понял. Вот очень простой пример с использованием предоставленного вами массива данных. Вы можете изменить в соответствии с вашими потребностями:
Flask Функции
from flask import Flask, render_template, request, url_for, send_file
import xlsxwriter
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/genexcel', methods=["GET", "POST"])
def createExcel():
if request.method == 'POST':
data = request.get_json(force=True)
# process json data
createExcel(data['data'])
file_path = 'static/files/test.xlsx'
return send_file(file_path, attachment_filename='test.xlsx', as_attachment=True)
def createExcel(data):
workbook = xlsxwriter.Workbook('static/files/test.xlsx')
worksheet = workbook.add_worksheet()
row_no = 0
col_no = 0
for row in data:
col_no = 0
for entry in row:
worksheet.write(row_no, col_no, entry)
col_no += 1
row_no += 1
workbook.close()
app.run(debug=True, port=5010)
index. html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>
<body>
Testing
<button id="genExcel">Direct Download</button>
<button id="genExcelFlask">Flask Download</button>
</body>
<script>
var btn = document.getElementById("genExcel");
var btnFlask = document.getElementById("genExcelFlask");
var dataArray = {
data: [
[1, "A", 100],
[2, "B", 200],
],
};
btn.addEventListener("click", (e) => {
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((resp) => resp.blob())
.then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
// the filename you want
a.download = "todo-1.json";
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert("your file has downloaded!"); // or you know, something with better UX...
})
.catch(() => alert("oh no!"));
});
btnFlask.addEventListener("click", (e) => {
console.log(JSON.stringify(dataArray));
fetch("{{ url_for('createExcel') }}", {
method: "post",
body: JSON.stringify(dataArray),
})
.then((resp) => resp.blob())
.then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
// the filename you want
a.download = "test.xlsx";
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert("your file has downloaded!"); // or you know, something with better UX...
})
.catch(() => alert("oh no!"));
});
</script>
</html>