Flask+MySQL+Echarts實(shí)現(xiàn)前后端交互

在MySQL中創(chuàng)建數(shù)據(jù)表,使用python連接數(shù)據(jù)庫,Ajax實(shí)現(xiàn)數(shù)據(jù)獲取,F(xiàn)lask框架做web后臺(tái),最后使用Echarts進(jìn)行可視化。

數(shù)據(jù)表如圖一所示:? ?(操作數(shù)據(jù)可以使用navicat)

圖一

工程目錄結(jié)構(gòu)如圖二所示:

圖二

使用的python第三方庫有:

from flask import Flask,render_template,url_for

import pymysql

import json

web.py源代碼如下:

app = Flask(__name__)

@app.route('/')

def my_tem():

#在瀏覽器上渲染my_templaces.html模板

? ? return render_template('my_template.html')

@app.route('/test',methods=['POST'])

def my_test():

#創(chuàng)建連接數(shù)據(jù)庫

? ? connection = pymysql.connect(host='localhost',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? user='root',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? passwd='123456',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? db='data',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? port=3306,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? charset='utf8'

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? )

cur=connection.cursor()#游標(biāo)(指針)cursor的方式操作數(shù)據(jù)

? ? sql='SELECT movie,score FROM tb1_movie_score' #sql語句

? ? cur.execute(sql)#execute(query, args):執(zhí)行單條sql語句。

? ? see=cur.fetchall()#使結(jié)果全部可看

#print(sql)

#print(see)

#print(cur)

#創(chuàng)建json數(shù)據(jù)

? ? xdays=[]

jsonData={}

yvalues=[]

for datain see:

xdays.append(data[0])

yvalues.append(data[1])

#print(xdays)

#print(yvalues)

? ? jsonData['xdays']=xdays

jsonData['yvalues']=yvalues

#print(jsonData)

#將json格式轉(zhuǎn)成str,因?yàn)槿绻苯訉ict類型的數(shù)據(jù)寫入json會(huì)發(fā)生報(bào)錯(cuò),因此將數(shù)據(jù)寫入時(shí)需要用到該函數(shù)。

? ? j=json.dumps(jsonData)

#print(j)

? ? cur.close()

connection.close()

#渲染html模板

? ? return (j)

if __name__ =="__main__":

#運(yùn)行項(xiàng)目

#my_test() #測試

? ? app.run(host='127.0.0.1',debug =True)#整個(gè)項(xiàng)目的運(yùn)行




my_template.html代碼如下:

<!DOCTYPE html>

? ? <meta charset="utf-8">

? ? <title>ECharts


? ? <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js">


? ? <script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts.min.js">


? ? <div id="main" style="width:600px;height:400px;">

? ? <script type="text/javascript">

? ? ? ? // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例

? ? ? ? var myChart =echarts.init(document.getElementById('main'));

? ? ? //建立axjx所需的json數(shù)據(jù)

? ? ? ? var app={

xday:[],

? ? ? ? ? ? yvalue:[]

};

? ? ? ? //發(fā)送ajax請求

? ? ? ? $(document).ready(function () {

getData();

? ? ? console.log(app.xday);

? ? ? console.log(app.yvalue)

});

? //設(shè)計(jì)畫圖

? ? function getData() {

$.ajax({

//渲染的是127.0.0.1/test 下的json數(shù)據(jù)

? ? ? ? ? ? url:'/test',

? ? ? ? ? ? data:{},

? ? ? ? ? ? type:'POST',

? ? ? ? ? ? async:false,

? ? ? ? ? ? dataType:'json',

? ? ? ? ? ? success:function(data) {

app.xday = data.xdays;

? ? ? ? ? ? ? ? app.yvalue = data.yvalues;

? ? ? ? ? ? ? ? myChart.setOption({

title: {

text:'電影評分總數(shù)'

? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? tooltip: {},

? ? ? ? ? ? ? ? ? ? legend: {

data:['評分']

},

? ? ? ? ? ? ? ? ? ? xAxis: {

data: app.xday

? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? yAxis: {},

? ? ? ? ? ? ? ? ? ? //

? ? ? ? ? ? ? ? ? ? series: [{

name:'評分',

? ? ? ? ? ? ? ? ? ? ? ? type:'bar',

? ? ? ? ? ? ? ? ? ? ? ? data: app.yvalue

? ? ? ? ? ? ? ? ? ? }]

})

},

? ? ? ? ? ? error:function (msg) {

console.log(msg);

? ? ? ? ? ? ? ? alert('系統(tǒng)發(fā)生錯(cuò)誤');

? ? ? ? ? ? }

})

}



運(yùn)行web.py代碼,如圖三所示:


圖三

點(diǎn)擊連接即可訪問站點(diǎn),如圖四所示:

圖四

更改MySQL中的數(shù)據(jù),如圖五所示:

圖五

刷新網(wǎng)頁即可展示最新數(shù)據(jù),如圖六所示:

圖六

查看后臺(tái),可以看到訪問的日志記錄,如圖七所示:

圖七

至此就實(shí)現(xiàn)了整個(gè)數(shù)據(jù)流的貫通,項(xiàng)目很小,也是汲取了前人的成果,本著開源的精神,更加詳細(xì)的展示了這個(gè)小系統(tǒng),希望對讀者有所幫助!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容