项目不需要多复杂。分享一个Flask+SQLite+Chart.js的极简方案,半天就能搭起来。
项目结构:
企业AI落地-dashboard/
app.py
models.py
templates/
index.html
brand.html
static/
style.css
requirements.txt
app.py:
code
from flask import Flask, render_template, jsonify
import sqlite3
app = Flask(__name__)
DB_PATH = '企业AI落地_data.db'
def get_db():
conn = sqlite3.connect(DB_PATH)return conn
@app.route('/')
code
def index():
db = get_db()
brands = db.execute('SELECT DISTINCT brand_name FROM checks ORDER BY brand_name').fetchall()
stats = db.execute('''COUNT(*) as total,
SUM(CASE WHEN mentioned=1 THEN 1 ELSE 0 END) as mentioned,
ROUND(AVG(mentioned)*100, 1) as visibility
FROM checks
WHERE date >= date("now", "-30 days")
GROUP BY brand_name
''').fetchall()
return render_template('index.html', brands=brands, stats=stats)
@app.route('/api/trend/<brand>')
code
def brand_trend(brand):
db = get_db()
data = db.execute('''ROUND(AVG(mentioned)*100, 1) as visibility
FROM checks
WHERE brand_name = ?
AND date >= date("now", "-30 days")
GROUP BY date
ORDER BY date
''', [brand]).fetchall()
code
return jsonify({'values': [r['visibility'] for r in data]
})
if __name__ == '__main__':
app.run(debug=True, port=5000)
index.html核心部分(用Chart.js画图):
<canvas id="trendChart" width="800" height="300"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
fetch('/api/trend/你的品牌名')
.then(r => r.json())
code
.then(data => {
new Chart(document.getElementById('trendChart'), {code
data: {code
datasets: [{data: data.values,
borderColor: '#3b49df',
tension: 0.3,
fill: true,
backgroundColor: 'rgba(59,73,223,0.1)'
}]
},
code
options: {
scales: { y: { beginAtZero: true, max: 100 } }
}
});
});requirements.txt:
code
flask==3.0.0这个方案适合个人或小团队快速搭建。不需要什么微服务架构。