|
| 1 | +from flask import Flask, render_template |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +import redis |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +app = Flask(__name__) |
| 8 | + |
| 9 | +pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True) |
| 10 | +r = redis.Redis(connection_pool=pool) |
| 11 | + |
| 12 | + |
| 13 | +@app.route('/') |
| 14 | +def index(): |
| 15 | + return render_template('a.html') |
| 16 | + |
| 17 | + |
| 18 | +@app.route('/global') |
| 19 | +def global_index(): |
| 20 | + context = { |
| 21 | + 'date': get_date(), |
| 22 | + 'statistics_data': json.loads(r.get('foreign_data')), |
| 23 | + 'country_data': get_rank_data(), |
| 24 | + 'article_data': json.loads(r.get('article_data')) |
| 25 | + } |
| 26 | + return render_template('global.html', **context) |
| 27 | + |
| 28 | + |
| 29 | +@app.route('/china') |
| 30 | +def china_index(): |
| 31 | + china_data = get_china_data() |
| 32 | + context = { |
| 33 | + 'date': get_date(), |
| 34 | + 'statistics_data': china_data[0], |
| 35 | + 'country_data': china_data[1], |
| 36 | + 'article_data': json.loads(r.get('article_data')) |
| 37 | + } |
| 38 | + return render_template('china.html', **context) |
| 39 | + |
| 40 | + |
| 41 | +def get_date(): |
| 42 | + today = datetime.date.today() |
| 43 | + yesterday = today + datetime.timedelta(days=-1) |
| 44 | + return {'today': today.strftime('%Y.%m.%d'), 'yesterday': yesterday.strftime('%m月%d日')} |
| 45 | + |
| 46 | + |
| 47 | +# 国外详细数据 |
| 48 | +def get_rank_data(): |
| 49 | + df = pd.DataFrame(json.loads(r.get('rank_data')), columns=['name', 'confirmAdd', 'confirm', 'heal', 'dead']) |
| 50 | + return df.sort_values('confirm', ascending=False).values.tolist() |
| 51 | + |
| 52 | + |
| 53 | +# 国内详细数据 |
| 54 | +def get_china_data(): |
| 55 | + china_data = json.loads(r.get('china_data')) |
| 56 | + statistics_data = {'nowConfirmAdd': china_data['chinaAdd']['confirm'], |
| 57 | + 'healAdd': china_data['chinaAdd']['heal'], |
| 58 | + 'deadAdd': china_data['chinaAdd']['dead'], |
| 59 | + 'nowConfirm': china_data['chinaTotal']['nowConfirm'], |
| 60 | + 'confirm': china_data['chinaTotal']['confirm'], |
| 61 | + 'heal': china_data['chinaTotal']['heal'], |
| 62 | + 'dead': china_data['chinaTotal']['dead'], |
| 63 | + } |
| 64 | + |
| 65 | + df = pd.DataFrame(china_data['province'], |
| 66 | + columns=['name', 'import_abroad', 'now_confirm', 'confirm', 'heal', 'dead']) |
| 67 | + province_list = df.sort_values('now_confirm', ascending=False).values.tolist() |
| 68 | + |
| 69 | + return statistics_data, province_list |
| 70 | + |
| 71 | + |
| 72 | +@app.route('/global_top10') |
| 73 | +def get_global_top10(): |
| 74 | + df = pd.DataFrame(json.loads(r.get('rank_data')), columns=['name', 'confirmAdd', 'confirm', 'heal', 'dead']) |
| 75 | + top10 = df.sort_values('confirmAdd', ascending=True).tail(10) |
| 76 | + result = {'country': top10['name'].values.tolist(), 'data': top10['confirmAdd'].values.tolist()} |
| 77 | + return json.dumps(result) |
| 78 | + |
| 79 | + |
| 80 | +@app.route('/global_map') |
| 81 | +def get_global_map(): |
| 82 | + df = pd.DataFrame(json.loads(r.get('rank_data')), columns=['name', 'confirmAdd', 'confirm', 'heal', 'dead']) |
| 83 | + records = df.to_dict(orient="records") |
| 84 | + china_data = json.loads(r.get('china_data')) |
| 85 | + result = { |
| 86 | + 'confirmAdd': [{'name': '中国', 'value': china_data['chinaAdd']['confirm']}], |
| 87 | + 'confirm': [{'name': '中国', 'value': china_data['chinaTotal']['confirm']}], |
| 88 | + 'heal': [{'name': '中国', 'value': china_data['chinaTotal']['heal']}], |
| 89 | + 'dead': [{'name': '中国', 'value': china_data['chinaTotal']['dead']}] |
| 90 | + } |
| 91 | + |
| 92 | + for item in records: |
| 93 | + result['confirmAdd'].append({'name': item['name'], 'value': item['confirmAdd']}) |
| 94 | + result['confirm'].append({'name': item['name'], 'value': item['confirm']}) |
| 95 | + result['heal'].append({'name': item['name'], 'value': item['heal']}) |
| 96 | + result['dead'].append({'name': item['name'], 'value': item['dead']}) |
| 97 | + |
| 98 | + return json.dumps(result) |
| 99 | + |
| 100 | + |
| 101 | +@app.route('/china_top10') |
| 102 | +def get_china_top10(): |
| 103 | + china_data = json.loads(r.get('china_data')) |
| 104 | + df = pd.DataFrame(china_data['province'], |
| 105 | + columns=['name', 'import_abroad', 'now_confirm', 'confirm', 'heal', 'dead']) |
| 106 | + top10 = df.sort_values('import_abroad', ascending=True).tail(10) |
| 107 | + result = {'country': top10['name'].values.tolist(), 'data': top10['import_abroad'].values.tolist()} |
| 108 | + return json.dumps(result) |
| 109 | + |
| 110 | + |
| 111 | +@app.route('/china_map') |
| 112 | +def get_china_map(): |
| 113 | + china_data = json.loads(r.get('china_data')) |
| 114 | + df = pd.DataFrame(china_data['province'], columns=['name', 'import_abroad', 'now_confirm', 'confirm', 'heal', 'dead']) |
| 115 | + records = df.to_dict(orient="records") |
| 116 | + result = { |
| 117 | + 'now_confirm': [], |
| 118 | + 'confirm': [], |
| 119 | + 'heal': [], |
| 120 | + 'dead': [] |
| 121 | + } |
| 122 | + |
| 123 | + for item in records: |
| 124 | + result['now_confirm'].append({'name': item['name'], 'value': item['now_confirm']}) |
| 125 | + result['confirm'].append({'name': item['name'], 'value': item['confirm']}) |
| 126 | + result['heal'].append({'name': item['name'], 'value': item['heal']}) |
| 127 | + result['dead'].append({'name': item['name'], 'value': item['dead']}) |
| 128 | + |
| 129 | + return json.dumps(result) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + app.run(host='127.0.0.1', port=5200, debug=True) |
| 134 | + # print(get_china_data()[1]) |
0 commit comments