add blueprint to app

This commit is contained in:
2023-05-31 17:21:35 +02:00
parent 54d141e861
commit 7d5f3058c3
2 changed files with 16 additions and 179 deletions

View File

@@ -1,174 +1,17 @@
#!/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
from flask import Flask
from .main import main
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/<compare>")
@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/<compare>")
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/<file_name>/<compare>")
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/<framework>/<compare>")
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():
app = Flask(__name__)
config = EnvData().load()
app.register_blueprint(main)
app.config[FRAMEWORK] = config[FRAMEWORK]
app.config[FRAMEWORKS] = ["bootstrap", "bulma"]
return app

View File

@@ -7,7 +7,7 @@ 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 Blueprint, current_app
from flask import render_template, request, redirect, url_for
@@ -67,13 +67,13 @@ def index(compare="False"):
"score": report.score,
"title": report.title,
}
candidate = app.config[FRAMEWORKS].copy()
candidate.remove(app.config[FRAMEWORK])
candidate = current_app.config[FRAMEWORKS].copy()
candidate.remove(current_app.config[FRAMEWORK])
return render_template(
"select.html",
files=files,
candidate=candidate[0],
framework=app.config[FRAMEWORK],
framework=current_app.config[FRAMEWORK],
compare=compare.capitalize() == "True",
)
@@ -88,12 +88,12 @@ def datasets(compare):
"datasets.html",
datasets=datos,
compare=compare,
framework=app.config[FRAMEWORK],
framework=current_app.config[FRAMEWORK],
)
@main.route("/showfile/<file_name>/<compare>")
def showfile(file_name, compare):
compare = compare.capitalize() == "True"
compare = compare.capitalize() == "True"
with open(os.path.join(Folders.results, file_name)) as f:
data = json.load(f)
try:
@@ -105,7 +105,7 @@ def showfile(file_name, compare):
data=data,
file=file_name,
summary=summary,
framework=app.config[FRAMEWORK],
framework=current_app.config[FRAMEWORK],
compare=compare,
)
@@ -127,7 +127,7 @@ 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=app.config[TEST])
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:
@@ -151,25 +151,19 @@ def excel():
).to_string()
if book is not None:
book.close()
Files.open(file_excel, test=app.config[TEST])
Files.open(file_excel, test=current_app.config[TEST])
return AjaxResponse(True, Files.be_list_excel).to_string()
@main.route("/config/<framework>/<compare>")
def config(framework, compare):
if not framework in app.config[FRAMEWORKS]:
if not framework 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()
app.config[FRAMEWORK] = framework
return redirect(url_for("index", compare=compare))
current_app.config[FRAMEWORK] = framework
return redirect(url_for("main.index", compare=compare))
def create_app():
config = EnvData().load()
app.config[FRAMEWORK] = config[FRAMEWORK]
app.config[FRAMEWORKS] = ["bootstrap", "bulma"]
return app