parent
efaca4c00b
commit
9c4516d62b
@ -0,0 +1,92 @@
|
||||
import pymysql
|
||||
from datetime import datetime, timedelta
|
||||
import time
|
||||
|
||||
|
||||
class MyDB:
|
||||
def __init__(self, host, user, passwd, db):
|
||||
self.conn = pymysql.connect(host, user, passwd, db)
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
def __del__(self):
|
||||
self.conn.close()
|
||||
|
||||
# 获取当然日期
|
||||
def get_cur_date(self):
|
||||
date = datetime.today()
|
||||
curdate = date.strftime('%Y.%m.%d')
|
||||
return curdate
|
||||
|
||||
# 获取前N天的日期
|
||||
def get_pren_date(self, n=1):
|
||||
predate = datetime.today() + timedelta(-n)
|
||||
predate = predate.strftime('%Y.%m.%d')
|
||||
return predate
|
||||
|
||||
# 获取现有确诊人数最多的5个国家
|
||||
def get_country_curconfirm_top5(self):
|
||||
curdate = self.get_cur_date() # 获取当天的日期
|
||||
sql = "select area,curConfirm,pub_date from foreign_daily_datas where pub_date like '%s' order by curConfirm desc limit 5" % (curdate + '%')
|
||||
#print('+++ sql: %s' % sql)
|
||||
|
||||
results = []
|
||||
try:
|
||||
self.cursor.execute(sql)
|
||||
results = self.cursor.fetchall()
|
||||
|
||||
n = 1
|
||||
|
||||
while len(results) <= 0:
|
||||
predate = self.get_pren_date(n)
|
||||
sql = "select area,curConfirm,pub_date from foreign_daily_datas where pub_date like '%s' order by curConfirm desc limit 5" % (predate + '%')
|
||||
#print('+++ presql: %s' % sql)
|
||||
|
||||
self.cursor.execute(sql)
|
||||
results = self.cursor.fetchall()
|
||||
n += 1
|
||||
|
||||
if n >= 30:
|
||||
break
|
||||
else:
|
||||
time.sleep(1)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
return results
|
||||
|
||||
#测试
|
||||
if __name__ == "__main__":
|
||||
mydb = MyDB('localhost', 'root', '1999z5g24x', 'text_data_increasing')
|
||||
results = mydb.get_country_curconfirm_top5() # results : []
|
||||
for i in results:
|
||||
print(i)
|
||||
|
||||
|
||||
|
||||
|
||||
from flask import Flask, render_template
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/hello')
|
||||
def say_hello():
|
||||
return '<html><body><h1>Hello World</h1></body></html>'
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
return render_template('we_text.html', name='Jerry')
|
||||
|
||||
|
||||
@app.route('/country_topn_curconfirm')
|
||||
def get_country_curconfirm_topn():
|
||||
mydb = MyDB('localhost', 'root', '1999z5g24x', 'text_data_increasing')
|
||||
results = mydb.get_country_curconfirm_top5() # results : []
|
||||
return jsonify(pub_date=results[0][2], country=[x[0] for x in results], curConfirms=[x[1] for x in results])
|
||||
|
||||
|
||||
app.run()
|
@ -0,0 +1,57 @@
|
||||
var myCharts1 = echarts.init(document.getElementById("main1"));
|
||||
|
||||
var option1 = {
|
||||
title: {
|
||||
text: '现有确诊人数Top5的国家',
|
||||
subtext: '',
|
||||
left: 'center',
|
||||
textStyle:{
|
||||
color:'#FF0000',
|
||||
fontFamily:'微软雅黑'
|
||||
}
|
||||
},
|
||||
tooltip: {
|
||||
formatter: '<br/>{b}:{c}'
|
||||
},
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: []
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
},
|
||||
series: [
|
||||
{
|
||||
data: [],
|
||||
type: 'bar',
|
||||
showBackground: true,
|
||||
backgroundStyle: {
|
||||
color: 'rgba(220, 220, 220, 0.8)'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
// 向后台发起请求,获取数据
|
||||
$.ajax({
|
||||
cache: false,
|
||||
type:"GET",
|
||||
url:"/country_topn_curconfirm",
|
||||
data: null,
|
||||
dataType : "json",
|
||||
async: false,
|
||||
error: function(request) {
|
||||
alert("发送请求失败!");
|
||||
},
|
||||
success: function(result) {
|
||||
// pub_date, privinces, curConfirms
|
||||
option1.title.subtext = "数据更新时间: " + result.pub_date
|
||||
for(i=0; i<result.country.length; ++i) {
|
||||
option1.xAxis.data.push(result.country[i])
|
||||
option1.series[0].data.push(result.curConfirms[i])
|
||||
}
|
||||
console.info(result)
|
||||
}
|
||||
});
|
||||
|
||||
myCharts1.setOption(option1);
|
Loading…
Reference in new issue