← Tutorials

Add live weather to your project

Ask a free weather service for the weather right now, and show it in your app or web page.

My project uses

Choose one above to see the steps.

Want your app or page to know what the weather's doing right now? By the end of this page it will show something like:

Canberra: 18.4 °C, wind 12.6 km/h

The numbers come live from Open-Meteo, a free weather service. You don't need to sign up or get a key, so there's nothing secret to keep out of your code. Choose at the top whether you're building a Gradio app or a web page.

You'll need an app made with gr.Interface (Your first app). If you'd like the longer version of the idea, Ask an API is a whole lesson on it, but you don't need it first.

This runs on your own computer, not in the tutorials' playground, and needs two libraries. Install them once:

pip install requests gradio

You'll need a page you can change with JavaScript (querySelector and textContent). If you'd like the longer version of the idea, Data from the web: fetch is a whole lesson on it, but you don't need it first.

How it works

A weather service like Open-Meteo is an API: a website made for programs instead of people. Your program sends it a question written as a web address, much like typing an address into a browser. Instead of a page, it sends back data: the numbers you asked for, in a format your program can read.

Your question has three parts: where (the place), what (which measurements you want) and when (right now, or a forecast). The steps below build that question, send it, and pick the answer apart.

Find your place

Open-Meteo doesn't know place names. It wants a latitude and longitude: two numbers that pin a spot on the map. Search for your town's name followed by "latitude longitude", or right-click a spot on an online map. Anywhere south of the equator, the latitude has a minus sign: Canberra is about -35.28, 149.13.

Ask for the weather

Start by just asking and printing whatever comes back.

import requests

URL = "https://api.open-meteo.com/v1/forecast"
details = {"latitude": -35.28, "longitude": 149.13, "current": "temperature_2m,wind_speed_10m"}

reply = requests.get(URL, params=details, timeout=10).json()
print(reply)

Here's what each part does:

  • URL is where the question goes: Open-Meteo's forecast service.
  • details is the question itself, as a dictionary:
    • "latitude" and "longitude" say where.
    • "current" says when (right now, not a forecast) and what: the names of the measurements you want, with commas between. temperature_2m is the air temperature 2 metres above the ground, which is how weather stations measure it. wind_speed_10m is the wind speed 10 metres up. Those are Open-Meteo's names, so they have to be spelt exactly like that.
  • requests.get(URL, params=details) sends the question. params= turns your dictionary into the end of the web address for you, so you don't have to write it out.
  • timeout=10 means give up after 10 seconds if there's no answer, instead of waiting forever.
  • .json() turns the reply into Python dictionaries and lists you can read.

Run it. You should see a long dictionary printed out. It has some extra information, such as the place's height above sea level and the units used ('°C', 'km/h'), and somewhere in it is 'current', with the temperature and wind inside. That's the part you want.

Pick out the temperature and wind

now = reply["current"]
print(str(now["temperature_2m"]) + " °C, wind " + str(now["wind_speed_10m"]) + " km/h")

Add these lines under the others and run it again: you should see one tidy line, like 18.4 °C, wind 12.6 km/h. Read the square brackets left to right: the reply, then its "current" part, then the value inside that.

Turn it into a function

Gradio runs a function when someone presses Submit, so the weather needs to be one: a place name goes in, a line of text comes out. A dictionary keeps the latitude and longitude for each place you want to offer.

PLACES = {
    "Canberra": {"latitude": -35.28, "longitude": 149.13},
    "Sydney": {"latitude": -33.87, "longitude": 151.21},
    "Darwin": {"latitude": -12.46, "longitude": 130.84},
}

def weather(place_name):
    place = PLACES[place_name]
    details = {"latitude": place["latitude"], "longitude": place["longitude"], "current": "temperature_2m,wind_speed_10m"}
    now = requests.get(URL, params=details, timeout=10).json()["current"]
    return place_name + ": " + str(now["temperature_2m"]) + " °C, wind " + str(now["wind_speed_10m"]) + " km/h"

Put it in your app

Use your function in an Interface. A Dropdown lets people pick a place:

import gradio as gr

gr.Interface(
    fn=weather,
    inputs=gr.Dropdown(["Canberra", "Sydney", "Darwin"], value="Canberra", label="Place"),
    outputs=gr.Textbox(label="Weather now"),
    title="Weather now",
    article="Weather data by Open-Meteo.com (CC BY 4.0)",
).launch()

Run it, pick a place and press Submit: you should see that place's weather right now. The article line puts a small credit to Open-Meteo under your app, which is the one thing they ask for in return.

Cope when the internet drops out

Right now, if the connection drops, your app shows a red Error and nothing useful. try and except catch that problem so your app can say what happened instead. Put them around the line that asks for the weather:

def weather(place_name):
    place = PLACES[place_name]
    details = {"latitude": place["latitude"], "longitude": place["longitude"], "current": "temperature_2m,wind_speed_10m"}
    try:
        now = requests.get(URL, params=details, timeout=10).json()["current"]
    except requests.RequestException:
        return "Couldn't reach the weather service. Check the internet and try again."
    return place_name + ": " + str(now["temperature_2m"]) + " °C, wind " + str(now["wind_speed_10m"]) + " km/h"
Show the full code
import requests
import gradio as gr

URL = "https://api.open-meteo.com/v1/forecast"

PLACES = {
    "Canberra": {"latitude": -35.28, "longitude": 149.13},
    "Sydney": {"latitude": -33.87, "longitude": 151.21},
    "Darwin": {"latitude": -12.46, "longitude": 130.84},
}

