내 프로젝트 3: Python + Streamlit으로 날씨 앱 만들기

발행: (2025년 12월 1일 오전 07:26 GMT+9)
3 분 소요
원문: Dev.to

Source: Dev.to

1. Project Overview

  • 도시 이름 입력
  • wttr.in을 사용해 실시간 날씨 가져오기
  • 온도, 상태, 습도 확인
  • 콘솔 스크립트(weather.py) 또는 Streamlit 웹 UI(weather_streamlit.py)로 제공

간단하고 깔끔하며 유용함 — 파이썬 연습에 딱 맞는 프로젝트.

2. API Used: wttr.in

wttr.in을 선택한 이유:

  • 회원가입이 필요 없음
  • JSON 반환이 간편함
  • 빠른 프로젝트에 적합

3. Console Version (weather.py)

Key Features

  • requests 라이브러리를 이용해 날씨 요청
  • 온도, 날씨 설명, 습도 표시
  • 오류를 우아하게 처리

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?

  • 깔끔한 웹 UI
  • 사용자 친화적인 도시 입력
  • 명확한 오류 메시지
  • 스타일이 적용된 날씨 데이터 출력

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: **{temp}°C**", unsafe_allow_html=True)
        st.write(f"☁ Condition: **{desc}**", unsafe_allow_html=True)
        st.write(f"💧 Humidity: **{humidity}%**", unsafe_allow_html=True)

5. How to Run

Console Version

python3 weather.py

Streamlit Version

pip install streamlit
streamlit run weather_streamlit.py

Streamlit이 자동으로 브라우저 창을 열어 웹 UI를 표시합니다.

6. What I Learned

  • requests를 이용한 API 요청 방법
  • JSON 응답 파싱
  • Streamlit으로 인터랙티브 UI 구축
  • 네트워크 응답에 대한 오류 처리
  • 콘솔 스크립트를 웹 앱으로 변환하는 과정

7. Future Improvements

  • 날씨 아이콘 추가
  • 시간별·주간 예보 표시
  • 다국어 지원
  • 배경 테마 추가(맑음, 흐림, 비)
  • 즐겨 찾는 도시 저장 기능
Back to Blog

관련 글

더 보기 »

Day 1276 : 커리어 클라이밍

토요일 역으로 가기 전에, 현재 진행 중인 사이드 프로젝트에서 코딩을 했어요. 꽤 좋은 진전을 이루었고, 이제 나갈 시간이었어요. Made i...

Stateless AI 애플리케이션의 아키텍처

프로젝트는 위험해 보이는 결정으로 시작되었습니다: 백엔드 데이터베이스를 사용하지 않는 것이었습니다. 당시에는 사용자 데이터를 영구 저장할 필요가 없었으며—사용자의 응답을 얻는 것이...

JWT 토큰 검증기 챌린지

개요 2019년에 Django의 세션 관리 프레임워크에 미묘하지만 치명적인 취약점 CVE‑2019‑11358이 존재했습니다. 프레임워크는 적절하게 inv...