deadshot2003 commited on
Commit
7e3af18
·
verified ·
1 Parent(s): 4571bd2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+ # Load environment variables from .env file
7
+ load_dotenv()
8
+
9
+ # Get OpenWeatherMap API key from environment variables
10
+ API_KEY = os.getenv('OPENWEATHERMAP_API_KEY')
11
+
12
+ # Function to get weather data
13
+ def get_weather(city):
14
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
15
+ response = requests.get(url)
16
+ if response.status_code == 200:
17
+ return response.json()
18
+ else:
19
+ return None
20
+
21
+ # Function to get the appropriate weather icon
22
+ def get_weather_icon(weather_description):
23
+ if 'rain' in weather_description:
24
+ return 'fa-cloud-showers-heavy'
25
+ elif 'clear' in weather_description:
26
+ return 'fa-sun'
27
+ elif 'cloud' in weather_description:
28
+ return 'fa-cloud'
29
+ elif 'thunderstorm' in weather_description:
30
+ return 'fa-bolt'
31
+ elif 'snow' in weather_description:
32
+ return 'fa-snowflake'
33
+ else:
34
+ return 'fa-smog'
35
+
36
+ # Streamlit UI
37
+ def main():
38
+ st.set_page_config(page_title="Current Weather App", page_icon=":sun_behind_rain_cloud:", layout="wide")
39
+
40
+ st.markdown(
41
+ """
42
+ <style>
43
+ @import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css');
44
+
45
+ .main {
46
+ text-align: center;
47
+ }
48
+ .stButton button {
49
+ width: 100%;
50
+ text-align: center;
51
+ }
52
+ .weather-icon {
53
+ font-size: 100px;
54
+ margin: 20px 0;
55
+ }
56
+ .city-text {
57
+ font-size: 24px;
58
+ }
59
+ .condition-text {
60
+ font-size: 18px;
61
+ }
62
+ </style>
63
+ """,
64
+ unsafe_allow_html=True,
65
+ )
66
+
67
+ st.markdown("<h1 style='text-align: center;'>Current Weather App</h1>", unsafe_allow_html=True)
68
+
69
+ city = st.text_input('Enter City Name', 'New York').strip()
70
+
71
+ if st.button('Get Weather'):
72
+ weather = get_weather(city)
73
+ if weather:
74
+ weather_description = weather['weather'][0]['description']
75
+ weather_icon_class = get_weather_icon(weather_description)
76
+
77
+ st.markdown(f"<h3 class='city-text' style='text-align: center;'>City: {weather['name']}</h3>", unsafe_allow_html=True)
78
+ st.markdown(f"<div class='weather-icon' style='text-align: center;'><i class='fas {weather_icon_class}'></i></div>", unsafe_allow_html=True)
79
+ st.markdown(f"<h4 class='condition-text' style='text-align: center;'>Temperature: {weather['main']['temp']}°C</h4>", unsafe_allow_html=True)
80
+ st.markdown(f"<h4 class='condition-text' style='text-align: center;'>Weather: {weather_description.capitalize()}</h4>", unsafe_allow_html=True)
81
+ st.markdown(f"<h4 class='condition-text' style='text-align: center;'>Humidity: {weather['main']['humidity']}%</h4>", unsafe_allow_html=True)
82
+ st.markdown(f"<h4 class='condition-text' style='text-align: center;'>Wind Speed: {weather['wind']['speed']} m/s</h4>", unsafe_allow_html=True)
83
+ else:
84
+ st.error("City not found or API error.")
85
+
86
+ if __name__ == '__main__':
87
+ main()