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
code
pip install streamlit requestsStep 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.pyStep 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
What to Try Next
/forecast endpoint.