DEV Community

Cover image for My Project 3: Building a Weather App with Python + Streamlit
Sabin Sim
Sabin Sim

Posted on

My Project 3: Building a Weather App with Python + Streamlit

🌀️ 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 requests library
  • 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"], "%")
Enter fullscreen mode Exit fullscreen mode

🌀️ 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)
Enter fullscreen mode Exit fullscreen mode


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)