import json from sqlalchemy import text def aqi_data(session): query2 = text(''' SELECT city_name,aqi,sqiText FROM aqi_now_analyse''') data = session.execute(query2).fetchall() data1 = [] for idx, row in enumerate(data): data1.append( { "name": row[0], "value": [idx + 1, float(row[1]), row[2]], } ) # Serialize the list of dictionaries to JSON data = json.dumps(data1, ensure_ascii=False, indent=4) # 将 JSON 数据写入 JavaScript 文件 with open('data.js', 'a', encoding='utf-8') as js_file: js_file.write('const Chart6_Data = ') js_file.write(data) js_file.write("\n\n")