python_weather/web/data/rain.py

37 lines
1.0 KiB
Python
Raw Normal View History

2024-06-07 12:15:48 +00:00
import json
2024-06-07 13:03:58 +00:00
from sqlalchemy import text
2024-06-07 12:15:48 +00:00
def rain(session):
# 使用 text() 构造您的 SQL 查询
query = text('SELECT city_name,rain24h FROM rain_24h_analyse ORDER BY rain24h DESC LIMIT 7')
# 降水排行
rain = session.execute(query).fetchall()
x = []
y = []
for i in rain:
x.append(i[0])
y.append(float(i[1]))
dict_list = {"x": x, "y": y}
# Serialize the list of dictionaries to JSON
data = json.dumps(dict_list, ensure_ascii=False, indent=4)
# 将 JSON 数据写入 JavaScript 文件
with open('data.js', 'w', encoding='utf-8') as js_file:
js_file.write('const Chart1_Data = ')
js_file.write(data)
js_file.write("\n\n")
2024-06-07 13:03:58 +00:00
def rain_all(session):
query = text('SELECT SUM(rain24h) FROM rain_24h_analyse')
data = session.execute(query).fetchall()
with open('data.js', 'a', encoding='utf-8') as js_file:
js_file.write('const RAIN_Data = ')
js_file.write(str(round(data[0][0], 1)))
js_file.write("\n\n")