@aaron wrote:
Here is my main code
import os from urllib.parse import quote as urlquote from flask import Flask, send_from_directory,send_file import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output, State from script.modeling_output_to_excel.modeling_output_to_excel import result_to_excel from script.modeling_output_to_excel.generate import model, generate_plot, model_plot UPLOAD_DIRECTORY = "data/interim/test" if not os.path.exists(UPLOAD_DIRECTORY): os.makedirs(UPLOAD_DIRECTORY) server = Flask(__name__, static_folder="assets") app = dash.Dash(server=server) app.config["suppress_callback_exceptions"] = True @server.route("/download/<path:path>") def download(path): """Serve a file from the upload directory.""" return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True) app.layout = html.Div( [ html.Button("Generate The Report", id="download_button", n_clicks=0), dcc.Loading(id="loading"), html.Div(id = "file_list") ], style={"display": "inline-block", "width": "30%", "margin": "auto"}, ) def uploaded_files(): """List the files in the upload directory.""" files = [] for filename in os.listdir(UPLOAD_DIRECTORY): path = os.path.join(UPLOAD_DIRECTORY, filename) if os.path.isfile(path): files.append(filename) return files def file_download_link(filename): location = "/download/{}".format(urlquote(filename)) return html.A( html.Button("Click To Download The Report"), filename, href=location, ) @app.callback( [Output("file_list", "children"), Output("loading","children") ], [Input("download_button", "n_clicks")], ) def update_output(n_clicks): path = UPLOAD_DIRECTORY + '/Report Demo.xlsx' if os.path.exists(path): os.remove(path) if n_clicks > 0: generate_report(). ## This function use to generate file for download files = uploaded_files() return [(file_download_link(filename)) for filename in files], None if __name__ == "__main__": app.run_server( debug=True, port=8099, dev_tools_ui=False, dev_tools_props_check=False )For now,this logic was clicking the button of generate the report get a file,then another button of download was shown up, click the download button, download this file.But,I wanna realize this scene is:click the button generate the report, then the text on the button changed from generate the report to download report,click again this button to execute download.Just one button,not two. Any help will appreciate it
Posts: 1
Participants: 1










