代码如下
from flask import Flask from flask import render_template from jinja2 import Markup, Environment, FileSystemLoader from pyecharts.globals import CurrentConfig from pyecharts.charts import Pie from pyecharts.charts import Bar from pyecharts.charts import Map from pyecharts import options as opts from pyecharts.options import * import requests import json import numpy as np import sys from pyecharts.charts import Page def spider_info(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' } response = requests.request(method='get', url=url, headers=headers) return json.loads(response.text) if response.status_code == 200 else None # 获取国内统计数据【 境外输入 现有确诊 确诊 治愈 死亡】 def get_daily_data(): data = spider_info("https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5") if data is None: return '' dict = json.loads(data['data']) Update_Time = dict['lastUpdateTime'] province_res = [] for province in dict['areaTree'][0]['children']: name = province['name'] now_confirm = province['total']['nowConfirm'] confirm = province['total']['confirm'] heal = province['total']['heal'] dead = province['total']['dead'] import_abroad = 0 for item in province['children']: if item['name'] == '境外输入': import_abroad = item['total']['confirm'] break province_res.append([name, import_abroad, now_confirm, confirm, heal, dead]) return {'chinaTotal': dict['chinaTotal'], 'chinaAdd': dict['chinaAdd'], 'province': province_res} # 获取各个国家当前【新增、确诊、治愈、死亡】数据 def get_foreign_data(): data = spider_info('https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist') if data is None: return '' return [[item['name'], item['confirmAdd'], item['confirm'], item['heal'], item['dead']] for item in data['data']] def china_map(): daily_data = get_daily_data() data = zip(np.array(daily_data['province'])[:, 0], np.array(daily_data['province'])[:, 2]) data_list = [] for i in list(data): data_list.append(list(i)) map = Map() textstyle_opts = TextStyleOpts(color='black') tooltip_opts = opts.TooltipOpts(background_color='yellow', textstyle_opts=textstyle_opts) label_opts = opts.LabelOpts(color='red') itemstyle_opts = ItemStyleOpts(color='blue', border_color='blue', area_color='green') map.add("现有确诊人数", data_list, "china", label_opts=label_opts, tooltip_opts=tooltip_opts, itemstyle_opts=itemstyle_opts) map.set_global_opts(title_opts=opts.TitleOpts(title="疫情各省情况分布")) map.render("D:\WorkSpace\WorkSpace\FlaskL\firstapp\templates\map.html") return map def china_pie(): daily_data = get_daily_data()['chinaTotal'] data = [daily_data['heal'], daily_data['dead'], daily_data['nowConfirm']] label = ['总共治愈人数', '总共死亡人数', '现有确诊人数'] data_pair = [] for i in zip(label,data): data_pair.append(i) pie = Pie() pie.add(series_name='总共统计', data_pair=data_pair) #pie.add(series_name='总共统计', data_pair=data_pair, radius=["30%", "75%"], center=["25%", "50%"], rosetype="radius") pie.set_global_opts(title_opts=opts.TitleOpts(title="疫情总共统计")) pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) pie.render("D:\WorkSpace\WorkSpace\FlaskL\firstapp\templates\pie.html") return pie pie = china_pie() map = china_map() page = Page(page_title='HTML', layout=opts.PageLayoutOpts(justify_content='space-around', display='flex', flex_wrap='nowrap')) page.add(pie,map) page.render("pie_map.html") app = Flask(__name__, static_folder="templates") @app.route("/") def index(): return Markup(page.render_embed()) if __name__ == '__main__': app.run(debug=True)
结果呈现
代码优化: