首次提交

dev_test1
commit 4d43485651

@ -0,0 +1,38 @@
# encoding: utf-8
import flask
from flask import jsonify
from flask import render_template
from app.mydb import MyDB
app = flask.Flask(__name__)
@app.route('/')
def start(name=None):
# return render_template('echartstest1.html', name=name)
return render_template('covid2019_show.html', name=name)
@app.route('/query_province_datas', methods=["GET", "POST"])
def query_province_datas():
mydb = MyDB('localhost', 'root', '123456', 'conv19_datas')
datas = mydb.province_curConfirm_top_n()
ret = jsonify(pub_date=datas[0][2], areas=[x[1] for x in datas], curconfirms=[x[0] for x in datas])
return ret
@app.route('/query_province_import_datas', methods=["GET", "POST"])
def query_province_import_datas():
mydb = MyDB('localhost', 'root', '123456', 'conv19_datas')
datas = mydb.province_import_top_n()
ret = jsonify(pub_date=datas[0][3], provinces=[x[0] for x in datas], confirmed=[x[1] for x in datas], curconfirms=[x[2] for x in datas])
return ret
@app.route('/query_city_datas')
def query_city_datas():
return '查询地级市数据...'
if __name__ == '__main__':
app.run(debug=True)

@ -0,0 +1,131 @@
# encoding: utf-8
import pymysql
import time
from app.utils import get_cur_date, get_pre_n_date
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):
if self.conn is not None:
self.conn.close()
# 测试:获取当前数据库中的所有表
def test_query_tables(self):
# sql = 'show tables'
sql = "select * from province_daily_datas where pub_time like '2020.08.03%'"
self.cursor.execute(sql)
results = self.cursor.fetchall()
return results
# 获取最近一日的数据
# table: 查指定表
# timeHeader: 该表中时间字段
def fetch_datas(self, table='', timeHeader='pub_time'):
curdatestr = get_cur_date()
sql = "select * from %s where %s like '%s'" % (table, timeHeader, curdatestr + '%')
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
# 如果当天没有取到数据,则尝试取前一天的数据,直到取到数据为止
n = 1
targetdate = curdatestr
while len(results) <= 0:
predatestr = get_pre_n_date(n)
sql = "select * from %s where %s like '%s'" % (table, timeHeader, predatestr + '%')
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
n += 1
if len(results) > 0:
targetdate = predatestr
else:
time.sleep(2) # 休眠2s
if n >= 100:
print('+++ [mydb] 最近三个月都没有数据')
return tuple()
print('+++ [mydb] 获取到%s的数据,数量:%d' % (targetdate, len(results)))
return results
# 获取省份数据top5
def province_curConfirm_top_n(self, limit=5):
curdatestr = get_cur_date()
sql = 'select curConfirm,area,pub_date from province_daily_datas where pub_date like "%s" order by curConfirm desc limit %s' % (curdatestr+'%', limit)
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
n = 1
targetdate = curdatestr
while len(results) <= 0:
predatestr = get_pre_n_date(n)
sql = 'select curConfirm,area,pub_date from province_daily_datas where pub_date like "%s" order by curConfirm desc limit %s' % (predatestr+'%', limit)
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
n += 1
if len(results) > 0:
targetdate = predatestr
else:
time.sleep(2) # 休眠2s
if n >= 100:
print('+++ [mydb] 最近三个月都没有数据')
return tuple()
print('+++ [mydb] 获取到%s的数据,数量:%d' % (targetdate, len(results)))
return results
# 根据条件字典生成字符串
# def generate_condition_text(self, condition_dict, flag='%'):
# text = ''
# for i in range(len(condition_dict)):
# text += key + flag + condition_dict[key]
# 统计境外输入病例总数以及现有境外输入病例数前20个省份柱状图
def province_import_top_n(self, limit=20):
curdatestr = get_cur_date()
sql = 'select province,confirmed,curConfirm,pub_date from city_daily_datas where city like "%s" and pub_date like "%s" order by confirmed desc limit %s' % ('境外输入%', curdatestr+'%', limit)
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
n = 1
targetdate = curdatestr
while len(results) <= 0:
predatestr = get_pre_n_date(n)
sql = 'select province,confirmed,curConfirm,pub_date from city_daily_datas where city like "%s" and pub_date like "%s" order by confirmed desc limit %s' % ('境外输入%', predatestr+'%', limit)
print('+++ [mydb] execute "%s"' % sql)
self.cursor.execute(sql)
results = self.cursor.fetchall()
n += 1
if len(results) > 0:
targetdate = predatestr
else:
time.sleep(2) # 休眠2s
if n >= 100:
print('+++ [mydb] 最近三个月都没有数据')
return tuple()
print('+++ [mydb] 获取到%s的数据,数量:%d' % (targetdate, len(results)))
return results
# 获取昨日新增确诊病例数前5的城市
# 指定城市最近n日新增确诊数趋势
# 现有确诊数量前10个城市的昨日新增确诊和新增出院人数柱状图
if __name__ == '__main__':
mydb = MyDB('localhost', 'root', '123456', 'conv19_datas')
results = mydb.fetch_datas('province_daily_datas')
print(type(results))
print(len(results))
print(results)

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,233 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>疫情数据展示</title>
<!-- 引入 echarts.js -->
<!-- 这里是加载刚下好的echarts.min.js注意路径 -->
<script src="{{url_for('static', filename='echarts.js') }}"></script>
<script src="{{url_for('static', filename='jquery-3.5.1.min.js')}}"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小宽高的Dom -->
<h1 style="text-align:center;background-color: cadetblue;">
<font face="微软雅黑" size=6>疫情数据展示</font>
</h1>
<div id="main1" style="width: 1300px;height:600px;"></div>
<div id="main2" style="width: 1300px;height:600px;"></div>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
var myChart = echarts.init(document.getElementById('main1'));
var option={
//backgroundColor: '#2c343c',
title: {
show:true,
text: '国内各省份现存确诊病例数Top5',
subtext: '',
left: 'center',
textStyle:{
color:"rgba(0, 0, 0)",
fontSize:20,
align:"center"
}
},
textStyle: {color: '#2c343c'},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
type: 'scroll',
orient: 'vertical',
right: 100,
top: 100,
bottom: 20,
data: []
},
series : [
{
name: '省份',
type: 'pie',
radius: '55%',
center: ['50%', '50%'],
data:[],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
},
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
normal: {
lineStyle: {
color: 'rgba(0, 0, 0, 0.4)'
}
}
}
}
]
};
// 查询省份数据'query_province_datas'
$.ajax({
cache: false,
type: "POST",
url: "/query_province_datas",
data: null,
dataType : "json",
async: false,
error: function(request) {
alert("发送请求失败!");
},
success: function(result) {
for(i=0, max=result.areas.length; i<max; ++i) {
option.series[0].data.push({value:result.curconfirms[i], name:result.areas[i]})
option.legend.data.push(result.areas[i])
}
option.title.subtext = '数据更新时间:' + result.pub_date
}
});
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
var myChart = echarts.init(document.getElementById('main2'));
var option={
title: {
show:true,
text: '国内Top20省份境外输入病例数情况',
subtext: '',
left: 'center',
textStyle:{
color:"rgba(0, 0, 0)",
fontSize:20,
align:"center"
}
},
legend: {
data: ['累计境外输入病例数', '现有境外输入确诊病例数'],
left: 10
},
brush: {
toolbox: ['rect', 'polygon', 'lineX', 'lineY', 'keep', 'clear'],
xAxisIndex: 0
},
toolbox: {
feature: {
magicType: {
type: ['stack', 'tiled']
},
dataView: {}
}
},
tooltip: {},
xAxis: {
data: [],
name: '省份',
axisLine: {onZero: true},
splitLine: {show: false},
splitArea: {show: false}
},
yAxis: {
inverse: true,
splitArea: {show: false}
},
grid: {
left: 100
},
visualMap: {
type: 'continuous',
dimension: 1,
text: ['High', 'Low'],
inverse: true,
itemHeight: 200,
calculable: true,
min: -2,
max: 6,
top: 60,
left: 10,
inRange: {
colorLightness: [0.4, 0.8]
},
outOfRange: {
color: '#bbb'
},
controller: {
inRange: {
color: '#2f4554'
}
}
},
series: [
{
name: '累计境外输入确诊',
type: 'bar',
stack: 'one',
center: ['50%', '50%'],
emphasis: {
itemStyle: {
barBorderWidth: 1,
shadowBlur: 10,
shadowOffsetX: 0,
shadowOffsetY: 0,
shadowColor: 'rgba(0,0,0,0.5)'
}
},
itemStyle: {
normal: {color: 'rgba(208, 72, 68)'}
},
data: []
},
{
name: '现有境外输入确诊',
type: 'bar',
stack: 'one',
emphasis: {
itemStyle: {
barBorderWidth: 1,
shadowBlur: 10,
shadowOffsetX: 0,
shadowOffsetY: 0,
shadowColor: 'rgba(0,0,0,0.5)'
}
},
itemStyle: {
normal: {color: 'rgba(84, 123, 150)'}
},
data: []
}
]
};
$.ajax({
cache: false,
type: "POST",
url: "/query_province_import_datas",
data: null,
dataType : "json",
async: false,
error: function(request) {
alert("发送请求失败!");
},
success: function(result) {
console.info(result)
for (i=0, max=result.provinces.length; i<max; ++i) {
option.xAxis.data.push(result.provinces[i])
option.series[0].data.push(result.confirmed[i])
option.series[1].data.push(result.curconfirms[i])
}
option.title.subtext = '数据更新时间:' + result.pub_date
}
});
myChart.setOption(option);
</script>
</body>

