Daily Weather Forecast Email

This post will be about automating a daily weather forecast utilizing a modified version of the Python Email script from the previous post.

The steps for this project are:

  • Find a way to get weather forecast data
  • Get a script to call that data
  • Incorporate the script into the email code file

For the first step, we will look for an API that will give us weather data. I searched for a weather API on Google and found the WeatherAPI.com through RapidAPI.com.

After subscribing to the API, we can get a Python script to call forecast data:

We can run a test in the workspace to see how to get what we want. I’ll use Denver, CO for this example.

As we can see, the data is presented as multiple dictionaries within dictionaries, as well as a list thrown in there. To make accessing the data a bit simpler, I will define a variable:

data = response.json()

This way we can call the dictionary keys from the ‘data’ variable, depending on which weather attribute we want. Lets say we want max temperature in Fahrenheit. The dictionary keys to call are:

data['forecast']['forecastday'][0]['day'][maxtemp_f']

To test, we run this through the CLI:

And we get 96.4, same as from the workspace test run above.

We know how to extract the data, so now we need to construct the email we want to send. We can do that with the method of sending a sample email and copying the HTML element into a new file. I will call it weather_template.html


For this email, we will be needing the date, city, state, high temp, low temp, chance of rain, total amount of rain, and max wind. All we have to do is call the correct dictionary keys for each and assign them to a corresponding variable.

However, we run into an issue:

If we set up the script with these variables, we get an error because the variable values are not strings. This is easy to fix by casting the float and integer values as strings:

The full script now is this:

Running the script should now send the email.



Looks like we are good to go. The next post will be about setting the email up to be sent automatically with cron.