def weather(place_name):
    place = PLACES[place_name]
    details = {"latitude": place["latitude"], "longitude": place["longitude"], "current": "temperature_2m,wind_speed_10m"}
    try:
        now = requests.get(URL, params=details, timeout=10).json()["current"]
    except requests.RequestException:
        return "Couldn't reach the weather service. Check the internet and try again."
    return place_name + ": " + str(now["temperature_2m"]) + " °C, wind " + str(now["wind_speed_10m"]) + " km/h"


gr.Interface(
    fn=weather,
    inputs=gr.Dropdown(["Canberra", "Sydney", "Darwin"], value="Canberra", label="Place"),
    outputs=gr.Textbox(label="Weather now"),
    title="Weather now",
    article="Weather data by Open-Meteo.com (CC BY 4.0)",
).launch()

Ask for the weather

Start by just asking and logging whatever comes back.

const URL = "https://api.open-meteo.com/v1/forecast?latitude=-35.28&longitude=149.13&current=temperature_2m,wind_speed_10m";

async function load() {
  const response = await fetch(URL);
  const data = await response.json();
  console.log(data);
}
load();

Here's what each part does:

  • The address before the ? is where the question goes: Open-Meteo's forecast service.
  • After the ? is the question itself, as name=value pairs with & between them:
    • latitude=-35.28&longitude=149.13 says where.
    • current=... says when (right now, not a forecast) and what: the names of the measurements you want, with commas between. temperature_2m is the air temperature 2 metres above the ground, which is how weather stations measure it. wind_speed_10m is the wind speed 10 metres up. Those are Open-Meteo's names, so they have to be spelt exactly like that.
  • fetch(URL) sends the question, and .json() turns the reply into JavaScript objects you can read.
  • Both take a moment to arrive over the internet, so they go inside an async function, with await in front to wait for each one.

Open your page, then the browser's console. You should see an object you can click to open up. It has some extra information, such as the place's height above sea level and the units used (°C, km/h), and inside it current holds the temperature and wind. That's the part you want.

Pick out the temperature and wind

Put these lines inside load, under the console.log(data) line:

const now = data.current;
console.log(now.temperature_2m + " °C, wind " + now.wind_speed_10m + " km/h");

Reload: the console should now show one tidy line, like 18.4 °C, wind 12.6 km/h.

Show it on your page

The console is only for you. To put the weather on the page, give it somewhere to go:

<p id="weather">Loading the weather…</p>
<p><small>Weather data by Open-Meteo.com (CC BY 4.0)</small></p>

Then change load so it fills that paragraph in:

async function load() {
  const response = await fetch(URL);
  const now = (await response.json()).current;
  document.querySelector("#weather").textContent = "Canberra: " + now.temperature_2m + " °C, wind " + now.wind_speed_10m + " km/h";
}
load();

The small line credits Open-Meteo, which is the one thing they ask for in return.

Reload your page: "Loading the weather…" should change to the weather after a moment.

Cope when the internet drops out

If the connection drops, fetch fails and your page is stuck on "Loading the weather…" forever. try and catch let it say what happened instead:

async function load() {
  const box = document.querySelector("#weather");
  try {
    const response = await fetch(URL);
    const now = (await response.json()).current;
    box.textContent = "Canberra: " + now.temperature_2m + " °C, wind " + now.wind_speed_10m + " km/h";
  } catch (error) {
    box.textContent = "Couldn't reach the weather service. Check the internet and try again.";
  }
}
load();

Try it here

Not sure the weather service works on your school's network? This button runs the same code right here. If it shows the weather, your network is fine, and any problem is in your code.

Nothing yet.

Show the full code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Weather now</title>
</head>
<body>
  <h1>Weather now</h1>
  <p id="weather">Loading the weather…</p>
  <p><small>Weather data by Open-Meteo.com (CC BY 4.0)</small></p>

  <script>
    const URL = "https://api.open-meteo.com/v1/forecast?latitude=-35.28&longitude=149.13&current=temperature_2m,wind_speed_10m";

    async function load() {
      const box = document.querySelector("#weather");
      try {
        const response = await fetch(URL);
        const now = (await response.json()).current;
        box.textContent = "Canberra: " + now.temperature_2m + " °C, wind " + now.wind_speed_10m + " km/h";
      } catch (error) {
        box.textContent = "Couldn't reach the weather service. Check the internet and try again.";
      }
    }
    load();
  </script>
</body>
</html>

Make it yours

When it goes wrong

For teachers

Which version to send. Students who know Gradio get ?for=gradio on the link; students building a web page get ?for=web. They can switch on the page, so a wrong guess costs nothing. "Copy link to this version" copies the address with their version in it.

What this is. A recipe for a project, not a lesson: nothing is checked or recorded. The steps come first and the full code is folded under "Show the full code". It's worth noticing whether students work through the steps or open the full code straight away: either is fine, and it tells us whether the steps are useful for a task like this.

Common sticking points.

  • The school network blocks api.open-meteo.com. Check from a student laptop before the lesson; if it's blocked, the lessons linked at the top use a saved copy of a forecast instead.
  • Gradio students run this on their own computer, so they need pip install requests gradio first.
  • Reading nested data (reply["current"]["temperature_2m"], data.current.temperature_2m). Printing or logging the whole reply first helps.
  • A missing minus sign on a southern latitude.

Why Open-Meteo. It needs no account and no API key, so students never handle a secret, and it allows calls from a web page. It is free for non-commercial use with credit (CC BY 4.0); check the current terms at open-meteo.com before using it beyond class. If a student moves to a service that needs a key, the key must never go in shared code or a public web page.