@ -0,0 +1,69 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
<!-- 这里是加载刚下好的echarts.min.js注意路径 -->
<script src="{{url_for('static', filename='echarts.min.js') }}"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小宽高的Dom -->
<h2>ECharts图例演示</h2>
<div id="main" style="width: 1000px;height:600px;"></div>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
var option={
backgroundColor: '#2c343c',
textStyle: {
color: 'rgba(255, 255, 255, 0.3)'
},
series : [
{
name: '访问来源',
type: 'pie',
radius: '55%',
data:[
{value:400, name:'搜索引擎'},
{value:335, name:'直接访问'},
{value:310, name:'邮件营销'},
{value:274, name:'联盟广告'},
{value:235, name:'视频广告'}
],
roseType: 'angle',
itemStyle: {
emphasis: {
shadowBlur: 200,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
label: {
normal: {
textStyle: {
color: 'rgba(255, 255, 255, 0.3)'
}
}
},
labelLine: {
normal: {
lineStyle: {
color: 'rgba(255, 255, 255, 0.3)'
}
}
}
}
]
}
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>

@ -0,0 +1,11 @@
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>

@ -0,0 +1,16 @@
# encoding: utf-8
from datetime import datetime, timedelta
# 获取当日日期
def get_cur_date():
date = datetime.today()
date = date.strftime('%Y.%m.%d')
return date
# 获取前n日的日期
def get_pre_n_date(n=1):
predate = datetime.today() + timedelta(-n)
predate = predate.strftime('%Y.%m.%d')
return predate
Loading…
Cancel
Save