Building a Weather App with Python + Streamlit: wttr.in API Tutorial
These articles are AI-generated summaries. Please check the original sources for full details.
Building a Weather App with Python + Streamlit (Using wttr.in API)
Sabin Sim built a weather app using Python and Streamlit, leveraging the wttr.in API for live weather data without API keys. The project includes both console and web versions with JSON parsing and error handling.
Why This Matters
The ideal model assumes seamless API access, but real-world systems face challenges like inconsistent data formats and network errors. For example, wttr.in returns JSON but requires strict parsing—missing a key like current_condition would crash the app. Error handling (e.g., checking response.status_code) is critical to avoid silent failures in production.
Key Insights
- “wttr.in requires no API key, 2025”: Ideal for quick prototyping but lacks advanced features like forecasts.
- “Streamlit for rapid web apps”: Used by developers to turn Python scripts into interactive dashboards with minimal code.
- “JSON parsing edge cases”: The
current_conditionarray must be indexed properly ([0]) to avoidIndexError.
Working Example
# Console Version (weather.py)
import requests
city = input("Enter city name: ")
url = f"https://wttr.in/{city}?format=j1"
response = requests.get(url)
if response.status_code != 200:
print("Error fetching weather data.")
exit()
data = response.json()
current = data["current_condition"][0]
print("=== Weather Report ===")
print("City:", city)
print("Temperature:", current["temp_C"], "°C")
print("Weather:", current["weatherDesc"][0]["value"])
print("Humidity:", current["humidity"], "%")
# Streamlit Version (weather_streamlit.py)
import streamlit as st
import requests
st.title("🌤️ Sabin's Weather App")
city = st.text_input("Enter city name:", "Chur")
if st.button("Check Weather"):
url = f"https://wttr.in/{city}?format=j1"
response = requests.get(url)
if response.status_code != 200:
st.error("Error fetching weather data.")
else:
data = response.json()
current = data["current_condition"][0]
temp = current["temp_C"]
desc = current["weatherDesc"][0]["value"]
humidity = current["humidity"]
st.subheader(f"Weather in {city}")
st.write(f"🌡 Temperature: <strong>{temp}°C</strong>", unsafe_allow_html=True)
st.write(f"☁ Condition: <strong>{desc}</strong>", unsafe_allow_html=True)
st.write(f"💧 Humidity: <strong>{humidity}%</strong>", unsafe_allow_html=True)
Practical Applications
- Use Case: A lightweight dashboard for personal weather tracking using Streamlit.
- Pitfall: Forgetting to handle
response.status_codemay lead to uncaught exceptions in production.
References:
Continue reading
Next article
One-command ComfyUI on Cloud GPUs: A Practical, Repeatable Setup
Related Content
Building a Movie Search App with Python and Streamlit: A Practical Guide
A Python-based movie search app using Streamlit and OMDb API, demonstrating API integration and UI design.
From Swagger to Tests: Building an AI-Powered API Test Generator with Python
This project automates API test generation from Swagger specifications using Gemini AI, reducing manual effort by up to 80%.
Building a Python-Based Hacker Terminal for Cybersecurity Learning
Developer Eidolmor launches a terminal-based hacker simulation in Python to bridge the gap between cybersecurity theory and practical implementation using modular game logic.