from sqlalchemy import text import json 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")