python_weather/web/data/temp_now.py

47 lines
1.4 KiB
Python
Raw Normal View History

2024-06-07 12:15:48 +00:00
import json
from sqlalchemy import text
def tem_now(session):
2024-06-07 12:54:53 +00:00
query2 = text('SELECT city_name,temperature FROM tem_now_analyse ORDER BY temperature DESC LIMIT 12')
2024-06-07 12:15:48 +00:00
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")
2024-06-07 13:03:58 +00:00
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")
2024-06-07 14:11:11 +00:00
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")