Add automatic download excel files

This commit is contained in:
2023-06-01 20:48:53 +02:00
parent 0b258595f9
commit 34b4cb6477
7 changed files with 50 additions and 11 deletions

View File

@@ -235,6 +235,16 @@ class Arguments(argparse.ArgumentParser):
"help": "number of folds", "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": [ "platform": [
("-P", "--platform"), ("-P", "--platform"),
{ {

View File

@@ -1,6 +1,5 @@
import os import os
import sys import sys
import json
import subprocess import subprocess
PYTHON_VERSION = "{}.{}".format(sys.version_info.major, sys.version_info.minor) PYTHON_VERSION = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
@@ -16,6 +15,7 @@ class Folders:
img = "img" img = "img"
excel = "excel" excel = "excel"
sql = "sql" sql = "sql"
current = os.getcwd()
@staticmethod @staticmethod
def src(): def src():

View File

@@ -1,19 +1,20 @@
#!/usr/bin/env python #!/usr/bin/env python
from benchmark.Arguments import EnvData from benchmark.Arguments import EnvData
from flask import Flask from flask import Flask
from .main import main from .main import main, OUTPUT
FRAMEWORK = "framework" FRAMEWORK = "framework"
FRAMEWORKS = "frameworks" FRAMEWORKS = "frameworks"
TEST = "test" TEST = "test"
def create_app(): def create_app(output="local"):
app = Flask(__name__) app = Flask(__name__)
config = EnvData().load() config = EnvData().load()
app.register_blueprint(main) app.register_blueprint(main)
app.config[FRAMEWORK] = config[FRAMEWORK] app.config[FRAMEWORK] = config[FRAMEWORK]
app.config[FRAMEWORKS] = ["bootstrap", "bulma"] app.config[FRAMEWORKS] = ["bootstrap", "bulma"]
app.config[OUTPUT] = output
app.jinja_env.auto_reload = True app.jinja_env.auto_reload = True
app.config["TEMPLATES_AUTO_RELOAD"] = True app.config["TEMPLATES_AUTO_RELOAD"] = True
return app return app

View File

@@ -1,19 +1,21 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import json import json
import shutil
import xlsxwriter import xlsxwriter
from benchmark.Utils import Files, Folders from benchmark.Utils import Files, Folders
from benchmark.Arguments import EnvData from benchmark.Arguments import EnvData
from benchmark.ResultsBase import StubReport from benchmark.ResultsBase import StubReport
from benchmark.ResultsFiles import Excel, ReportDatasets from benchmark.ResultsFiles import Excel, ReportDatasets
from benchmark.Datasets import Datasets 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 from flask import render_template, request, redirect, url_for
main = Blueprint("main", __name__) main = Blueprint("main", __name__)
FRAMEWORK = "framework" FRAMEWORK = "framework"
FRAMEWORKS = "frameworks" FRAMEWORKS = "frameworks"
OUTPUT = "output"
TEST = "test" TEST = "test"
@@ -29,6 +31,7 @@ class AjaxResponse:
{ {
"success": self.success, "success": self.success,
"file": self.file_name, "file": self.file_name,
"output": current_app.config[OUTPUT],
} }
), ),
self.code, self.code,
@@ -120,7 +123,9 @@ def show():
return showfile( return showfile(
file_name=selected_file, file_name=selected_file,
compare=compare, 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 = ReportDatasets(excel=True, output=False)
report.report() report.report()
excel_name = os.path.join(Folders.excel, Files.datasets_report_excel) 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() return AjaxResponse(True, Files.datasets_report_excel).to_string()
try: try:
for file_name in selected_files: for file_name in selected_files:
@@ -158,10 +164,21 @@ def excel():
).to_string() ).to_string()
if book is not None: if book is not None:
book.close() 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() return AjaxResponse(True, Files.be_list_excel).to_string()
@main.route("/download/<file_name>")
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/<framework>/<compare>") @main.route("/config/<framework>/<compare>")
def config(framework, compare): def config(framework, compare):
if framework not in current_app.config[FRAMEWORKS]: if framework not in current_app.config[FRAMEWORKS]:

View File

@@ -0,0 +1 @@
*.xlsx

View File

@@ -12,7 +12,11 @@ function excelFiles(selectedFiles, compare) {
dataType: 'json', dataType: 'json',
success: function(data){ success: function(data){
if (data.success) { 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 { } else {
alert(data.file); alert(data.file);
} }

View File

@@ -1,11 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
import webbrowser 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 # Launch a flask server to serve the results
def main(args_test=None): def main(args_test=None):
arguments = Arguments(prog="be_flask")
arguments.xset("output")
args = arguments.parse(args_test)
app = create_app() app = create_app()
app.config[TEST] = args_test is not None app.config[TEST] = args_test is not None
webbrowser.open_new("http://127.0.0.1:1234/") app.config[OUTPUT] = args.output
app.run(port=1234) if args.output == "local":
webbrowser.open_new("http://127.0.0.1:1234/")
app.run(port=1234, host="0.0.0.0")