Python,Dash - 点击下载生成的 Excel 文件
Python, Dash - Download Generated Excel File on Click
我正在尝试制作一个 Dash 应用程序,让用户下载通过以下函数生成的 Excel:
def Create_Excel(perfume_name):
workbook = xlsxwriter.Workbook(r'D:\Perfumes\' + perfume_name + ".xlsx")
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Perfume')
worksheet.write('B1', 'Preços(€)')
worksheet.write('C1', 'Link')
worksheet.write('D1', 'Disponibilidade (caso possível)')
worksheet.write('E1', 'OLX Local')
worksheet.write('F1', 'ML')
workbook.close()
def main():
perfume = input()
Create_Excel(perfume)
我有以下适用于静态文件路径的代码:
import dash
import pandas as pd
from dash import html as html
from dash.dependencies import Output, Input
from dash_extensions import Download
from dash_extensions.snippets import send_bytes
from Excel import *
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download xlsx", id="btn"), Download(id="download")])
Create_Excel('Teste')
df = pd.read_excel(r'D:\Perfumes\Teste.xlsx')
@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def generate_xlsx(excel):
def to_xlsx(bytes_io):
xslx_writer = pd.ExcelWriter(bytes_io, engine="xlsxwriter")
df.to_excel(xslx_writer, index=False, sheet_name="sheet1")
xslx_writer.save()
return send_bytes(to_xlsx, "Download_Perfume.xlsx")
if __name__ == '__main__':
app.run_server()
这有效,但我想与其他人分享该应用程序。
objective是用户点击按钮要求香水输入,然后生成excel文件下载。
所以文件路径是可变的,香水输入也会改变。
谢谢!
你可以有 2 个输入来动态获取你想要的值,还有一个提交按钮来为你的生成函数处理这些输入,所以像这样的模板:
app.layout = html.Div(children=[
dcc.Input(id="input_1"),
dcc.Input(id="input_2"),
html.Button('Submit', id='submit-val'),
dbc.Label(id="result")
])
@app.callback(
Output("result", "children"),
[
Input("submit-val", "n_clicks"),
],
[
State("input_1", "value"),
State("input_2", "value")
], prevent_initial_call=True
)
def get_inputs(_, input_1, input_2):
return f"{input_1}, {input_2}"
我正在尝试制作一个 Dash 应用程序,让用户下载通过以下函数生成的 Excel:
def Create_Excel(perfume_name):
workbook = xlsxwriter.Workbook(r'D:\Perfumes\' + perfume_name + ".xlsx")
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Perfume')
worksheet.write('B1', 'Preços(€)')
worksheet.write('C1', 'Link')
worksheet.write('D1', 'Disponibilidade (caso possível)')
worksheet.write('E1', 'OLX Local')
worksheet.write('F1', 'ML')
workbook.close()
def main():
perfume = input()
Create_Excel(perfume)
我有以下适用于静态文件路径的代码:
import dash
import pandas as pd
from dash import html as html
from dash.dependencies import Output, Input
from dash_extensions import Download
from dash_extensions.snippets import send_bytes
from Excel import *
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download xlsx", id="btn"), Download(id="download")])
Create_Excel('Teste')
df = pd.read_excel(r'D:\Perfumes\Teste.xlsx')
@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def generate_xlsx(excel):
def to_xlsx(bytes_io):
xslx_writer = pd.ExcelWriter(bytes_io, engine="xlsxwriter")
df.to_excel(xslx_writer, index=False, sheet_name="sheet1")
xslx_writer.save()
return send_bytes(to_xlsx, "Download_Perfume.xlsx")
if __name__ == '__main__':
app.run_server()
这有效,但我想与其他人分享该应用程序。
objective是用户点击按钮要求香水输入,然后生成excel文件下载。 所以文件路径是可变的,香水输入也会改变。
谢谢!
你可以有 2 个输入来动态获取你想要的值,还有一个提交按钮来为你的生成函数处理这些输入,所以像这样的模板:
app.layout = html.Div(children=[
dcc.Input(id="input_1"),
dcc.Input(id="input_2"),
html.Button('Submit', id='submit-val'),
dbc.Label(id="result")
])
@app.callback(
Output("result", "children"),
[
Input("submit-val", "n_clicks"),
],
[
State("input_1", "value"),
State("input_2", "value")
], prevent_initial_call=True
)
def get_inputs(_, input_1, input_2):
return f"{input_1}, {input_2}"