diff --git a/benchmark/scripts/be_flask.py b/benchmark/scripts/be_flask.py new file mode 100755 index 0000000..69e17af --- /dev/null +++ b/benchmark/scripts/be_flask.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import webbrowser +from benchmark.scripts.flask_app.app import create_app + + +# Launch a flask server to serve the results +def main(args_test=None): + app = create_app() + app.config["TEST"] = args_test is not None + output = app.config["OUTPUT"] + print("Output is ", output) + if output == "local": + webbrowser.open_new("http://127.0.0.1:1234/") + app.run(port=1234, host="0.0.0.0") diff --git a/benchmark/scripts/flask_app/.env b/benchmark/scripts/flask_app/.env new file mode 100644 index 0000000..13f5acd --- /dev/null +++ b/benchmark/scripts/flask_app/.env @@ -0,0 +1,2 @@ +OUTPUT="local" +FRAMEWORK="bulma" \ No newline at end of file diff --git a/benchmark/scripts/flask_app/__init__.py b/benchmark/scripts/flask_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/benchmark/scripts/flask_app/app.py b/benchmark/scripts/flask_app/app.py new file mode 100644 index 0000000..11b6796 --- /dev/null +++ b/benchmark/scripts/flask_app/app.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +from flask import Flask +from .config import Config + +# from .results import results +from .main import main + + +def create_app(): + app = Flask(__name__) + app.register_blueprint(main) + # app.register_blueprint(results) + app.config.from_object(Config) + app.jinja_env.auto_reload = True + return app diff --git a/benchmark/scripts/flask_app/config.py b/benchmark/scripts/flask_app/config.py new file mode 100644 index 0000000..ca4092b --- /dev/null +++ b/benchmark/scripts/flask_app/config.py @@ -0,0 +1,12 @@ +import os +from dotenv import load_dotenv + +basedir = os.path.abspath(os.path.dirname(__file__)) +load_dotenv(os.path.join(basedir, ".env")) + + +class Config(object): + FRAMEWORKS = ["bootstrap", "bulma"] + FRAMEWORK = os.environ.get("FRAMEWORK") or FRAMEWORKS[0] + OUTPUT = os.environ.get("OUTPUT") or "local" # local or docker + TEMPLATES_AUTO_RELOAD = True diff --git a/benchmark/scripts/flask_app/main.py b/benchmark/scripts/flask_app/main.py new file mode 100644 index 0000000..d3e41a9 --- /dev/null +++ b/benchmark/scripts/flask_app/main.py @@ -0,0 +1,33 @@ +import os +from flask import Blueprint, current_app, render_template, url_for +from benchmark.Utils import Files, Folders +from benchmark.ResultsBase import StubReport + +main = Blueprint("main", __name__) + + +@main.route("/") +@main.route("/index") +def index(): + # Get a list of files in a directory + files = {} + compare = False + # names = Files.get_all_results(hidden=False) + # for name in names: + # report = StubReport(os.path.join(Folders.results, name)) + # report.report() + # files[name] = { + # "duration": report.duration, + # "score": report.score, + # "title": report.title, + # } + # candidate = current_app.config["FRAMEWORKS"].copy() + # candidate.remove(current_app.config["FRAMEWORK"]) + # return render_template( + # "select.html", + # files=files, + # candidate=candidate[0], + # framework=current_app.config["FRAMEWORK"], + # compare=compare.capitalize() == "True", + # ) + return render_template("index.html") diff --git a/benchmark/scripts/flask_app/results.py b/benchmark/scripts/flask_app/results.py new file mode 100644 index 0000000..bf83600 --- /dev/null +++ b/benchmark/scripts/flask_app/results.py @@ -0,0 +1,208 @@ +#!/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, send_file +from flask import render_template, request, redirect, url_for + + +main = Blueprint("main", __name__) +FRAMEWORK = "framework" +FRAMEWORKS = "frameworks" +OUTPUT = "output" +TEST = "test" + + +class AjaxResponse: + def __init__(self, success, file_name, code=200): + self.success = success + self.file_name = file_name + self.code = code + + def to_string(self): + return ( + json.dumps( + { + "success": self.success, + "file": self.file_name, + "output": current_app.config[OUTPUT], + } + ), + self.code, + {"ContentType": "application/json"}, + ) + + +def process_data(file_name, compare, data): + report = StubReport( + os.path.join(Folders.results, file_name), compare=compare + ) + new_list = [] + for result in data["results"]: + symbol = report._compute_status(result["dataset"], result["score"]) + result["symbol"] = symbol if symbol != " " else " " + new_list.append(result) + data["results"] = new_list + # Compute summary with explanation of symbols + summary = {} + for key, value in report._compare_totals.items(): + summary[key] = (report._status_meaning(key), value) + return summary + + +@results.route("/index/") +@results.route("/") +def index(compare="False"): + # Get a list of files in a directory + files = {} + names = Files.get_all_results(hidden=False) + for name in names: + report = StubReport(os.path.join(Folders.results, name)) + report.report() + files[name] = { + "duration": report.duration, + "score": report.score, + "title": report.title, + } + candidate = current_app.config[FRAMEWORKS].copy() + candidate.remove(current_app.config[FRAMEWORK]) + return render_template( + "select.html", + files=files, + candidate=candidate[0], + framework=current_app.config[FRAMEWORK], + compare=compare.capitalize() == "True", + ) + + +@results.route("/datasets/") +def datasets(compare): + dt = Datasets() + datos = [] + for dataset in dt: + datos.append(dt.get_attributes(dataset)) + return render_template( + "datasets.html", + datasets=datos, + compare=compare, + framework=current_app.config[FRAMEWORK], + ) + + +@results.route("/showfile//") +def showfile(file_name, compare, back=None): + compare = compare.capitalize() == "True" + back = request.args["url"] if back is None else back + print(f"back [{back}]") + with open(os.path.join(Folders.results, file_name)) as f: + data = json.load(f) + try: + summary = process_data(file_name, compare, data) + except Exception as e: + return render_template("error.html", message=str(e), compare=compare) + return render_template( + "report.html", + data=data, + file=file_name, + summary=summary, + framework=current_app.config[FRAMEWORK], + back=back, + ) + + +@results.route("/show", methods=["post"]) +def show(): + selected_file = request.form["selected-file"] + compare = request.form["compare"] + return showfile( + file_name=selected_file, + compare=compare, + back=url_for( + "main.index", compare=compare, output=current_app.config[OUTPUT] + ), + ) + + +@results.route("/excel", methods=["post"]) +def excel(): + selected_files = request.json["selectedFiles"] + compare = request.json["compare"] + book = None + if selected_files[0] == "datasets": + # Create a list of datasets + report = ReportDatasets(excel=True, output=False) + report.report() + excel_name = os.path.join(Folders.excel, Files.datasets_report_excel) + 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: + file_name_result = os.path.join(Folders.results, file_name) + if book is None: + file_excel = os.path.join(Folders.excel, Files.be_list_excel) + book = xlsxwriter.Workbook( + file_excel, {"nan_inf_to_errors": True} + ) + excel = Excel( + file_name=file_name_result, + book=book, + compare=compare, + ) + excel.report() + except Exception as e: + if book is not None: + book.close() + return AjaxResponse( + False, "Could not create excel file, " + str(e) + ).to_string() + if book is not None: + book.close() + if current_app.config[OUTPUT] == "local": + Files.open(file_excel, test=current_app.config[TEST]) + return AjaxResponse(True, Files.be_list_excel).to_string() + + +@results.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) + + +@results.route("/config//") +def config(framework, compare): + if framework not in current_app.config[FRAMEWORKS]: + message = f"framework {framework} not supported" + return render_template("error.html", message=message) + env = EnvData() + env.load() + env.args[FRAMEWORK] = framework + env.save() + current_app.config[FRAMEWORK] = framework + return redirect(url_for("main.index", compare=compare)) + + +@results.route("/best_results//") +def best_results(file, compare): + compare = compare.capitalize() == "True" + try: + with open(os.path.join(Folders.results, file)) as f: + data = json.load(f) + except Exception as e: + return render_template("error.html", message=str(e), compare=compare) + return render_template( + "report_best.html", + data=data, + compare=compare, + framework=current_app.config[FRAMEWORK], + ) diff --git a/benchmark/scripts/flask_app/static/css/main.css b/benchmark/scripts/flask_app/static/css/main.css new file mode 100644 index 0000000..e7f649c --- /dev/null +++ b/benchmark/scripts/flask_app/static/css/main.css @@ -0,0 +1,38 @@ +.alternate-font { + font-family: Arial; +} + +tbody { + font-family: Courier; +} + +.tag { + cursor: pointer; +} + +.ajaxLoading { + cursor: progress !important; +} + +#file-table tbody tr.selected td { + background-color: #0dcaf0; + color: white; +} + +#report-table tbody tr.selected td { + background-color: #0dcaf0; + color: white; +} + +.btn-small { + padding: 0.25rem 0.5rem; + font-size: 0.75rem; +} + +body { + padding-bottom: 20px; +} + +.navbar { + margin-bottom: 20px; +} \ No newline at end of file diff --git a/benchmark/scripts/flask_app/static/js/excelFiles.js b/benchmark/scripts/flask_app/static/js/excelFiles.js new file mode 100644 index 0000000..930b44c --- /dev/null +++ b/benchmark/scripts/flask_app/static/js/excelFiles.js @@ -0,0 +1,29 @@ +function excelFiles(selectedFiles, compare) { + var data = { + "selectedFiles": selectedFiles, + "compare": compare + }; + // send data to server with ajax post + $.ajax({ + type:'POST', + url:'/excel', + data: JSON.stringify(data), + contentType: "application/json", + dataType: 'json', + success: function(data){ + if (data.success) { + if (data.output == "local") { + alert("Se ha generado el archivo " + data.file); + } else { + window.open('/download/' + data.file, "_blank"); + } + } else { + alert(data.file); + } + }, + error: function (xhr, ajaxOptions, thrownError) { + var mensaje = JSON.parse(xhr.responseText || '{\"mensaje\": \"Error indeterminado\"}'); + alert(mensaje.mensaje); + } + }); +} \ No newline at end of file diff --git a/benchmark/scripts/flask_app/templates/base.html b/benchmark/scripts/flask_app/templates/base.html new file mode 100644 index 0000000..b15f92b --- /dev/null +++ b/benchmark/scripts/flask_app/templates/base.html @@ -0,0 +1,58 @@ +{% extends 'base_bulma.html' %} +{% block title %} + {% if title %} + {{ title }} - benchmark + {% else %} + Benchmark + {% endif %} +{% endblock %} +{% block navbar %} +
+
+ +
+ {% endblock %} + {% block content %} +
+ {% with messages = get_flashed_messages() %} + {% if messages %} + {% for message in messages %}{% endfor %} + {% endif %} + {% endwith %} + {# application content needs to be provided in the app_content block #} + {% block app_content %}{% endblock %} +
+ {% endblock %} diff --git a/benchmark/scripts/flask_app/templates/base_bootstrap.html b/benchmark/scripts/flask_app/templates/base_bootstrap.html new file mode 100644 index 0000000..033f81d --- /dev/null +++ b/benchmark/scripts/flask_app/templates/base_bootstrap.html @@ -0,0 +1,49 @@ +{% block doc -%} + + + {%- block html %} + + {%- block head %} + + {% block title %} + {{ title|default }} + {% endblock title %} + + {%- block metas %} + + {%- endblock metas %} + {%- block styles %} + + + + + + {%- endblock styles %} + {%- endblock head %} + + + {% block body -%} + {% block navbar %} + {%- endblock navbar %} + {% block content -%} + {%- endblock content %} + {% block scripts %} + + + {%- endblock scripts %} + {%- endblock body %} + + {%- endblock html %} + +{% endblock doc -%} diff --git a/benchmark/scripts/flask_app/templates/base_bulma.html b/benchmark/scripts/flask_app/templates/base_bulma.html new file mode 100644 index 0000000..2c9fd97 --- /dev/null +++ b/benchmark/scripts/flask_app/templates/base_bulma.html @@ -0,0 +1,27 @@ + + + + + {% block title %} + {{ title|default }} + {% endblock title %} + + + + + + + + {% block navbar %}{% endblock %} + {% block content %}{% endblock %} + + + {% block jscript %}{% endblock %} + diff --git a/benchmark/scripts/flask_app/templates/index.html b/benchmark/scripts/flask_app/templates/index.html new file mode 100644 index 0000000..2d156ec --- /dev/null +++ b/benchmark/scripts/flask_app/templates/index.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block content %} +

My First Heading

+

My first paragraph.

+{% endblock %} diff --git a/requirements.txt b/requirements.txt index 32065e2..373795f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ pandas scikit-learn scipy +python-dotenv +flask odte cython fimdlp