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 webbrowser
import xlsxwriter import xlsxwriter
from benchmark.Utils import Files, Folders from benchmark.Utils import Files, Folders
from benchmark.Arguments import Arguments, EnvData from benchmark.Arguments import EnvData
from benchmark.ResultsBase import StubReport from benchmark.ResultsBase import StubReport
from benchmark.ResultsFiles import Excel from benchmark.ResultsFiles import Excel
from flask import Flask from flask import Flask
@@ -15,13 +15,12 @@ from flask import render_template, request, redirect, url_for
app = Flask(__name__) app = Flask(__name__)
FRAMEWORK = "framework" FRAMEWORK = "framework"
FRAMEWORKS = "frameworks" FRAMEWORKS = "frameworks"
COMPARE = "compare"
TEST = "test" TEST = "test"
def process_data(file_name, data): def process_data(file_name, compare, data):
report = StubReport( report = StubReport(
os.path.join(Folders.results, file_name), compare=app.config[COMPARE] os.path.join(Folders.results, file_name), compare=compare
) )
new_list = [] new_list = []
for result in data["results"]: for result in data["results"]:
@@ -36,9 +35,9 @@ def process_data(file_name, data):
return summary return summary
@app.route("/index") @app.route("/index/<compare>")
@app.route("/") @app.route("/")
def index(): def index(compare="False"):
# Get a list of files in a directory # Get a list of files in a directory
files = {} files = {}
names = Files.get_all_results(hidden=False) names = Files.get_all_results(hidden=False)
@@ -57,33 +56,34 @@ def index():
files=files, files=files,
candidate=candidate[0], candidate=candidate[0],
framework=app.config[FRAMEWORK], framework=app.config[FRAMEWORK],
compare=compare.capitalize() == "True",
) )
@app.route("/show", methods=["post"]) @app.route("/show", methods=["post"])
def show(): def show():
selected_file = request.form["selected-file"] selected_file = request.form["selected-file"]
compare = request.form["compare"] == "true"
with open(os.path.join(Folders.results, selected_file)) as f: with open(os.path.join(Folders.results, selected_file)) as f:
data = json.load(f) data = json.load(f)
try: try:
summary = process_data(selected_file, data) summary = process_data(selected_file, compare, data)
except Exception as e: 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( return render_template(
"report.html", "report.html",
data=data, data=data,
file=selected_file, file=selected_file,
summary=summary, summary=summary,
framework=app.config[FRAMEWORK], framework=app.config[FRAMEWORK],
compare=compare,
) )
@app.route("/excel", methods=["post"]) @app.route("/excel", methods=["post"])
def excel(): def excel():
if request.is_json: selected_files = request.json["selected-files"]
selected_files = request.json compare = request.json["compare"] == "true"
else:
selected_files = request.form["selected-files"]
book = None book = None
try: try:
for file_name in selected_files: for file_name in selected_files:
@@ -96,7 +96,7 @@ def excel():
excel = Excel( excel = Excel(
file_name=file_name_result, file_name=file_name_result,
book=book, book=book,
compare=app.config[COMPARE], compare=compare,
) )
excel.report() excel.report()
except Exception as e: except Exception as e:
@@ -122,8 +122,8 @@ def excel():
) )
@app.route("/config/<framework>") @app.route("/config/<framework>/<compare>")
def config(framework): def config(framework, compare):
if not framework in app.config[FRAMEWORKS]: if not framework in app.config[FRAMEWORKS]:
message = f"framework {framework} not supported" message = f"framework {framework} not supported"
return render_template("error.html", message=message) return render_template("error.html", message=message)
@@ -132,20 +132,13 @@ def config(framework):
env.args[FRAMEWORK] = framework env.args[FRAMEWORK] = framework
env.save() env.save()
app.config[FRAMEWORK] = framework app.config[FRAMEWORK] = framework
return redirect(url_for("index")) return redirect(url_for("index", compare=compare))
def main(args_test=None): def main(args_test=None):
arguments = Arguments(prog="be_flask")
arguments.xset("compare")
args = arguments.parse(args_test)
config = EnvData().load() config = EnvData().load()
app.config[FRAMEWORK] = config[FRAMEWORK] app.config[FRAMEWORK] = config[FRAMEWORK]
app.config[COMPARE] = args.compare
app.config[FRAMEWORKS] = ["bootstrap", "bulma"] app.config[FRAMEWORKS] = ["bootstrap", "bulma"]
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/") webbrowser.open_new("http://127.0.0.1:1234/")
app.run(port=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"> <div class="alert alert-danger my-5" role="alert">
<h4 class="alert-heading"><button class="btn-close btn-sm" type="button" <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> <p>There was an error processing action, {{ message }}. Please try again later.</p>
<hr> <hr>
<p class="mb-0">If the problem persists, please contact support.</p> <p class="mb-0">If the problem persists, please contact support.</p>

View File

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

View File

@@ -2,7 +2,7 @@
<section class="section"> <section class="section">
<div class="container-fluid"> <div class="container-fluid">
<div class="p-4 bg-primary text-white"> <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> <h1>{{ data.title }}</h1>
</div> </div>
<div> <div>
@@ -77,7 +77,7 @@
</table> </table>
</div> </div>
{% endif %} {% 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> <h7><b>
Total score: {{ "%.6f" % (data.results | sum(attribute="score")) }} Total score: {{ "%.6f" % (data.results | sum(attribute="score")) }}
</b></h7> </b></h7>

View File

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

View File

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