Using Python (language), Flask (framework) and Jinja2 (template) to GET and display current weather conditions from WUnderground API. From one station in WU's 250,000+ Personal Weather Station Network
Note: Weather Underground no longer provides free API keys.
from flask import Flask, flash, redirect, render_template, request
from urllib3 import PoolManager
import jsonThe Anaconda Python distribution was used to develop this app, so related dependencies have been linked below:
Sudden changes may not reflect the list above. Additional required modules may be overlooked.
NOTE: virtual environments should act jointly with development environments, since operating systems use a subset of Python. Added modules may modify important dependencies which potentially corrupt the system.
Jinja2 is inclusive, just as templates live in a specific directory, e.g. /templates. Since templates specify HTML document structure. Notice the handlebar structure insertion below, e.g. {{ co['weather'] }}.
<table>
<tr>
<td><span class="blue">Weather condition</span></td>
<td>{{ co['weather'] }}</td>
</tr>
...The current_cond() method assigns a dictionary variable for insertion of URL parts.
@app.route("/current/")
def current_cond():
d = {'api_key': '51857b97d97c71a0',
'state_code': 'IL',
'personal_weather_station': 'pws:KILMORRI2'}
pm = PoolManager()Values from that dictionary are spliced into the URL to GET raw weather data from the API.
r = pm.request('GET', 'http://api.wunderground.com/api/' +
d['api_key'] + '/conditions/q/' + d['state_code'] +
'/' + d['personal_weather_station'] + '.json')Sometimes leaf_wetness data is omitted, which causes a KeyError, so the following try/except block was added.
try:
lw = co['leaf_wetness']
except KeyError:
pass