From f4c1466cb7f8a9c64f39037bec6b830726bbc01a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 19 May 2026 06:38:22 +0800 Subject: [PATCH] =?utf8?q?update:=20=E6=B7=BB=E5=8A=A0=E7=A7=91=E7=9B=AE?= =?utf8?q?=E9=A6=96=E9=A1=B5=E5=88=86=E6=9E=90=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app.py | 94 +++++++----- templates/subject/index.html | 282 +++++++++++++---------------------- 2 files changed, 155 insertions(+), 221 deletions(-) diff --git a/app.py b/app.py index e9f9dac..00ce655 100644 --- a/app.py +++ b/app.py @@ -1,55 +1,35 @@ -from flask import Flask, render_template -import os -from database import query_db, execute_db +from flask import Flask, render_template, request, jsonify +from database import query_db app = Flask(__name__) -# 配置:确保 Flask 能正确找到模板和静态文件 -app.config['SECRET_KEY'] = 'studybase_secret_key_2026' - # 1. 首页 @app.route('/') def index(): return render_template('index.html') -# 2. 科目面板 +# 2. 科目面板 (注意:路由加了斜杠,匹配你的访问日志) @app.route('/subject/') -def subject(): - # 这里的 SQL 逻辑: - # 1. 从 Subject 表查询所有科目 - # 2. 关联 questions 表统计每个科目的题目数量 - # 3. 过滤 enabled=1 的科目 - query = """ - SELECT - s.ID, - s.Subject as name, - s.CreateTime as updated_at, - (SELECT COUNT(*) FROM questions q WHERE q.subject_id = s.ID) as q_count - FROM Subject s - WHERE s.enabled = 1 - ORDER BY s.ID ASC - """ +def subject_portal(): try: - subjects = query_db(query) + subjects = query_db("SELECT ID, Subject as name FROM Subject WHERE enabled = 1") + stages = query_db("SELECT ID, stage_name FROM stage WHERE enabled = 1 ORDER BY ID") + default_subject_id = subjects[0]['ID'] if subjects else None + return render_template('subject/index.html', + subjects=subjects, + stages=stages, + default_id=default_subject_id) except Exception as e: - print(f"数据库查询出错: {e}") - subjects = [] - - return render_template('subject/index.html', subjects=subjects) + return f"数据库连接失败: {e}" # 3. 题目管理 @app.route('/questions/') def questions(): - # 示例:读取最近的10道题目 - # questions = query_db('SELECT * FROM questions LIMIT 10') - # return render_template('questions/index.html', questions=questions) return render_template('questions/index.html') # 4. 试卷管理 @app.route('/papers/') def papers(): - # 示例:读取所有试卷列表 - # papers = query_db('SELECT * FROM papers') return render_template('papers/index.html') # 5. 系统设定 @@ -57,12 +37,48 @@ def papers(): def settings(): return render_template('settings/index.html') -# 错误处理:404 页面返回首页 -@app.errorhandler(404) -def page_not_found(e): - return render_template('index.html'), 404 +# 6. API 接口 +@app.route('/api/subject_stats') +def get_subject_stats(): + sub_id = request.args.get('subject_id') + stage_ids = request.args.get('stage_ids', '').split(',') + + if not sub_id or not stage_ids or stage_ids == ['']: + return jsonify({"error": "未选中科目或阶段"}), 400 + + # 构造阶段过滤 SQL + stage_placeholders = ','.join(['?'] * len(stage_ids)) + + def get_count(q_type, extra_condition=""): + sql = f""" + SELECT COUNT(*) as count FROM questions + WHERE question_type = ? AND subject_id = ? + AND stage_id IN ({stage_placeholders}) {extra_condition} + """ + params = [q_type, sub_id] + stage_ids + res = query_db(sql, params, one=True) + return res['count'] if res else 0 + + w_total = get_count('错题') + w_fixed = get_count('错题', 'AND right_times > 0') + p_total = get_count('推题') + p_fixed = get_count('推题', 'AND right_times > 0') + + return jsonify({ + "wrong": { + "total": w_total, + "fixed": w_fixed, + "ratio": f"{(w_fixed/w_total*100):.1f}%" if w_total > 0 else "0%", + "remained": w_total - w_fixed + }, + "push": { + "total": p_total, + "fixed": p_fixed, + "ratio": f"{(p_fixed/p_total*100):.1f}%" if p_total > 0 else "0%", + "remained": p_total - p_fixed + } + }) if __name__ == '__main__': - # debug=True 允许热更新,修改代码后保存即可生效 - # host='0.0.0.0' 允许通过公网 IP 或局域网访问 - app.run(host='0.0.0.0', port=5000, debug=True) + # 开启 debug=True,这样报错会有详细提示 + app.run(host='0.0.0.0', port=5000, debug=True, threaded=True) diff --git a/templates/subject/index.html b/templates/subject/index.html index 0a53198..0b4c63a 100644 --- a/templates/subject/index.html +++ b/templates/subject/index.html @@ -2,201 +2,119 @@ - - 科目面板 - 错题管理系统 + + 科目概要面板 - - - - -
- -
-
- {{ subjects|length if subjects else 0 }} - 活跃科目 + + + +
+ +
+
+ +
-
- - {% set total_q = 0 %} - {% if subjects %} - {% for sub in subjects %} - {% set total_q = total_q + sub.q_count %} - {% endfor %} - {% endif %} - {{ total_q }} - - 错题总数 +
+ +
+ {% for stage in stages %} + + {% endfor %} +
- -
- - - - - - - - - - - {% if subjects %} - {% for sub in subjects %} - - - - - - - {% endfor %} - {% else %} - - - - {% endif %} - -
科目名称题目数创建时间管理
-
- ID:{{ sub.ID }} - {{ sub.name }} -
-
- {{ sub.q_count }} 题 - - {{ sub.updated_at }} - - 查看 -
-
🔍
- 暂无科目数据
- 请检查数据库中的 Subject 表是否已录入内容 -
+ +
+ +
+
错题情况回顾
+
错题总数0
+
已解决数0
+
+ 解决比例 + 0% +
+
遗留题目数0
+
+ + +
+
强化推题追踪
+
推题总数0
+
已完成数0
+
+ 完成比例 + 0% +
+
遗留题目数0
+
-
+ + -- 2.43.0