Be warned, this one is a longer read than the previous ones.
Let’s get to it
1 - Grabbing API Keys
We’ll now add in some weather using an API. For weather, we’ll use: https://openweathermap.org/api.
Go ahead, and create accounts on those, and grab an API access key. Generally, you’ll want to keep these keys private by storing them in a secrets file. But since I’m just using substack to teach, I don’t mind sharing my key:
Here is the documentation page for OpenWeather: https://openweathermap.org/api. Give this page a quick glossover.
Here is an example of me pulling data for Toronto, using my API key: https://api.openweathermap.org/data/2.5/weather?q=Toronto&units=metric&appid=cb932829eacb6a0e9ee4f38bfbf112ed
As you can see, it basically gives us all of the data as a JSON format. Similarily, we can also pull data from the open exchange API. Here is the documentation page for Openexchange: https://docs.openexchangerates.org/reference/api-introduction. We’ll worry about pulling data from this API later on.
2 - JSON in Python
We can grab structured weather data using a URL, but to make use of it directly in our Python projects like Flask apps, we need to fetch and read this data automatically with Python. Python comes with several handy libraries to help with this.
JSON, the format of the weather data we're talking about, looks a lot like a Python dictionary, which makes it easy to understand if you're familiar with Python. While it might be tempting to just convert JSON text into a Python dictionary using Python's eval function, it's not a safe practice, especially with data from the internet. This is because JSON and Python dictionaries aren't exactly the same—for instance, JSON uses true and false instead of Python's True and False, and using eval could run risky code unintentionally.
To safely handle this, we use Python's json library which safely parses JSON into Python dictionaries. We also need to fetch the data from the internet, and for that, we use Python libraries like urllib.request to download the data and urllib.parse to handle URL parameters correctly. These tools make it easy to work with web data in a safe and Python-friendly way.
2.1 Parsing JSON
To start working with JSON data in Python, you'll first need to add three new imports to your main.py file. Here's what you need to bring in:
import json
import urllib
Next, let’s create a new function called get_weather(). This function will send a request to the weather API using a specific query. It's quite simple; just add the following code into your project. Remember to swap out <your-api-key-here> with the actual API key you got from the OpenWeatherMap website.
def get_weather(query):
api_url = f'http://api.openweathermap.org/data/2.5/weather?q={query}&units=metric&appid=cb932829eacb6a0e9ee4f38bfbf112ed'
url = api_url
data = urllib.request.urlopen(url).read()
parsed = json.loads(data)
weather = None
if parsed.get("weather"):
weather = {"description":parsed["weather"][0]["description"],"temperature":parsed["main"]["temp"],"city":parsed["name"]
}
return weather
In our new function, we use the same URL we previously looked at, but we modify the query part so that the city from which we retrieve the weather can change dynamically. If city names contain spaces (like "New York"), we need to convert these spaces into a format that URLs can handle. We do this using the urllib.parse.quote() function, which, for example, turns a space into "%20".
Next, we use Python's urllib.request library to fetch the data from the internet and load it into a string. Just like when we were fetching feed data, downloading from the internet can be a bit shaky, so in a professional setting, you’d want to include some error handling and retry mechanisms here.
Once we have the data, we use the json.loads() function to turn the JSON string into a Python dictionary. Since the OpenWeatherMap API gives us more details than we actually need, we pick and choose what to keep and create a simpler dictionary that suits our app better. This way, we get just the data we need, nicely organized.
3 - Integrating it to our app
Now let's tweak the get_news() function a bit so we can use our new get_weather() function. We'll call get_weather() inside get_news(), using "Toronto" as our example city for now. Then, w
Keep reading with a 7-day free trial
Subscribe to Data Science & Machine Learning 101 to keep reading this post and get 7 days of free access to the full post archives.