forked from pyecharts/pyecharts-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_all.py
255 lines (226 loc) · 6.88 KB
/
run_all.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import hashlib
import json
import os
import subprocess
import operator
import time
from typing import Dict, List, Optional
from jinja2 import Template
# 清理的命令
ClearCmd: str = (
'find ./ -mindepth 2 -type f ! -name "*.py" ! -name "*.json" ! '
'-path ".//.idea/*" ! -path ".//.mypy_cache/*" ! -path ".//.git/*" '
"| xargs rm -f"
)
# 需要跳过的文件名
SKIP_FILE: List = [
".git",
".mypy_cache",
".idea",
"run_all.py",
"README.md",
"README_EN.md",
"LICENSE",
"index.html",
"CNAME",
"favicon.ico",
"_sidebar.md",
"_navbar.md",
"_coverpage.md",
".nojekyll",
".DS_Store",
"file_checker.json",
]
# 当前 Demo 的模块
ChartModelDict: Dict = {
"Bar": "柱状图",
"Bar3D": "3D 柱状图",
"BMap": "百度地图",
"Boxplot": "箱形图",
"Calendar": "日历图",
"Candlestick": "K 线图",
"Dataset": "数据集",
"EffectScatter": "涟漪散点图",
"Funnel": "漏斗图",
"Gauge": "仪表盘",
"Geo": "地理坐标",
"Graph": "关系图",
"GraphGL": "GL 关系图",
"Graphic": "图形组件",
"Grid": "组合组件",
"Heatmap": "热力图",
"Image": "图片",
"Line": "折线图",
"Line3D": "3D 折线图",
"Liquid": "水球图",
"Map": "地图",
"Map3D": "3D 地图",
"MapGlobe": "Globe 地图",
"Overlap": "层叠组件",
"Page": "页面组件",
"Parallel": "平行坐标系",
"PictorialBar": "象型柱图",
"Pie": "饼状图",
"Polar": "极坐标系",
"Radar": "雷达图",
"Sankey": "桑基图",
"Scatter": "散点图",
"Scatter3D": "3D 散点图",
"Sunburst": "旭日图",
"Surface3D": "3D 曲面",
"Tab": "分页组件",
"Table": "表格组件",
"Theme": "主题组件",
"ThemeRiver": "主题河流图",
"Timeline": "时间轴组件",
"Tree": "树图",
"Treemap": "矩形树图",
"WordCloud": "词云图",
}
# Markdown 模版
ChartMarkdownModel: str = """
## pyecharts 代码 / 效果
```python
{code}
```
<iframe width="100%" height="800px" src="{html}"></iframe>
"""
# 每个图的 README 的模版
ChartREADMEModel: str = "- [{chart_name}]({path} ':type=code')"
# 侧边栏模版
SidebarModel: str = """
- 概览
- [中文简介](README.md)
- [英文简介](README_EN.md)
- 示例
{% for chart in charts -%}
- [**{{ chart.name }} {{ chart.type }}**]({{ chart.type }}/README.md)
{% for info in chart.info -%}
{{ info }}
{% endfor %}
{% endfor %}
"""
def get_md5_of_file(filename: str) -> Optional[str]:
"""
获取文件的 MD5
:param filename: 文件名
:return: None or md5 string
"""
if not os.path.isfile(filename):
return None
md5_obj = hashlib.md5()
with open(filename, "rb") as f:
while True:
b = f.read(8096)
if not b:
break
md5_obj.update(b)
return md5_obj.hexdigest()
def save_md5_to_json(md5_json: dict):
"""
保存所有的文件 md5 到 json 文件中
:param md5_json: md5 dict
"""
with open("file_checker.json", "a") as md5_f:
md5_f.write(json.dumps(md5_json))
# 清理删除命令
# 命令解释:搜索当前目录下除根目录外的子文件夹中后缀不为 .py 和 .json 的文件,且忽略 .idea, .mypy_cache, .git 文件夹
# find ./ -mindepth 2 -type f ! -name "*.py" ! -name "*.json" !
# -path ".//.idea/*" ! -path ".//.mypy_cache/*" ! -path ".//.git/*"
# {上面的命令} | xargs rm -f
# {上面的命令} -exec rm -f {} \;
def clear_all():
print("正在清理...")
try:
print(ClearCmd)
os.system(ClearCmd)
time.sleep(3)
print("清理成功!")
except Exception as err:
raise err
def write_chart_markdown_and_html(
chart_script: str, chart_script_name: str, folder: str
):
"""
保存各图的 markdown 和 html
:param chart_script: 路径
:param chart_script_name: 脚本名称
:param folder: 文件夹名字
"""
chart_model = ChartMarkdownModel
with open(f"{chart_script_name}.md", "w") as md:
with open(chart_script, "r") as f:
chart_model = chart_model.replace("{code}", f.read())
subprocess.check_call(["python3", chart_script], stdout=subprocess.PIPE)
chart_model = chart_model.replace(
"{html}", f"{folder}/{chart_script_name}.html"
)
md.write(chart_model)
def update_chart_readme_markdown(chart_script_name: str, folder: str):
"""
更新各图的 README.md
:param chart_script_name: 脚本名称
:param folder: 文件夹名字
"""
readme_model = ChartREADMEModel
with open(f"README.md", "a") as readme:
readme_model = readme_model.replace(
"{chart_name}", f"{folder.capitalize()} - {chart_script_name.capitalize()}"
)
readme_model = readme_model.replace(
"{path}", f"{folder}/{chart_script_name}.md"
)
readme.write(readme_model + "\n")
def run_all() -> List[Dict]:
charts = []
for folder in os.listdir():
if folder in SKIP_FILE:
continue
print(f"当前图的类型: {folder}")
os.chdir(folder)
for chart_script in os.listdir():
if ".py" not in chart_script:
continue
else:
print(f"当前图的类型对应的示例: {folder}/{chart_script}")
# TODO 后续进行开发(新增图后局部更新)
# file_md5 = get_md5_of_file(filename=f"{chart_script}")
chart_script_name = chart_script.replace(".py", "")
# 更新各板块图的 md 和对应的 html
write_chart_markdown_and_html(chart_script, chart_script_name, folder)
# 更新各板块图的 README.md
update_chart_readme_markdown(chart_script_name, folder)
# 封装数据
with open(f"README.md", "r") as f:
charts.append(
{
"name": ChartModelDict[folder],
"type": folder,
"info": [d.replace("\n", "") for d in f.readlines()],
}
)
# 返回上一级
os.chdir("../")
return charts
def format_code():
os.system("black .")
os.system("flake8 --max-line-length 89 --ignore=F401,E501")
def write_sidebar_markdown(charts: Dict):
"""
更新项目侧边栏的 Markdown
:param charts: 图的数据
"""
with open("_sidebar.md", "w") as sidebar_f:
template = Template(SidebarModel)
result = template.render(charts)
sidebar_f.write(result)
if __name__ == "__main__":
# 清理所有旧版数据
clear_all()
# 格式化代码
format_code()
# 运行
all_charts = run_all()
all_charts.sort(key=operator.itemgetter("type"))
# 更新 SideBar
write_sidebar_markdown(charts={"charts": all_charts})