python_weather/web/data/aqi_pie.py
wobeitaoleshigexuruo c70f9ff81e style
2024-06-07 20:54:53 +08:00

43 lines
1.0 KiB
Python

import json
from sqlalchemy import text
def aqi_pie(session):
query2 = text('''
SELECT city_name,sqiText
FROM aqi_now_analyse''')
data = session.execute(query2).fetchall()
color_map = {
"": "#06c8ab",
"": "#066eab",
"轻度污染": "#0696ab",
}
info_map = {}
for row in data:
if row[1] not in info_map:
info_map[row[1]] = []
info_map[row[1]].append(row[0])
data1 = []
for k, v in info_map.items():
data1.append(
{
"value": len(v),
"name": k,
"degree": v,
"color": color_map.get(k, "#065aab"),
}
)
# Serialize the list of dictionaries to JSON
data = json.dumps(data1, ensure_ascii=False, indent=4)
# 将 JSON 数据写入 JavaScript 文件
with open('data.js', 'a', encoding='utf-8') as js_file:
js_file.write('const Chart32_Data = ')
js_file.write(data)
js_file.write("\n\n")