From 54d141e861864dd4d0bb3b556196201e953753c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Wed, 31 May 2023 17:00:48 +0200 Subject: [PATCH] Create separate app --- benchmark/scripts/app/__init__.py | 0 benchmark/scripts/app/app.py | 174 +++++++++++++++++ benchmark/scripts/app/main.py | 175 ++++++++++++++++++ .../{ => app}/templates/base_bootstrap.html | 0 .../{ => app}/templates/base_bulma.html | 0 .../scripts/{ => app}/templates/datasets.html | 0 .../scripts/{ => app}/templates/error.html | 0 .../partials/cfg_select_bootstrap.jinja | 0 .../templates/partials/cfg_select_bulma.jinja | 0 .../templates/partials/datasets_table.html | 0 .../templates/partials/excelFiles.js | 0 .../{ => app}/templates/partials/report.js | 0 .../{ => app}/templates/partials/select.js | 0 .../templates/partials/table_report.html | 0 .../partials/table_report_bootstrap.html | 0 .../partials/table_report_bulma.html | 0 .../templates/partials/table_select.html | 0 .../partials/table_select_design.html | 1 + .../templates/partials/table_summary.html | 0 .../scripts/{ => app}/templates/report.html | 0 .../scripts/{ => app}/templates/select.html | 0 benchmark/scripts/be_flask.py | 167 +---------------- 22 files changed, 352 insertions(+), 165 deletions(-) create mode 100644 benchmark/scripts/app/__init__.py create mode 100755 benchmark/scripts/app/app.py create mode 100755 benchmark/scripts/app/main.py rename benchmark/scripts/{ => app}/templates/base_bootstrap.html (100%) rename benchmark/scripts/{ => app}/templates/base_bulma.html (100%) rename benchmark/scripts/{ => app}/templates/datasets.html (100%) rename benchmark/scripts/{ => app}/templates/error.html (100%) rename benchmark/scripts/{ => app}/templates/partials/cfg_select_bootstrap.jinja (100%) rename benchmark/scripts/{ => app}/templates/partials/cfg_select_bulma.jinja (100%) rename benchmark/scripts/{ => app}/templates/partials/datasets_table.html (100%) rename benchmark/scripts/{ => app}/templates/partials/excelFiles.js (100%) rename benchmark/scripts/{ => app}/templates/partials/report.js (100%) rename benchmark/scripts/{ => app}/templates/partials/select.js (100%) rename benchmark/scripts/{ => app}/templates/partials/table_report.html (100%) rename benchmark/scripts/{ => app}/templates/partials/table_report_bootstrap.html (100%) rename benchmark/scripts/{ => app}/templates/partials/table_report_bulma.html (100%) rename benchmark/scripts/{ => app}/templates/partials/table_select.html (100%) rename benchmark/scripts/{ => app}/templates/partials/table_select_design.html (87%) rename benchmark/scripts/{ => app}/templates/partials/table_summary.html (100%) rename benchmark/scripts/{ => app}/templates/report.html (100%) rename benchmark/scripts/{ => app}/templates/select.html (100%) diff --git a/benchmark/scripts/app/__init__.py b/benchmark/scripts/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/benchmark/scripts/app/app.py b/benchmark/scripts/app/app.py new file mode 100755 index 0000000..5d85890 --- /dev/null +++ b/benchmark/scripts/app/app.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python +import os +import json +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 Flask, render_template, request, redirect, url_for + + +app = Flask(__name__) +FRAMEWORK = "framework" +FRAMEWORKS = "frameworks" +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, + } + ), + 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 + + +@app.route("/index/") +@app.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 = app.config[FRAMEWORKS].copy() + candidate.remove(app.config[FRAMEWORK]) + return render_template( + "select.html", + files=files, + candidate=candidate[0], + framework=app.config[FRAMEWORK], + compare=compare.capitalize() == "True", + ) + + +@app.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=app.config[FRAMEWORK], + ) + +@app.route("/showfile//") +def showfile(file_name, compare): + compare = compare.capitalize() == "True" + 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=app.config[FRAMEWORK], + compare=compare, + ) + +@app.route("/show", methods=["post"]) +def show(): + selected_file = request.form["selected-file"] + compare = request.form["compare"] + return showfile(selected_file, compare) + + + +@app.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) + Files.open(excel_name, test=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() + Files.open(file_excel, test=app.config[TEST]) + return AjaxResponse(True, Files.be_list_excel).to_string() + + +@app.route("/config//") +def config(framework, compare): + if not framework in 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() + app.config[FRAMEWORK] = framework + return redirect(url_for("index", compare=compare)) + + +def create_app(): + config = EnvData().load() + app.config[FRAMEWORK] = config[FRAMEWORK] + app.config[FRAMEWORKS] = ["bootstrap", "bulma"] + return app diff --git a/benchmark/scripts/app/main.py b/benchmark/scripts/app/main.py new file mode 100755 index 0000000..8486cef --- /dev/null +++ b/benchmark/scripts/app/main.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python +import os +import json +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 +from flask import render_template, request, redirect, url_for + + +main = Blueprint("main", __name__) +FRAMEWORK = "framework" +FRAMEWORKS = "frameworks" +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, + } + ), + 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 + + +@main.route("/index/") +@main.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 = app.config[FRAMEWORKS].copy() + candidate.remove(app.config[FRAMEWORK]) + return render_template( + "select.html", + files=files, + candidate=candidate[0], + framework=app.config[FRAMEWORK], + compare=compare.capitalize() == "True", + ) + + +@main.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=app.config[FRAMEWORK], + ) + +@main.route("/showfile//") +def showfile(file_name, compare): + compare = compare.capitalize() == "True" + 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=app.config[FRAMEWORK], + compare=compare, + ) + +@main.route("/show", methods=["post"]) +def show(): + selected_file = request.form["selected-file"] + compare = request.form["compare"] + return showfile(selected_file, compare) + + + +@main.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) + Files.open(excel_name, test=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() + Files.open(file_excel, test=app.config[TEST]) + return AjaxResponse(True, Files.be_list_excel).to_string() + + +@main.route("/config//") +def config(framework, compare): + if not framework in 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() + app.config[FRAMEWORK] = framework + return redirect(url_for("index", compare=compare)) + + +def create_app(): + config = EnvData().load() + app.config[FRAMEWORK] = config[FRAMEWORK] + app.config[FRAMEWORKS] = ["bootstrap", "bulma"] + return app diff --git a/benchmark/scripts/templates/base_bootstrap.html b/benchmark/scripts/app/templates/base_bootstrap.html similarity index 100% rename from benchmark/scripts/templates/base_bootstrap.html rename to benchmark/scripts/app/templates/base_bootstrap.html diff --git a/benchmark/scripts/templates/base_bulma.html b/benchmark/scripts/app/templates/base_bulma.html similarity index 100% rename from benchmark/scripts/templates/base_bulma.html rename to benchmark/scripts/app/templates/base_bulma.html diff --git a/benchmark/scripts/templates/datasets.html b/benchmark/scripts/app/templates/datasets.html similarity index 100% rename from benchmark/scripts/templates/datasets.html rename to benchmark/scripts/app/templates/datasets.html diff --git a/benchmark/scripts/templates/error.html b/benchmark/scripts/app/templates/error.html similarity index 100% rename from benchmark/scripts/templates/error.html rename to benchmark/scripts/app/templates/error.html diff --git a/benchmark/scripts/templates/partials/cfg_select_bootstrap.jinja b/benchmark/scripts/app/templates/partials/cfg_select_bootstrap.jinja similarity index 100% rename from benchmark/scripts/templates/partials/cfg_select_bootstrap.jinja rename to benchmark/scripts/app/templates/partials/cfg_select_bootstrap.jinja diff --git a/benchmark/scripts/templates/partials/cfg_select_bulma.jinja b/benchmark/scripts/app/templates/partials/cfg_select_bulma.jinja similarity index 100% rename from benchmark/scripts/templates/partials/cfg_select_bulma.jinja rename to benchmark/scripts/app/templates/partials/cfg_select_bulma.jinja diff --git a/benchmark/scripts/templates/partials/datasets_table.html b/benchmark/scripts/app/templates/partials/datasets_table.html similarity index 100% rename from benchmark/scripts/templates/partials/datasets_table.html rename to benchmark/scripts/app/templates/partials/datasets_table.html diff --git a/benchmark/scripts/templates/partials/excelFiles.js b/benchmark/scripts/app/templates/partials/excelFiles.js similarity index 100% rename from benchmark/scripts/templates/partials/excelFiles.js rename to benchmark/scripts/app/templates/partials/excelFiles.js diff --git a/benchmark/scripts/templates/partials/report.js b/benchmark/scripts/app/templates/partials/report.js similarity index 100% rename from benchmark/scripts/templates/partials/report.js rename to benchmark/scripts/app/templates/partials/report.js diff --git a/benchmark/scripts/templates/partials/select.js b/benchmark/scripts/app/templates/partials/select.js similarity index 100% rename from benchmark/scripts/templates/partials/select.js rename to benchmark/scripts/app/templates/partials/select.js diff --git a/benchmark/scripts/templates/partials/table_report.html b/benchmark/scripts/app/templates/partials/table_report.html similarity index 100% rename from benchmark/scripts/templates/partials/table_report.html rename to benchmark/scripts/app/templates/partials/table_report.html diff --git a/benchmark/scripts/templates/partials/table_report_bootstrap.html b/benchmark/scripts/app/templates/partials/table_report_bootstrap.html similarity index 100% rename from benchmark/scripts/templates/partials/table_report_bootstrap.html rename to benchmark/scripts/app/templates/partials/table_report_bootstrap.html diff --git a/benchmark/scripts/templates/partials/table_report_bulma.html b/benchmark/scripts/app/templates/partials/table_report_bulma.html similarity index 100% rename from benchmark/scripts/templates/partials/table_report_bulma.html rename to benchmark/scripts/app/templates/partials/table_report_bulma.html diff --git a/benchmark/scripts/templates/partials/table_select.html b/benchmark/scripts/app/templates/partials/table_select.html similarity index 100% rename from benchmark/scripts/templates/partials/table_select.html rename to benchmark/scripts/app/templates/partials/table_select.html diff --git a/benchmark/scripts/templates/partials/table_select_design.html b/benchmark/scripts/app/templates/partials/table_select_design.html similarity index 87% rename from benchmark/scripts/templates/partials/table_select_design.html rename to benchmark/scripts/app/templates/partials/table_select_design.html index e598d74..c4714ed 100644 --- a/benchmark/scripts/templates/partials/table_select_design.html +++ b/benchmark/scripts/app/templates/partials/table_select_design.html @@ -5,6 +5,7 @@ {{ select.get_button("Use " ~ candidate, "redirectIndex('" ~ candidate ~ "')")|safe }} {{ select.get_button(select.icon("excel") ~ " Excel", "excel()")|safe }} {{ select.get_button(select.icon("database-eye") ~ " Datasets", "redirectDatasets()")|safe }} + {{ select.get_button(select.icon("star-circle-outline") ~ " Best results", "{{ url_for('best_results')}}")}}
diff --git a/benchmark/scripts/templates/partials/table_summary.html b/benchmark/scripts/app/templates/partials/table_summary.html similarity index 100% rename from benchmark/scripts/templates/partials/table_summary.html rename to benchmark/scripts/app/templates/partials/table_summary.html diff --git a/benchmark/scripts/templates/report.html b/benchmark/scripts/app/templates/report.html similarity index 100% rename from benchmark/scripts/templates/report.html rename to benchmark/scripts/app/templates/report.html diff --git a/benchmark/scripts/templates/select.html b/benchmark/scripts/app/templates/select.html similarity index 100% rename from benchmark/scripts/templates/select.html rename to benchmark/scripts/app/templates/select.html diff --git a/benchmark/scripts/be_flask.py b/benchmark/scripts/be_flask.py index 721c8fe..5573241 100755 --- a/benchmark/scripts/be_flask.py +++ b/benchmark/scripts/be_flask.py @@ -1,174 +1,11 @@ #!/usr/bin/env python -import os -import json import webbrowser -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 Flask -from flask import render_template, request, redirect, url_for - +from benchmark.scripts.app.app import create_app, TEST # Launch a flask server to serve the results -app = Flask(__name__) -FRAMEWORK = "framework" -FRAMEWORKS = "frameworks" -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, - } - ), - 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 - - -@app.route("/index/") -@app.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 = app.config[FRAMEWORKS].copy() - candidate.remove(app.config[FRAMEWORK]) - return render_template( - "select.html", - files=files, - candidate=candidate[0], - framework=app.config[FRAMEWORK], - compare=compare.capitalize() == "True", - ) - - -@app.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=app.config[FRAMEWORK], - ) - - -@app.route("/show", methods=["post"]) -def show(): - selected_file = request.form["selected-file"] - compare = request.form["compare"].capitalize() == "True" - with open(os.path.join(Folders.results, selected_file)) as f: - data = json.load(f) - try: - summary = process_data(selected_file, compare, data) - except Exception as e: - return render_template("error.html", message=str(e), compare=compare) - return render_template( - "report.html", - data=data, - file=selected_file, - summary=summary, - framework=app.config[FRAMEWORK], - compare=compare, - ) - - -@app.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) - Files.open(excel_name, test=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() - Files.open(file_excel, test=app.config[TEST]) - return AjaxResponse(True, Files.be_list_excel).to_string() - - -@app.route("/config//") -def config(framework, compare): - if not framework in 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() - app.config[FRAMEWORK] = framework - return redirect(url_for("index", compare=compare)) - def main(args_test=None): - config = EnvData().load() - app.config[FRAMEWORK] = config[FRAMEWORK] - app.config[FRAMEWORKS] = ["bootstrap", "bulma"] + app = create_app() app.config[TEST] = args_test is not None webbrowser.open_new("http://127.0.0.1:1234/") app.run(port=1234)