我如何使用python mako或jinja模板将数据从json打印到html页面?

问题描述 投票:0回答:1

如何使用mako或jinja模板显示从json到html的数据。

这是我的json文件

{ "coord": { "lon": -0.13, "lat": 51.51 }, "weather": [ { "id": 300, "main": "Drizzle", "description": "light intensity drizzle", "icon": "09d" }], "base": "stations", "main": {"temp": 280.32, "pressure": 1012, "humidity": 81, "temp_min": 279.15, "temp_max": 281.15 }, "visibility": 10000, "wind": { "speed": 4.1, "deg": 80 }, "clouds": { "all": 90 }, "dt": 1485789600, "sys": { "type": 1, "id": 5091, "message": 0.0103, "country": "GB", "sunrise": 1485762037, "sunset": 1485794875 }, "id": 2643743, "name": "London", "cod": 200 }

这是我的用于下载json API的python代码:

`import json

导入请求

res = requests.get('https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22')

print(res.json())

res_text = res.text打印(类型(res_text))

data = json.loads(res_text)打印(类型(数据))

data_serialized = json.dump(data,open('data.json',“ w”),indent = 2)`

输出应为:

伦敦,GB小雨6°С温度从5到7.2°С,风速8.2 m / s。云100%,974 hPa

python json jinja2 mako python-jsons
1个回答
1
投票
将数据传递到模板并通过jinja模板进行访问例如:在视图末尾:

return render_template(<template_file>, data=<json-data>)

在模板文件中:

{{data.name}}, {{data.sys.country}} {{data.weather.description}}

[对其他人同样如此。
© www.soinside.com 2019 - 2024. All rights reserved.