Day 8 of the Anvil Advent Calendar

Build a web app every day until Christmas, with nothing but Python!

What’s the weather at the North Pole?

Weather conditions can be pretty tough for Santa Claus and his Elves at the North Pole. With todays advent calendar app, you can find out whether the weather is being naughty or nice for Santa:

https://north-pole-weather.anvil.app

Of course, the app is built with nothing but Python!

Here’s how it works:

AccuWeather provides a simple free API for getting the current weather conditions at a given location. All we need to do is send a request to the API and build a front end for it to display the information we want.

We can start by building the app’s UI by dragging and dropping components, like Labels, to display the weather information we want.

Dragging and dropping a Label onto a form and naming it self.chance_of_snow

Then we just need to get an API key and store it as a secret in our app. We can then create a server function to send the HTTP request and get weather data back:

@anvil.server.callable
def get_north_pole_weather():
  resp = anvil.http.request(f"http://dataservice.accuweather.com/currentconditions/v1/336714?apikey={anvil.secrets.get_secret('accuweather_key')}", json=True)
  return resp[0]

Then, we can set the properties of each front-end component, like a Label’s display text, in client-side Python to display the weather data:

    ...
    self.weather_description.text = weather_data['WeatherText']
    self.temperature_celsius.text = str(weather_data['Temperature']['Metric']['Value']) + "°C"
    ...

And that’s all there is to it! We now have an app to check the weather at the North Pole.

See the Source

Check out the source code for yourself!


Give the Gift of Python

Share this post: