Я новичок в колбе и JavaScript, и у меня возникли сомнения.
Мне нужно создать файл Excel и отправить его пользователю, но я не знаю, почему это не работает:
Сценарий похож на этот:
function return_file(){
window.get_params= {
'user': $('#user').val()
};
$.post('/return_file', get_param, file.xlsx);
}
Эта функция вызывается кнопкой:
<a id="export_button" class="waves-effect waves-light btn btn-large left grey lighten-3 black-text" onclick="return_file()"><i class="material-icons">file_download</i></a>
Наконец, код Python создает файл с информацией на сервере и отправляет его:
@app.route('/return_file/')
def return_file():
# The real one creates a query to a server and create a dataframe with the input
df_1=pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
###########
#create an output stream
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
#taken from the original question
df_1.to_excel(writer, startrow = 0, merge_cells = False, sheet_name = "user info")
workbook = writer.book
worksheet = writer.sheets["user info"]
format = workbook.add_format()
format.set_bg_color('#eeeeee')
worksheet.set_column(0,9,28)
#the writer has done its job
writer.close()
#go back to the beginning of the stream
output.seek(0)
filename=datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".xlsx"
#finally return the file
return send_file(output, attachment_filename=filename, as_attachment=True)
Наконец, после нажатия кнопки должен быть загружен файл Excel.
Большое спасибо за помощь
Мой код основан на: https://pythonprogramming.net/flask-send-file-tutorial/