Jonas Neves commited on
Commit
cff1430
·
1 Parent(s): bc849b0

Make it compatible with HF streamlit

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +54 -19
src/streamlit_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import requests
 
3
  from dotenv import load_dotenv
4
 
5
  def get_weather(city):
@@ -7,44 +8,78 @@ def get_weather(city):
7
  load_dotenv()
8
  api_key = os.getenv('OPENWEATHER_API_KEY')
9
 
 
 
 
 
10
  base_url = "http://api.openweathermap.org/data/2.5/weather"
11
  params = {
12
  'q': city,
13
- 'appid': api_key
 
14
  }
15
 
16
- response = requests.get(base_url, params=params)
17
- return response.json()
 
 
 
 
 
18
 
19
  def display_weather(weather_data):
20
- """Display formatted weather information"""
21
-
22
- temp_celsius = weather_data['main']['temp']
 
 
 
 
 
 
 
23
  humidity = weather_data['main']['humidity']
24
  description = weather_data['weather'][0]['description'].title()
25
 
26
  city_name = weather_data['name']
27
  country = weather_data['sys']['country']
28
 
29
- # Display weather report
30
- print("\n" + "="*50)
31
- print(f"🌤️ WEATHER REPORT FOR {city_name.upper()}, {country}")
32
- print("="*50)
33
- print(f"🌡️ Temperature: {temp_celsius}°C")
34
- print(f"💧 Humidity: {humidity}%")
35
- print(f" Description: {description}")
36
- print("="*50)
 
 
 
 
 
37
 
38
  def main():
39
  """Main application function"""
40
- print("🌤️ Welcome to Weather CLI App!")
 
 
 
 
41
 
42
- city = input("🏙 Enter a city name: ").strip()
 
43
 
44
- print(f"🔍 Fetching weather data for {city}...")
 
 
 
45
 
46
- weather_data = get_weather(city)
47
- display_weather(weather_data)
 
 
 
 
48
 
49
  if __name__ == "__main__":
50
  main()
 
1
  import os
2
  import requests
3
+ import streamlit as st
4
  from dotenv import load_dotenv
5
 
6
  def get_weather(city):
 
8
  load_dotenv()
9
  api_key = os.getenv('OPENWEATHER_API_KEY')
10
 
11
+ if not api_key:
12
+ st.error("⚠️ OpenWeather API key not found. Please set OPENWEATHER_API_KEY in your environment variables.")
13
+ return None
14
+
15
  base_url = "http://api.openweathermap.org/data/2.5/weather"
16
  params = {
17
  'q': city,
18
+ 'appid': api_key,
19
+ 'units': 'imperial'
20
  }
21
 
22
+ try:
23
+ response = requests.get(base_url, params=params)
24
+ response.raise_for_status()
25
+ return response.json()
26
+ except requests.exceptions.RequestException as e:
27
+ st.error(f"Error fetching weather data: {e}")
28
+ return None
29
 
30
  def display_weather(weather_data):
31
+ """Display formatted weather information in Streamlit"""
32
+ if not weather_data:
33
+ return
34
+
35
+ if 'main' not in weather_data:
36
+ st.error("Invalid response from weather API. Please check the city name.")
37
+ return
38
+
39
+ temp_fahrenheit = weather_data['main']['temp']
40
+ feels_like = weather_data['main']['feels_like']
41
  humidity = weather_data['main']['humidity']
42
  description = weather_data['weather'][0]['description'].title()
43
 
44
  city_name = weather_data['name']
45
  country = weather_data['sys']['country']
46
 
47
+ # Display weather report using Streamlit components
48
+ st.success(f"Weather data retrieved for {city_name}, {country}")
49
+
50
+ col1, col2, col3 = st.columns(3)
51
+
52
+ with col1:
53
+ st.metric("🌡Temperature", f"{temp_fahrenheit}°F", f"Feels like {feels_like}°F")
54
+
55
+ with col2:
56
+ st.metric("💧 Humidity", f"{humidity}%")
57
+
58
+ with col3:
59
+ st.metric("☁️ Condition", description)
60
 
61
  def main():
62
  """Main application function"""
63
+ st.set_page_config(
64
+ page_title="Weather App",
65
+ page_icon="🌤️",
66
+ layout="wide"
67
+ )
68
 
69
+ st.title("🌤Weather CLI App")
70
+ st.markdown("Get current weather information for any city!")
71
 
72
+ # Create input form
73
+ with st.form("weather_form"):
74
+ city = st.text_input("🏙️ Enter a city name:", placeholder="e.g., London, Tokyo, New York")
75
+ submit_button = st.form_submit_button("Get Weather")
76
 
77
+ if submit_button and city:
78
+ with st.spinner(f"🔍 Fetching weather data for {city}..."):
79
+ weather_data = get_weather(city.strip())
80
+ display_weather(weather_data)
81
+ elif submit_button and not city:
82
+ st.warning("Please enter a city name!")
83
 
84
  if __name__ == "__main__":
85
  main()