I have a problem. I want to get xlsx made from this route, the problem is open other page but this take a lot of time then I do a button with javascript to do fetch and get the file but it isn’t working.
¿What I do wrong? or ¿What I can try?
I’ve saw some solutions with chunks but I don’t know, ¿someone can help me?
<!-- after I try this -->
{% if total_records > 0 %}
<button class="btn btn-success" id="button_download_xslx" type="button">Convertir a
xlsx</button>
<a class="btn btn-primary mx-5" id="anchor-download-xlsx">Descargar</a>
<!-- original idea -->
<a
href="{{ next_page_or_download_xlsx(page='transport_cash_blueprint.download_xlsx', query_params=query_params) }}"
target="_blank"
class="btn btn-primary mx-5">Descargar</a>
{% endif %}
from quart import Blueprint
from quart import request
from quart import send_file, render_template, redirect, url_for
from quart_auth import login_required
from xlsxwriter import Workbook
from asyncio import TaskGroup
from wtforms import Form
from io import BytesIO
from math import ceil
from ..form import TransportCashForm
from ..contants import COLUMNS_TRANSPORT_CASH
from ..database import (
transport_cash_all,
transport_cash_limited_per_page,
transport_cash_total_records,
create_query_transport_cash,
)
transport_cash_blueprint = Blueprint(
"transport_cash_blueprint",
__name__,
url_prefix="/transport_cash",
)
@transport_cash_blueprint.get("/download_xlsx", methods=["GET"])
async def download_xlsx():
query_params = dict(request.args)
output = BytesIO()
workbook = Workbook(output, {"in_memory": True})
worksheet = workbook.add_worksheet()
query = create_query_transport_cash(query_params)
data = await transport_cash_all(query)
for col_num, title_data in enumerate(COLUMNS_TRANSPORT_CASH):
worksheet.write(0, col_num, title_data)
for row_num, row_data in enumerate(data):
for col_num, col_data in enumerate(row_data):
worksheet.write(row_num + 1, col_num, col_data)
workbook.close()
output.seek(0)
return await send_file(output, download_name="test.xlsx", as_attachment=True)
document.getElementById('button_download_xslx').addEventListener('click',
async (event) => {
event.preventDefault();
let descargarEnlace = document.getElementById("anchor-download-xlsx")
let newUrl = query_params()
const response = await fetch(newUrl, {method: "GET"});
const data = await response.blob();
const url = window.URL.createObjectURL(data);
descargarEnlace.href = url; // Asignar la URL al enlace
descargarEnlace.click();
}
)
query_params = () => {
const params = new URLSearchParams(window.location.search);
let url = new URL("http://127.0.0.1:5000/transport_cash/download_xlsx");
let params_dictionary = {
fecha_desde_cuando_value: params.get("fecha_desde_cuando_value"),
fecha_hasta_cuando_value: params.get("fecha_hasta_cuando"),
numero_identificacion_value: params.get("numero_identificacion"),
nombre_razon_social_value: params.get("nombre_razon_social"),
}
// Si el largo del objecto filtrado es mayor a 0 añade las llaves y valores
if (Object.keys(params_dictionary).length > 0) {
// filtramos los valores si es nulo
let filtered_params = Object.fromEntries(
Object.entries(params_dictionary).filter(
([key, value]) => value != null)
)
Object.keys(filtered_params).forEach(
key => url.searchParams.append(key, filtered_params[key])
);
return url
}
return url
}