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")