We built a site to take a questionnaire using flash and then read and output the xlsx of the template.
However, it worked well offline, but it turns into an Internal Server Error online.
I think the processing has timed out.
Please let me know if there are any alternatives or ways to make the process lighter.
I read and use sqlite data.
I've just started studying programming, so I'd like you to give me some basic points.
Library used: sqlite3, openpyxl, io.bytesio, flash.make_response
PaaS I use: Azure
output=io.BytesIO()
wb=openpyxl.load_workbook('data/template.xlsx')
worksheet=wb.get_sheet_by_name('sheet1')
for x, data in enumerate (datas):
worksheet.cell(row=3+x, column=2).value=int(get_user_transport(data)
worksheet.cell(row=3+x, column=3).value=int(get_user_payment(data)
worksheet.cell(row=3+x, column=4).value=data["user_name" ]
for i, day in enumerate (day_list):
if data[day] == "0":
pass
else:
worksheet.cell(row=3+x, column=5+(i*3)).value=int(data[day].split("-")[0])
worksheet.cell(row=3+x, column=6+(i*3)).value=int(data[day].split("-")[1])
wb.save(output)
output.seek(0)
response=make_response()
response.data=output.read()
response.headers ['Content-Disposition'] = 'attachment; filename=' + file_name
response.mimetype=XLSX_MIMETYPE
output.close()
return response
Internal Server Error means HTTP Status Code is 500.If the cause may not be a timeout, preventing the timeout will not resolve the problem.For example, there is a possibility that there is too much data and insufficient memory, so it is better to investigate the cause of the error carefully.Log the Flask run log and check the log if an error occurs.
If the reason is timeout, you should reduce the time it takes to return a response to the browser.To do this, there is a way to implement and address asynchronous processing.For asynchronous processing, Python often uses a library called Celery.For more information, http://www.celeryproject.org/, but I think some people write articles like blog in Japanese.
The Python for
is slow to repeat, so if you add Pandas, Numpy to make it as Numpy as possible, the process will be faster.However, since it consumes a lot of memory, free servers are more likely to run out of memory.
Creating Excel files online is quite a heavy task, so if you want to respond fundamentally quickly, you can take the following steps:
For server side processing, create Excel file using Sheet.js
on browser side instead of just creating csv or json file.
Instead of creating an Excel file online, an Excel file for output is created in advance and outputted.For example, if you make it every hour, there are many cases where the latest data is not included, but it is not a problem.Also, if you really need the most recent data, I will try to create only that online.
© 2024 OneMinuteCode. All rights reserved.