From 18700dc5282f28b350f37902fcdbdd30e3226a20 Mon Sep 17 00:00:00 2001 From: Kento HASEGAWA Date: Thu, 16 Aug 2018 13:44:34 +0900 Subject: [PATCH] Add save method to save the best solution (#12) --- comm/server/main.py | 44 ++++++++++++++++++- comm/server/static/css/pynq-manager.css | 4 ++ comm/server/static/js/pynq-manager.js | 11 +++++ .../templates/part_question_status.html | 7 ++- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/comm/server/main.py b/comm/server/main.py index 177d6c5..4511d98 100644 --- a/comm/server/main.py +++ b/comm/server/main.py @@ -67,11 +67,11 @@ def load_questions(): # 既に回答されているものを読み込む answer_path = os.path.abspath(app_args["out"]) - answer_list = glob.glob("{}/T03_A*.txt".format(answer_path)) + answer_list = glob.glob("{}/submit/T01_A*.txt".format(answer_path)) for v in answer_list: _ans_name = os.path.basename(v) - m = re.search(r"T03_A([0-9A-Za-z]+)\.txt", _ans_name) + m = re.search(r"T01_A([0-9A-Za-z]+)\.txt", _ans_name) if m: _name = "NL_Q{}.txt".format(m.group(1)) with open(v, "r") as fp: @@ -102,6 +102,9 @@ def update_answer_list(qname): answer_log = json.load(fp) questions[qname]['answers'][json_name] = answer_log + if questions[qname]['answer'] == answer_log['answer']: + questions[qname]['best_json'] = json_name + @app.before_request def before_request(): global app_args @@ -283,6 +286,43 @@ def get_status(): return json.dumps(res) +@app.route("/save", methods=["POST"]) +def save_best_solution(): + """ + それぞれの問題に対し,解き終わった答えの中で最も品質の高い解を + submitフォルダに出力する. + """ + global questions + global app_args + + qname = request.form["qname"] + + _best_score = -1 + _best_json = "" + + for k, v in questions[qname]['answers'].items(): + if v['nlcheck'] > _best_score: + _best_socer = v['nlcheck'] + _best_json = k + + if _best_json != "": + out_path = "{}/{}".format(app_args['out'], "submit") + if not os.path.isdir(out_path): + os.makedirs(out_path) + + qnumber = qname.replace("NL_Q", "").replace(".txt", "") + out_file_path = "{}/T01_A{}.txt".format(out_path, qnumber) + with open(out_file_path, 'w') as fp: + fp.write(questions[qname]['answers'][_best_json]['answer']) + + questions[qname]['best_json'] = _best_json + + res = {"status": "OK"} + else: + res = {"status": "No answer is found"} + + return json.dumps(res) + @app.route("/board-data", methods=["POST"]) def get_board_data(): diff --git a/comm/server/static/css/pynq-manager.css b/comm/server/static/css/pynq-manager.css index 36d1611..72f5dc7 100644 --- a/comm/server/static/css/pynq-manager.css +++ b/comm/server/static/css/pynq-manager.css @@ -71,3 +71,7 @@ body{ background-color: rgba(200, 100, 100, .15); } +#client-control-pane tr.answer-detail-row.submit-answer{ + background-color: rgba(100, 200, 100, .3); +} + diff --git a/comm/server/static/js/pynq-manager.js b/comm/server/static/js/pynq-manager.js index 6c19ac8..a48aa8d 100644 --- a/comm/server/static/js/pynq-manager.js +++ b/comm/server/static/js/pynq-manager.js @@ -166,6 +166,17 @@ $(function(){ $("#client-control-pane").find(".stop-button").eq(0).click(function(){ pm.sendStop(); }); + $("#client-control-pane").find(".save-button").eq(0).click(function(){ + var qname = $(this).data("qname"); + $.ajax({ + type: "POST", + dataType: "json", + url: "/save", + data: {qname: qname} + }).done((data) => { + alert(data['status']); + }); + }); $(".answer-detail-row td").click(function(){ var json_name = $(this).parent("tr").data("json"); diff --git a/comm/server/templates/part_question_status.html b/comm/server/templates/part_question_status.html index 27b8df0..225e00e 100644 --- a/comm/server/templates/part_question_status.html +++ b/comm/server/templates/part_question_status.html @@ -3,7 +3,8 @@

【{{qname}}】

- +
+

@@ -40,7 +41,11 @@ Score {% for k, v in qdata.answers.items() %} + {% if qdata.best_json == k %} + + {% else %} + {% endif %} {{v.timestamp}} {{v.solver}} {{v.nlcheck}} -- 2.22.0