from flask import Flask from flask import jsonify from flask import request import json import pymysql import sys import mysubmit import testcase import my_check_logic_error sys.path.append('../') from PIPE.main import PIPEModel app = Flask(__name__) app.config['JSON_AS_ASCII'] = False db = pymysql.connect(host='192.168.0.89', port=3306, user='root', password='2020yunjisuan!', db='shuishan', charset='utf8') @app.route('/get-problem-items/') def get_problem_items(): db.ping(reconnect=True) cursor = db.cursor() cursor.execute("select id, title from problem") data = cursor.fetchall() pids = [] for d in data: pids.append({"id": d[0], "title": d[1]}) pids = sorted(pids, key=lambda x: x.__getitem__('id'), reverse=False) for i in range(len(pids)): pids[i]['id'] = str(pids[i]['id']) return jsonify(pids), 200 @app.route('/get-problem-description/<_id>/') def get_problem_description(_id: str): db.ping(reconnect=True) cursor = db.cursor() cursor.execute("select title, description, input_description, output_description, samples " "from problem where id = %d" % int(_id)) des = cursor.fetchone() # print(des) title = '
%s. %s

' % (_id, des[0]) description = '
Description
%s

' % des[1] input_description = '
Input Description
%s

' \ % des[2] output_description = '
Output Description
%s

' \ % des[3] samples = json.loads(des[-1].replace("\n", "\\n")) samples_html = '' for s in samples: _input = s["input"].strip().replace(" ", "  ").replace("\n", "
    ") _output = s["output"].strip().replace(" ", "  ").replace("\n", "
    ") # _input = s["input"].strip().replace(" ", "  ") # _output = s["output"].strip().replace(" ", "  ") stra = '
' strb = '
' strc = '
Input
' strd = '
' stre = '
Output
' strf = '

' strg = '
' strh = '

    ' + _input + '
    
' stri = '
' strj = '

    ' + _output + '
    
' strk = '
' strl = '

' samples_html = samples_html + stra + strb + strc + strd + stre + strf + strg + strh + stri + strj + strk + strl return title + description + input_description + output_description + samples_html, 200 @app.route('/case_test/', methods=['POST']) def case_test_(): pid: str = request.json.get("pid") code: str = request.json.get("code") with open('wulin.txt', 'w', encoding='utf-8', newline='') as f: f.write(pid) f.close() results = testcase.submit_handler(code, pid, 'C') return results, 200 @app.route('/check-logic-error/', methods=['POST']) def check_logic_error(): pid: str = request.json.get("pid") code: str = request.json.get("code") # print(type(code)) # model = PIPEModel() # model.load_savedModel('./PIPE/training/C2AE_model/cp-0015.ckpt') # answer = model.predict_code(code=code) # del model # with open('wulin3.txt', 'w', encoding='utf-8', newline='') as f: # 设置文件对象 # f.write(answer) # 将字符串写入文件中 # f.close() answer = my_check_logic_error.check_logic_error() # results = 'check_logic_error' return answer, 200 @app.route('/submit/', methods=['POST']) def submit(): pid: str = request.json.get("pid") code: str = request.json.get("code") with open('wulin2.txt', 'w', encoding='utf-8', newline='') as f: f.write(pid) f.close() results = mysubmit.submit_handler(code, pid, 'C') # results = check_logic_error(pid, code) return results, 200 if __name__ == '__main__': app.run()