π€οΈ Building a Weather App with Python + Streamlit (Using wttr.in API)
For my next project, I wanted to work with a public API and learn how to fetch external data in Python.
So I built a simple Weather App using wttr.in, which provides weather data without requiring an API key.
This project has two versions:
- Console Version (weather.py)
- Streamlit Web Version (weather_streamlit.py)
This small project helped me learn how to make API requests, handle JSON data, and build a clean UI with Streamlit.
π§± 1. Project Overview
The Weather App allows you to:
- Enter a city name
- Fetch live weather using wttr.in
- View temperature, condition, humidity
- Use it via console or a Streamlit web UI
Simple, clean, and useful β exactly what I want for my Python practice.
π Project Structure
weather_app/ β βββ weather.py # Console version βββ weather_streamlit.py # Streamlit version βββ README.md # (optional)
π 2. API Used: wttr.in
I chose wttr.in because it:
- Requires no signup
- Returns JSON easily
- Works well for quick projects
π₯οΈ 3. Console Version (weather.py)
Key features:
- Requests weather using
requestslibrary - Shows temperature, weather description, humidity
- Gracefully handles errors
π Code
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"], "%")
π€οΈ 4. Streamlit Web Version (weather_streamlit.py)
Why Streamlit?
Because Streamlit turns Python scripts into fast, interactive web apps β perfect for tools like this.
Added features:
- Clean web UI
- User-friendly city input
- Error messages
- Styled weather data output
π Code
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)
python
βοΈ 5. How to Run
β Console Version
python3 weather.py
β Streamlit Version
pip install streamlit
streamlit run weather_streamlit.py
Streamlit will automatically open a browser window for the web UI.
π 6. What I Learned
- How to make API requests with
requests - How to parse JSON responses
- How to build an interactive UI with Streamlit
- Error handling for network responses
- How to convert a console script into a web app
π§ 7. Future Improvements
- Add weather icons
- Show hourly + weekly forecast
- Support multiple languages
- Add background theme (sunny, cloudy, rainy)
- Allow saving favorite cities
Top comments (0)