新型肺炎最新可视化

数据源 DataSource

腾讯新闻: https://news.qq.com/zt2020/page/feiyan.htm

开发者工具

json 数据

https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5

https://www.json.cn

vscode 替换 "

转换 model

https://quicktype.io

保存 nCoV2019.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#-*- coding: utf-8 -*-

# This code parses date/times, so please
#
# pip install python-dateutil
#
# To use this code, make sure you
#
# import json
#
# and then, to convert JSON from a string, do
#
# result = n_co_v2019_from_dict(json.loads(json_string))

from datetime import datetime
import dateutil.parser


def from_int(x):
assert isinstance(x, int) and not isinstance(x, bool)
return x

...

def n_co_v2019_from_dict(s):
return NCoV2019.from_dict(s)


def n_co_v2019_to_dict(x):
return to_class(NCoV2019, x)

虚拟环境

1
2
3
4
$ python3 -m venv 2019
$ source 2019/bin/activate
(2019) $ pip3 install pyecharts
(2019) $ pip3 install flask

新建一个 Flask 项目

1
2
mkdir nCoV2019
cd nCoV2019

Flask 模板渲染

Step 1: 拷贝 pyecharts 模板

将 pyecharts 模板,位于 pyecharts.render.templates 拷贝当前目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cp -r ../2019/lib/python3.7/site-packages/pyecharts/render/templates .
# list
tree templates
templates
├── components.html
├── macro
├── nb_components.html
├── nb_jupyter_globe.html
├── nb_jupyter_lab.html
├── nb_jupyter_lab_tab.html
├── nb_jupyter_notebook.html
├── nb_jupyter_notebook_tab.html
├── nb_nteract.html
├── simple_chart.html
├── simple_globe.html
├── simple_page.html
└── simple_tab.html

0 directories, 13 files

Step 2: 渲染图表

请将下面的代码保存为 app.py 文件并移至项目的根目录下。

目录结构如下

1
2
3
4
5
6
7
tree -L 1
.
├── app.py
├── nCoV2019.py
└── templates

1 directory, 2 files

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pyecharts.charts import Bar
from pyecharts import options as opts
from flask import Flask
from jinja2 import Markup, Environment, FileSystemLoader
from pyecharts.globals import CurrentConfig

# 关于 CurrentConfig,可参考 [基本使用-全局变量]
CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))

app = Flask(__name__, static_folder="templates")

...

if __name__ == "__main__":
app.run()

Step 3: 运行项目

1
2
chmod +x app.py
./app.py

使用浏览器打开 http://127.0.0.1:5000 即可访问服务

Step 3: 编写代码

剩下就是编码阶段了,全部代码可以去 GitHub 查看

https://github.com/Game2020/nCoV2019