Add persistence of checkbox compare on app

This commit is contained in:
2023-05-30 00:08:36 +02:00
parent 10c352fdb5
commit 40af738ed9
6 changed files with 30 additions and 34 deletions

View File

@@ -4,7 +4,7 @@ import json
import webbrowser
import xlsxwriter
from benchmark.Utils import Files, Folders
from benchmark.Arguments import Arguments, EnvData
from benchmark.Arguments import EnvData
from benchmark.ResultsBase import StubReport
from benchmark.ResultsFiles import Excel
from flask import Flask
@@ -15,13 +15,12 @@ from flask import render_template, request, redirect, url_for
app = Flask(__name__)
FRAMEWORK = "framework"
FRAMEWORKS = "frameworks"
COMPARE = "compare"
TEST = "test"
def process_data(file_name, data):
def process_data(file_name, compare, data):
report = StubReport(
os.path.join(Folders.results, file_name), compare=app.config[COMPARE]
os.path.join(Folders.results, file_name), compare=compare
)
new_list = []
for result in data["results"]:
@@ -36,9 +35,9 @@ def process_data(file_name, data):
return summary
@app.route("/index")
@app.route("/index/<compare>")
@app.route("/")
def index():
def index(compare="False"):
# Get a list of files in a directory
files = {}
names = Files.get_all_results(hidden=False)
@@ -57,33 +56,34 @@ def index():
files=files,
candidate=candidate[0],
framework=app.config[FRAMEWORK],
compare=compare.capitalize() == "True",
)
@app.route("/show", methods=["post"])
def show():
selected_file = request.form["selected-file"]
compare = request.form["compare"] == "true"
with open(os.path.join(Folders.results, selected_file)) as f:
data = json.load(f)
try:
summary = process_data(selected_file, data)
summary = process_data(selected_file, compare, data)
except Exception as e:
return render_template("error.html", message=str(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():
if request.is_json:
selected_files = request.json
else:
selected_files = request.form["selected-files"]
selected_files = request.json["selected-files"]
compare = request.json["compare"] == "true"
book = None
try:
for file_name in selected_files:
@@ -96,7 +96,7 @@ def excel():
excel = Excel(
file_name=file_name_result,
book=book,
compare=app.config[COMPARE],
compare=compare,
)
excel.report()
except Exception as e:
@@ -122,8 +122,8 @@ def excel():
)
@app.route("/config/<framework>")
def config(framework):
@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)
@@ -132,20 +132,13 @@ def config(framework):
env.args[FRAMEWORK] = framework
env.save()
app.config[FRAMEWORK] = framework
return redirect(url_for("index"))
return redirect(url_for("index", compare=compare))
def main(args_test=None):
arguments = Arguments(prog="be_flask")
arguments.xset("compare")
args = arguments.parse(args_test)
config = EnvData().load()
app.config[FRAMEWORK] = config[FRAMEWORK]
app.config[COMPARE] = args.compare
app.config[FRAMEWORKS] = ["bootstrap", "bulma"]
app.config[TEST] = args_test is not None
webbrowser.open_new("http://127.0.0.1:1234/")
app.run(port=1234)
# Poner checkboxes para seleccionar resultados y poner un botón abajo para hacer un excel con los seleccionados
# Calcular símbolo igual que en list, o bien si ha puesto el parámetro de compare, con best o con zeror

View File

@@ -10,7 +10,7 @@
<div class="alert alert-danger my-5" role="alert">
<h4 class="alert-heading"><button class="btn-close btn-sm" type="button"
onclick="location.href='/index';"></button>Error</h4>
onclick="location.href='/index/{{ compare }}';"></button>Error</h4>
<p>There was an error processing action, {{ message }}. Please try again later.</p>
<hr>
<p class="mb-0">If the problem persists, please contact support.</p>

View File

@@ -37,6 +37,7 @@ $(document).ready(function () {
function showFile(selectedFile) {
var form = $('<form action="/show" method="post">' +
'<input type="hidden" name="selected-file" value="' + selectedFile + '" />' +
'<input type="hidden" name="compare" value='+$("#compare").is(':checked')+' />' +
'</form>');
$('body').append(form);
form.submit();
@@ -78,4 +79,7 @@ function setCheckBoxes(value) {
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked=value;
}
}
function redirectIndex(candidate){
location.href="/config/"+ candidate + "/" + $("#compare").is(':checked');
}

View File

@@ -2,7 +2,7 @@
<section class="section">
<div class="container-fluid">
<div class="p-4 bg-primary text-white">
<button type="button" class="btn-close" aria-label="Close" onclick="location.href='/index';"></button>
<button type="button" class="btn-close" aria-label="Close" onclick="location.href = '/index/{{ compare }}'"></button>
<h1>{{ data.title }}</h1>
</div>
<div>
@@ -77,7 +77,7 @@
</table>
</div>
{% endif %}
<button type="button" class="btn-close" aria-label="Close" onclick="location.href='/index';"></button>
<button type="button" class="btn-close" aria-label="Close" onclick="location.href = '/index/{{ compare }}'"></button>
<h7><b>
Total score: {{ "%.6f" % (data.results | sum(attribute="score")) }}
</b></h7>

View File

@@ -3,7 +3,7 @@
<div class="container is-fluid">
<div class="hero is-info is-bold">
<div class="hero-body">
<button class="delete is-large" onclick="location.href='/index';"></button>
<button class="delete is-large" onclick="location.href = '/index/{{ compare }}'"></button>
<h1 class="is-size-3">{{ data.title }}</h1>
</div>
</div>
@@ -84,7 +84,7 @@
</div>
{% endif %}
<h2 class="has-text-white has-background-primary"><b>
<button class="delete" onclick="location.href='/index';"></button>
<button class="delete" onclick="location.href = '/index/{{ compare }}'"></button>
Total score: {{ "%.6f" % (data.results | sum(attribute="score")) }}
</b></h2>
<h2>Number of files: {{ data.results | length }}</h2>

View File

@@ -2,15 +2,14 @@
<h1 class="{{ h1_class }}"><b>Benchmark Results</b></h1>
<div class="{{ level }}">
<div class="{{ frbutton_position }}">
<button class="{{ button_class }}" onclick="location.href='/config/{{ candidate }}'">Use {{ candidate
<button class="{{ button_class }}" onclick="redirectIndex('{{candidate}}')">Use {{ candidate
}}</button>
<button class="{{ button_class }}" onclick="excel()"><i class="mdi mdi-file-excel"></i> Excel</button>
</div>
{% if config.compare %}
<div class={{ frtag_position }}>
<span class="{{ tag_class }}">Comparing with best results</span>
</div>
{% endif %}
<div class={{ frtag_position }}>
<input type="checkbox" id="compare" name="compare" {% if compare %} {{ "checked" }} {% endif %}>
<span class="{{ tag_class }}">Comparing with best results</span>
</div>
</div>
{% include "partials/table_select.html" %}
</div>