Day 17 of the Anvil Advent Calendar

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

Will it be a White Christmas?

All the crooning songs are demanding snow, and you want to know whether today will be the day it happens. Find the answer with today’s Advent Calendar app:

Of course, it’s built with nothing but Python!

Our app looks up the weather based on your location. Sadly for us, the Anvil office is in the south of England, which means:

Here’s how it works:

Anvil works out the approximate location of every visitor to your app from their internet (IP) address. This information is available in your code at anvil.server.context.client.location (see the docs) – including an approximate latitude and longitude.

Then, it’s just a matter of getting a weather forecast API – and Dark Sky’s API is perfect for that! You’ll need to get an API key, but it only takes 2 minutes to sign up, and it’s free up to 1,000 requests per day.

def will_it_snow_here():
  location = anvil.server.context.client.location
  darksky_key = anvil.secrets.get_secret('darksky_key')
  r = anvil.http.request(f"{darksky_key}/{location.latitude},{location.longitude}",
  it_will_snow = (r['daily'][0].get("precipType") == "snow")
  return it_will_snow, f"{ or location.subdivision}, {}"

And now, we just need to build a Python UI! We get the forecast and the location from the server, and we use that to select which message to show (“yes” or “no”):

See the Source

Check out the source code for yourself!

Give the Gift of Python

Share this post: