import os
import json
from flask import Flask, render_template_string, request, redirect, url_for, flash
app = Flask(__name__)
app.secret_key = "supersecretkey"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
VOCAB_DIR = os.path.join(BASE_DIR, "data", "vocab")
CONFIG_DIR = os.path.join(BASE_DIR, "data", "config")
FILES = {
"l1": os.path.join(VOCAB_DIR, "l1_standard.json"),
"l2": os.path.join(VOCAB_DIR, "l2_hospital.json"),
"l3": os.path.join(VOCAB_DIR, "l3_mapping.json"),
"pinyin": os.path.join(VOCAB_DIR, "pinyin_map.json"),
"scoring": os.path.join(CONFIG_DIR, "scoring_standard.json")
}
HTML_TEMPLATE = """
医疗影像AI质控 - 后台管理
🏥 医疗影像质控后台管理
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
正在编辑:{{ file_name }}
"""
@app.route('/')
def index():
return redirect(url_for('edit', file_key='l1'))
@app.route('/edit/', methods=['GET', 'POST'])
def edit(file_key):
if file_key not in FILES:
return "File not found", 404
file_path = FILES[file_key]
if request.method == 'POST':
content = request.form.get('content')
try:
# Validate JSON
json_data = json.loads(content)
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=2)
flash("保存成功!RuleEngine 已实时同步最新规则。", "success")
except Exception as e:
flash(f"保存失败:JSON 格式错误 ( {str(e)} )", "error")
return redirect(url_for('edit', file_key=file_key))
# GET
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
else:
content = "{}" if file_key != "l1" and file_key != "l2" else "[]"
return render_template_string(
HTML_TEMPLATE,
content=content,
current=file_key,
file_name=os.path.basename(file_path)
)
if __name__ == '__main__':
# Ensure directories exist
os.makedirs(VOCAB_DIR, exist_ok=True)
os.makedirs(CONFIG_DIR, exist_ok=True)
app.run(port=5005, debug=True)