Sep-03-2019, 03:41 PM
Moin!
hiermit entwickle ich eine jkleine WebApp mit Flask und da ich eine API verwenden möchte für eine Visualisierung von Wetter-Daten (Name, temp_min und temp_max) habe ich den folgenden Fehler erhalten und mit Json zu arbeiten ist ganz neu für mich! Danke für Rückmeldung.
VG
KeyError
KeyError: 'name'
hiermit entwickle ich eine jkleine WebApp mit Flask und da ich eine API verwenden möchte für eine Visualisierung von Wetter-Daten (Name, temp_min und temp_max) habe ich den folgenden Fehler erhalten und mit Json zu arbeiten ist ganz neu für mich! Danke für Rückmeldung.
VG
KeyError
KeyError: 'name'
@app.route("/weather") def weather(): url = "https://api.openweathermap.org/data/2.5/group?id=524901,703448,2643743&units=metric&appid=get a Key" r = requests.get(url) data = r.json() # Retrieve city names cities = [] for city in data["name"]: cities.append(city["name"]) # Retrieve temperatures min_temp, max_temp = [], [] for elt in data["main"]: min_temp.append(elt["temp_min"]) max_temp.append(elt["temp_max"]) #Graph #Define simple style, added with pygal.style import style = LS("#333666", base_style=DS) chart = pygal.Bar(style=style, x_label_rotation=45, show_legend=False) chart.title = "Minimum and Maximum Temperatures" chart.x_labels = map(str, cities) chart.add("Minimum Temperature", min_temp) chart.add("Maximum Temperature", max_temp) chart = chart.render_data_uri() return render_template('weather.html', title='Call the API', chart = chart)
Output:{% extends "layout.html" %}
{% block title %}Call the API{% endblock %}
{% block content %}
<div class="content-section">
<h3>Visualization</h3>
<!-- Don't forget the "|safe"! -->
<div id="chart">
<embed type="image/svg+xml" src= {{ chart|safe }} />
</div>
</div>
{% endblock content %}
Json outputOutput:{
"cnt":3,
"list":[
{
"coord":{
"lon":37.62,
"lat":55.75
},
"sys":{
"country":"RU",
"timezone":10800,
"sunrise":1567478244,
"sunset":1567527668
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03d"
}
],
"main":{
"temp":23.64,
"pressure":1017,
"humidity":49,
"temp_min":23,
"temp_max":25
},
"visibility":10000,
"wind":{
"speed":4,
"deg":110
},
"clouds":{
"all":40
},
"dt":1567508823,
"id":524901,
"name":"Moscow"
},
{
"coord":{
"lon":30.52,
"lat":50.43
},
"sys":{
"country":"UA",
"timezone":10800,
"sunrise":1567480483,
"sunset":1567528836
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
}
],
"main":{
"temp":30.45,
"pressure":1014,
"humidity":40,
"temp_min":29,
"temp_max":31.67
},
"visibility":10000,
"wind":{
"speed":3,
"deg":300
},
"clouds":{
"all":0
},
"dt":1567508947,
"id":703448,
"name":"Kiev"
},
{
"coord":{
"lon":-0.13,
"lat":51.51
},
"sys":{
"country":"GB",
"timezone":3600,
"sunrise":1567487750,
"sunset":1567536278
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04d"
}
],
"main":{
"temp":18.83,
"pressure":1024,
"humidity":59,
"temp_min":16.67,
"temp_max":20.56
},
"visibility":10000,
"wind":{
"speed":4.6,
"deg":260
},
"clouds":{
"all":90
},
"dt":1567508964,
"id":2643743,
"name":"London"
}
]
}