from flask import Flask, request, jsonify import pandas as pd import random app = Flask(__name__) # 加载学生名单,从Excel文件导入 def load_students(file_path): df = pd.read_excel(file_path) students = df.to_dict('records') return students # 保存学生名单,更新积分后导出到Excel def save_students(students, file_path): df = pd.DataFrame(students) df.to_excel(file_path, index=False) # 根据积分设置每个学生的被选概率,积分越高,权重越低 def get_student_weights(students): max_score = max([s['积分'] for s in students]) weights = [(max_score - s['积分'] + 1) for s in students] return weights # 随机点名 def pick_student(students): weights = get_student_weights(students) picked_student = random.choices(students, weights=weights, k=1)[0] return picked_student # 增加到达课堂的积分 def update_attendance_score(student): student['积分'] += 1 return student['积分'] # 处理回答问题的积分 def update_answer_score(student, repeated_question_correctly, answer_score): if repeated_question_correctly: student['积分'] += 0.5 else: student['积分'] -= 1 student['积分'] += answer_score return student['积分'] # 文件路径 file_path = 'students.xlsx' @app.route('/get_random_student', methods=['GET']) def get_random_student(): students = load_students(file_path) picked_student = pick_student(students) return jsonify({'姓名':picked_student['姓名'], '学号':picked_student['学号']}) @app.route('/update_attendance_score_route', methods=['POST']) def update_attendance_score_route(): data = request.json #确定学生到达课堂后 前端传递学号回来 student_id = data['学号'] students = load_students(file_path) for student in students: if student['学号'] == student_id: updated_score = update_attendance_score(student) break save_students(students, file_path) return jsonify({"学号": student_id, "积分": updated_score}) @app.route('/update_answer_score_route', methods=['POST']) def update_answer_score_route(): data = request.json student_id = data['学号'] repeated_correctly = data['repeated_correctly'] answer_score = data['answer_score'] students = load_students(file_path) for student in students: if student['学号'] == student_id: updated_score = update_answer_score(student, repeated_correctly, answer_score) break save_students(students, file_path) return jsonify({"学号": student_id, "积分": updated_score}) if __name__ == '__main__': app.run(debug=True)