Python Flask定时调度疫情大数据爬取全栈项目实战使用-18可视化大屏右侧模板制作## 可视化大屏模板制作
最终的效果
复制柱状图option
main.html导入js文件:
utils.py
添加数据库查询操作:
def get_r1_data():
sql = 'select city,confirm from ' \
'(select city,confirm from details ' \
'where update_time=(select update_time from details order by update_time desc limit 1) ' \
'and province not in ("湖北","北京","上海","天津","重庆","香港","台湾") ' \
'union all ' \
'select province as city,sum(confirm) as confirm from details ' \
'where update_time=(select update_time from details order by update_time desc limit 1) ' \
'and province in ("北京","上海","天津","重庆","香港","台湾") group by province) as a ' \
'order by confirm desc limit 5'
res = query(sql)
return res
def get_r2_data():
sql = "select content from hotsearch order by id desc limit 20"
res = query(sql)
return res
app.py定义路由:
@app.route('/r1')
def get_r1_data():
data = utils.get_r1_data()
city = []
confirm = []
for k,v in data:
city.append(k)
confirm.append(int(v))
return jsonify({"city": city,"confirm": confirm})
@app.route('/r2')
def get_r2_data():
data = utils.get_r2_data()
d = []
for i in data:
k = i[0].rstrip(string.digits)
#v = i[0][len(k):]
v = '1000'
ks = extract_tags(k)
for j in ks:
if not j.isdigit():
d.append({"name": j,"value": v})
return jsonify({"kws": d})
controler.js中定义ajax回调
function get_r1_data() {
$.ajax({
url:"/r1",
success: function(data) {
option_right1.xAxis.data = data.city
option_right1.series[0].data = data.confirm
ec_right1.setOption(option_right1)
},
error: function(xhr, type, errorThrown) {
}
})
}
function get_r2_data() {
$.ajax({
url:"/r2",
success: function(data) {
option_right2.series[0].data = data.kws
ec_right2.setOption(option_right2)
},
error: function(xhr, type, errorThrown) {
}
})
}
词云图需要引入echart-worldcloud.js:
js文件可以到官网的进行下载:
https://echarts.apache.org/zh/download-extension.html
安装Jieba分词
pip install jieba
代码中使用这个jieba库
from jieba.analyse import extract_tags
import string
词云图的option接受的是这个: