导入包
1
2
import pymysql
from flask import Flask, url_for, render_template, json
获取flask进程
1
2
3
4
app = Flask(__name__)
if __name__ == '__main__':
app.run()
连接数据库获得数据(getData())
连接数据库
1
2
3
4
5
6
7
8
db = pymysql.connect(
user='root',
password='root',
host='192.168.75.129',
database='sogou',
charset='utf8',
port=3306
)
获取cursor
1
cursor = db.cursor()
执行查询(需要指定查询哪一张表)
1
cursor.execute('select * from sogou.sogou_top50')
从cursor获得数据
1
alldata = cursor.fetchall()
打包json数据(top50Json())
分配路由
1
@app.route('/top50Json')
执行查询
1
all_data = getData()
打包成为json包(数据类型为字典,key为keyword, value为count)
1
2
3
4
5
6
7
t1 = []
t2 = []
for res in all_data:
t1.append(res[0])
t2.append(res[1])
json_value = json.dumps({'keyword': t1, 'count': t2})
return json_value
展示图表
分配路由
1
@app.route('/top50')
响应模板
1
2
def show():
return render_template('datashow.html')
####