python_weather/web/data/info.py

44 lines
1017 B
Python
Raw Normal View History

2024-06-07 12:15:48 +00:00
import json
from sqlalchemy import text
def info(session):
query2 = text('''
SELECT city_name, info
FROM tem_now_analyse
''')
data = session.execute(query2).fetchall()
color_map = {
"": "#065aab",
"多云": "#06c8ab",
"": "#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,
"weather": 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 Chart31_Data = ')
js_file.write(data)
js_file.write("\n\n")