From 34b4cb6477babf29afe838dc0ecb1eac74a6fbd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Thu, 1 Jun 2023 20:48:53 +0200 Subject: [PATCH] Add automatic download excel files --- benchmark/Arguments.py | 10 ++++++++ benchmark/Utils.py | 2 +- benchmark/scripts/app/app.py | 5 ++-- benchmark/scripts/app/main.py | 25 ++++++++++++++++--- benchmark/scripts/app/static/excel/.gitignore | 1 + benchmark/scripts/app/static/js/excelFiles.js | 6 ++++- benchmark/scripts/be_flask.py | 12 ++++++--- 7 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 benchmark/scripts/app/static/excel/.gitignore diff --git a/benchmark/Arguments.py b/benchmark/Arguments.py index cb43ef7..57f490c 100644 --- a/benchmark/Arguments.py +++ b/benchmark/Arguments.py @@ -235,6 +235,16 @@ class Arguments(argparse.ArgumentParser): "help": "number of folds", }, ], + "output": [ + ("-o", "--output"), + { + "type": str, + "default": "local", + "choices": ["local", "docker"], + "required": False, + "help": "in be_flask tells if it is running in local or in docker {local, docker}", + }, + ], "platform": [ ("-P", "--platform"), { diff --git a/benchmark/Utils.py b/benchmark/Utils.py index 33941de..6f6eb31 100644 --- a/benchmark/Utils.py +++ b/benchmark/Utils.py @@ -1,6 +1,5 @@ import os import sys -import json import subprocess PYTHON_VERSION = "{}.{}".format(sys.version_info.major, sys.version_info.minor) @@ -16,6 +15,7 @@ class Folders: img = "img" excel = "excel" sql = "sql" + current = os.getcwd() @staticmethod def src(): diff --git a/benchmark/scripts/app/app.py b/benchmark/scripts/app/app.py index 8b286cb..b0f384c 100755 --- a/benchmark/scripts/app/app.py +++ b/benchmark/scripts/app/app.py @@ -1,19 +1,20 @@ #!/usr/bin/env python from benchmark.Arguments import EnvData from flask import Flask -from .main import main +from .main import main, OUTPUT FRAMEWORK = "framework" FRAMEWORKS = "frameworks" TEST = "test" -def create_app(): +def create_app(output="local"): app = Flask(__name__) config = EnvData().load() app.register_blueprint(main) app.config[FRAMEWORK] = config[FRAMEWORK] app.config[FRAMEWORKS] = ["bootstrap", "bulma"] + app.config[OUTPUT] = output app.jinja_env.auto_reload = True app.config["TEMPLATES_AUTO_RELOAD"] = True return app diff --git a/benchmark/scripts/app/main.py b/benchmark/scripts/app/main.py index 999d28b..9e7a3e3 100755 --- a/benchmark/scripts/app/main.py +++ b/benchmark/scripts/app/main.py @@ -1,19 +1,21 @@ #!/usr/bin/env python import os import json +import shutil import xlsxwriter from benchmark.Utils import Files, Folders from benchmark.Arguments import EnvData from benchmark.ResultsBase import StubReport from benchmark.ResultsFiles import Excel, ReportDatasets from benchmark.Datasets import Datasets -from flask import Blueprint, current_app +from flask import Blueprint, current_app, send_file from flask import render_template, request, redirect, url_for main = Blueprint("main", __name__) FRAMEWORK = "framework" FRAMEWORKS = "frameworks" +OUTPUT = "output" TEST = "test" @@ -29,6 +31,7 @@ class AjaxResponse: { "success": self.success, "file": self.file_name, + "output": current_app.config[OUTPUT], } ), self.code, @@ -120,7 +123,9 @@ def show(): return showfile( file_name=selected_file, compare=compare, - back=url_for("main.index", compare=compare), + back=url_for( + "main.index", compare=compare, output=current_app.config[OUTPUT] + ), ) @@ -134,7 +139,8 @@ def excel(): report = ReportDatasets(excel=True, output=False) report.report() excel_name = os.path.join(Folders.excel, Files.datasets_report_excel) - Files.open(excel_name, test=current_app.config[TEST]) + if current_app.config[OUTPUT] == "local": + Files.open(excel_name, test=current_app.config[TEST]) return AjaxResponse(True, Files.datasets_report_excel).to_string() try: for file_name in selected_files: @@ -158,10 +164,21 @@ def excel(): ).to_string() if book is not None: book.close() - Files.open(file_excel, test=current_app.config[TEST]) + if current_app.config[OUTPUT] == "local": + Files.open(file_excel, test=current_app.config[TEST]) return AjaxResponse(True, Files.be_list_excel).to_string() +@main.route("/download/") +def download(file_name): + src = os.path.join(Folders.current, Folders.excel, file_name) + dest = os.path.join( + Folders.src(), "scripts", "app", "static", "excel", file_name + ) + shutil.copyfile(src, dest) + return send_file(dest, as_attachment=True) + + @main.route("/config//") def config(framework, compare): if framework not in current_app.config[FRAMEWORKS]: diff --git a/benchmark/scripts/app/static/excel/.gitignore b/benchmark/scripts/app/static/excel/.gitignore new file mode 100644 index 0000000..10326c2 --- /dev/null +++ b/benchmark/scripts/app/static/excel/.gitignore @@ -0,0 +1 @@ +*.xlsx \ No newline at end of file diff --git a/benchmark/scripts/app/static/js/excelFiles.js b/benchmark/scripts/app/static/js/excelFiles.js index d8e33fa..930b44c 100644 --- a/benchmark/scripts/app/static/js/excelFiles.js +++ b/benchmark/scripts/app/static/js/excelFiles.js @@ -12,7 +12,11 @@ function excelFiles(selectedFiles, compare) { dataType: 'json', success: function(data){ if (data.success) { - alert("Se ha generado el archivo "+data.file); + if (data.output == "local") { + alert("Se ha generado el archivo " + data.file); + } else { + window.open('/download/' + data.file, "_blank"); + } } else { alert(data.file); } diff --git a/benchmark/scripts/be_flask.py b/benchmark/scripts/be_flask.py index 5573241..071f8ed 100755 --- a/benchmark/scripts/be_flask.py +++ b/benchmark/scripts/be_flask.py @@ -1,11 +1,17 @@ #!/usr/bin/env python import webbrowser -from benchmark.scripts.app.app import create_app, TEST +from benchmark.Arguments import Arguments +from benchmark.scripts.app.app import create_app, TEST, OUTPUT # Launch a flask server to serve the results def main(args_test=None): + arguments = Arguments(prog="be_flask") + arguments.xset("output") + args = arguments.parse(args_test) app = create_app() app.config[TEST] = args_test is not None - webbrowser.open_new("http://127.0.0.1:1234/") - app.run(port=1234) + app.config[OUTPUT] = args.output + if args.output == "local": + webbrowser.open_new("http://127.0.0.1:1234/") + app.run(port=1234, host="0.0.0.0")