DevelopmentNovember 01, 20244 min read

Weather App using Python, Streamlit, and OpenWeatherMap API

Build a real-time weather app with Python and Streamlit using the free OpenWeatherMap API — get temperature, humidity, wind speed, and conditions for any city instantly.

Galvan

Galvan

Founder & Creator

Introduction

In this project you will build a real-time weather app using Python, Streamlit, and the free OpenWeatherMap API. Type any city name and instantly see the current temperature, humidity, wind speed, and weather description.

Prerequisites

  • Python 3.8+python.org
  • Libraries — Install with pip:
  • code
    pip install streamlit requests
  • OpenWeatherMap API Key — Sign up free at openweathermap.org and copy your key from the dashboard.
  • Step 1: Create the Script

    Create weather_app.py and paste the following:

    code
    import streamlit as st
    import requests
    
    st.set_page_config(page_title="Weather App", page_icon="🌤️")
    st.title("🌤️ Real-Time Weather App")
    
    API_KEY = "YOUR_OPENWEATHERMAP_API_KEY"
    
    city = st.text_input("Enter a city name:", placeholder="e.g. Mumbai, London, New York")
    
    def get_weather(city):
        url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
        return requests.get(url).json()
    
    if st.button("Get Weather") and city:
        data = get_weather(city)
        if data.get("cod") == 200:
            col1, col2 = st.columns(2)
            with col1:
                st.metric("🌡️ Temperature", f"{data['main']['temp']} °C")
                st.metric("💧 Humidity", f"{data['main']['humidity']}%")
            with col2:
                st.metric("💨 Wind Speed", f"{data['wind']['speed']} m/s")
                st.metric("☁️ Condition", data['weather'][0]['description'].title())
            st.success(f"Showing weather for **{data['name']}, {data['sys']['country']}**")
        else:
            st.error("City not found. Please check the spelling and try again.")

    Step 2: Add Your API Key

    Replace YOUR_OPENWEATHERMAP_API_KEY with the key you copied from the OpenWeatherMap dashboard.

    Step 3: Run the App

    code
    streamlit run weather_app.py

    Step 4: Use the App

    Type a city name and click Get Weather. The app displays current conditions in a clean two-column layout with metric cards.

    Key Concepts

  • `requests.get()` — Sends the HTTP GET request to the OpenWeatherMap API.
  • `response.json()` — Parses the JSON response into a Python dictionary.
  • `st.metric()` — Renders each weather value as a styled metric card.
  • `st.columns()` — Splits the layout into two side-by-side columns.
  • What to Try Next

  • Show a 5-day forecast using the /forecast endpoint.
  • Add weather icons from OpenWeatherMap's icon library.
  • Allow users to switch between Celsius and Fahrenheit using a toggle.