python_weather/web/data/temp_now.py
wobeitaoleshigexuruo ada0e97916 style: map
2024-06-07 22:11:11 +08:00

47 lines
1.4 KiB
Python

import json
from sqlalchemy import text
def tem_now(session):
query2 = text('SELECT city_name,temperature FROM tem_now_analyse ORDER BY temperature DESC LIMIT 12')
data = session.execute(query2).fetchall()
x = []
y = []
for i in data:
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', 'a', encoding='utf-8') as js_file:
js_file.write('const Chart4_Data = ')
js_file.write(data)
js_file.write("\n\n")
def tem_avg(session):
query2 = text('SELECT AVG(temperature) FROM tem_now_analyse')
data = session.execute(query2).fetchall()
with open('data.js', 'a', encoding='utf-8') as js_file:
js_file.write('const TMP_Data = ')
js_file.write(str(round(data[0][0], 1)))
js_file.write("\n\n")
def tem_all(session):
query2 = text('SELECT city_name,temperature FROM tem_now_analyse')
data = session.execute(query2).fetchall()
data1 = []
for i in data:
data1.append({
"name": f"{i[0]}",
"value": i[1],
})
with open('data.js', 'a', encoding='utf-8') as js_file:
js_file.write('const TMP_ALL_DATA = ')
js_file.write(json.dumps(data1, ensure_ascii=False, indent=4))
js_file.write("\n\n")