Python + Flask + Bootstarp 生成分析图表

直接上代码:view.py

#-------------------------------------
1
user_question_data = [] 2 for u in users: 3 color=COLOR_LIB[random.randint(0,(len(COLOR_LIB))-1)] 4 u.question_num = db_session.query(Question).filter(Question.user_id==u.id).count() 5 user_question_data.append({"value":u.question_num,"color":color, "label":u.username}) 6 print(json.dumps(user_question_data)) 7 #print(jsonify(json.dumps(user_question_data))) 8 return render_template('m_analysis.html',u_q_data=repr(user_question_data))

界面脚本代码:

<script src="/static/js/Chart.min.js"></script>
 <script>
 var uqdata = {{u_q_data|safe}};
    window.onload = function() {
        var ctx_pie = document.getElementById("uqchart").getContext("2d");
        window.myPieChart = new Chart(ctx_pie).Pie(uqdata, {
            responsive : true
        });
    }
 </script>

HTML代码

<canvas id="uqchart"></canvas>

需要引入Bootstrap类库及Jquery类库。需要注意Jinja模板转义的情况,在页面上 {{变量名 | safe}}方可。

原文地址:https://www.cnblogs.com/purple-blog/p/5119140.html