Как распечатать данные с json на html-страницу с помощью шаблона python mako или jinja?

Как показать данные из json в html с помощью шаблона mako или jinja.

Вот мой файл 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
}

Вот мой код на Python для загрузки JSON API:

import json

import requests

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

print (res.json())

res_text= res.text
print(type(res_text))

data=json.loads(res_text)
print(type(data))

data_serialized= json.dump(data, open('data.json', "w"),indent=2)

Результат должен быть:

London, GB  light rain
6°С temperature from 5 to 7.2 °С, wind 8.2 m/s. clouds 100 %, 974 hPa

1 ответ

Передать данные в шаблон и получить доступ через шаблон jinja, например: В конце просмотра:

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

В файле шаблона:

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

и то же самое для других.

Другие вопросы по тегам