python_weather/web/data/temp_24.py
2024-06-07 20:15:48 +08:00

38 lines
995 B
Python

import json
from sqlalchemy import text
def get_month(date: int) -> str:
month = date % 100
return f"{month}"
def tem_24(session):
query2 = text('''
SELECT date, AVG(hmax),AVG(hmin),AVG(havg) AS average_havg
FROM tem_2024_analyse
GROUP BY date
ORDER BY date
DESC LIMIT 12
''')
data = session.execute(query2).fetchall()
x = []
y1 = []
y2 = []
y3 = []
for i in data[::-1]:
x.append(get_month(i[0]))
y1.append(round(float(i[1]), 2))
y2.append(round(float(i[2]), 2))
y3.append(round(float(i[3]), 2))
dict_list = {"x": x, "y1": y1, "y2": y2, "y3": y3}
# Serialize the list of dictionaries to JSON
data = json.dumps(dict_list, ensure_ascii=False, indent=4)
# 将 JSON 数据写入 JavaScript 文件
with open('data.js', 'a', encoding='utf-8') as js_file:
js_file.write('const Chart2_Data = ')
js_file.write(data)
js_file.write("\n\n")