KAPtechies commited on
Commit
3546322
Β·
verified Β·
1 Parent(s): 4809bde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -60
app.py CHANGED
@@ -5,7 +5,6 @@ import pandas as pd
5
  # OpenWeather API details
6
  BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
7
  API_KEY = "2682f6801720ebeb43c93ad55c826c5c"
8
- ICON_URL = "http://openweathermap.org/img/wn/"
9
 
10
  def get_weather(city):
11
  url = f"{BASE_URL}q={city}&appid={API_KEY}&units=metric"
@@ -18,108 +17,86 @@ def get_weather(city):
18
 
19
  # Organizing data by date
20
  for entry in response["list"]:
21
- date = entry["dt_txt"].split(" ")[0]
22
- time = entry["dt_txt"].split(" ")[1]
23
- temp = f"{entry['main']['temp']}Β°C"
24
- humidity = f"{entry['main']['humidity']}%"
25
- pressure = f"{entry['main']['pressure']} hPa"
26
- wind_speed = f"{entry['wind']['speed']} m/s"
27
- rain = f"{entry['rain']['3h']} mm" if "rain" in entry and "3h" in entry["rain"] else "0 mm"
28
- cloud_cover = f"{entry['clouds']['all']}%"
29
- icon = f"{ICON_URL}{entry['weather'][0]['icon']}@2x.png" # Fetch weather icon
30
-
31
  if date not in weather_info:
32
  weather_info[date] = []
33
 
34
- weather_info[date].append([time, temp, humidity, pressure, wind_speed, rain, cloud_cover, icon])
 
 
 
 
 
 
 
 
35
 
36
- markdown_output = """
37
- <h2 style="text-align:center; color: #fff; background: linear-gradient(to right, #667eea, #764ba2); padding: 10px; border-radius: 10px;">
38
- πŸ“… 5-Day Weather Forecast
39
- </h2>
40
- """
41
 
42
  for date, details in weather_info.items():
43
- df = pd.DataFrame(details, columns=["Time", "Temperature", "Humidity", "Pressure", "Wind Speed", "Rain", "Cloud Cover", "Icon"])
44
- df_html = df.to_html(index=False, escape=False) # Disable escaping for images
45
-
46
- # Convert "Icon" column to actual images
47
- df_html = df_html.replace("&lt;img", "<img").replace("&gt;", ">") # Fix HTML escaping for images
48
 
49
  markdown_output += f"""
50
  <div class='weather-box'>
51
- <h3 class="date-header">πŸ“† {date}</h3>
52
- {df_html}
53
  </div>
54
  """
55
 
56
  return markdown_output
57
 
58
- # **CSS for styling**
59
  custom_css = """
60
  body {
61
- background: url('https://images.unsplash.com/photo-1548337138-e87d889cc369?q=80&w=1796&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D.jpg') no-repeat center center fixed;
62
- color: white;
63
- font-family: 'Poppins', sans-serif;
64
  }
65
  .gradio-container {
66
- background: rgba(255, 255, 255, 0.15);
67
  padding: 20px;
68
  border-radius: 10px;
69
- box-shadow: 4px 4px 10px rgba(255, 255, 255, 0.2);
70
  }
71
- h1, h2, h3 {
 
72
  text-align: center;
73
  }
74
  .weather-box {
75
- background: rgba(255, 255, 255, 0.2);
76
  border-radius: 10px;
77
  padding: 15px;
78
  margin-bottom: 10px;
79
- box-shadow: 5px 5px 15px rgba(255, 255, 255, 0.3);
80
  }
81
- .styled-table {
82
  width: 100%;
83
  border-collapse: collapse;
84
- background: #222;
85
- color: white;
86
- margin-top: 10px;
87
- border-radius: 10px;
88
- overflow: hidden;
89
- box-shadow: 0px 3px 10px rgba(255, 255, 255, 0.2);
90
  }
91
- .styled-table th, .styled-table td {
92
- border: 1px solid rgba(255, 255, 255, 0.2);
93
- padding: 10px;
94
  text-align: center;
95
  }
96
- .styled-table th {
97
- background: #4a5568;
98
- color: #fff;
99
- font-weight: bold;
100
- }
101
- .styled-table tbody tr {
102
- transition: all 0.3s ease-in-out;
103
- }
104
- .styled-table tbody tr:hover {
105
- background: rgba(255, 255, 255, 0.1);
106
- }
107
- .styled-table img {
108
- width: 50px; /* Weather icons size */
109
- height: 50px;
110
  }
111
  """
112
 
113
- # **Gradio Interface**
114
  iface = gr.Interface(
115
  fn=get_weather,
116
  inputs=gr.Textbox(label="🌍 Enter City Name"),
117
  outputs=gr.HTML(),
118
- title="🌀️ Weather Forecast",
119
  description="Enter a city name to get **temperature, humidity, wind speed, and cloud cover** for the next **5 days** (grouped by date).",
120
  theme="default",
121
  css=custom_css
122
  )
123
 
124
  if __name__ == "__main__":
125
- iface.launch()
 
5
  # OpenWeather API details
6
  BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
7
  API_KEY = "2682f6801720ebeb43c93ad55c826c5c"
 
8
 
9
  def get_weather(city):
10
  url = f"{BASE_URL}q={city}&appid={API_KEY}&units=metric"
 
17
 
18
  # Organizing data by date
19
  for entry in response["list"]:
20
+ date = entry["dt_txt"].split(" ")[0] # Extract only the date
21
+ time = entry["dt_txt"].split(" ")[1] # Extract only the time
 
 
 
 
 
 
 
 
22
  if date not in weather_info:
23
  weather_info[date] = []
24
 
25
+ weather_info[date].append([
26
+ time,
27
+ f"{entry['main']['temp']}Β°C",
28
+ f"{entry['main']['humidity']}%",
29
+ f"{entry['main']['pressure']} hPa",
30
+ f"{entry['wind']['speed']} m/s",
31
+ f"{entry['rain']['3h']} mm" if "rain" in entry and "3h" in entry["rain"] else "0 mm",
32
+ f"{entry['clouds']['all']}%"
33
+ ])
34
 
35
+ # Formatting output as tables inside boxes
36
+ markdown_output = "<h2>πŸ“… 5-Day Weather Forecast</h2>"
 
 
 
37
 
38
  for date, details in weather_info.items():
39
+ df = pd.DataFrame(details, columns=["Time", "Temperature", "Humidity", "Pressure", "Wind Speed", "Rain", "Cloud Cover"])
40
+ table_html = df.to_html(index=False)
 
 
 
41
 
42
  markdown_output += f"""
43
  <div class='weather-box'>
44
+ <h3>πŸ“† {date}</h3>
45
+ {table_html}
46
  </div>
47
  """
48
 
49
  return markdown_output
50
 
51
+ # **Adding Background Image and Box Styling**
52
  custom_css = """
53
  body {
54
+ background: url('https://images.unsplash.com/photo-1548337138-e87d889cc369?q=80&w=1796&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D.jpg') no-repeat center center fixed;
55
+ background-size: cover;
56
+ color: black;
57
  }
58
  .gradio-container {
59
+ background: rgba(255, 255, 255, 0.6);
60
  padding: 20px;
61
  border-radius: 10px;
 
62
  }
63
+ h1 {
64
+ color: black;
65
  text-align: center;
66
  }
67
  .weather-box {
68
+ background: rgba(255, 255, 255, 0.9);
69
  border-radius: 10px;
70
  padding: 15px;
71
  margin-bottom: 10px;
72
+ box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
73
  }
74
+ table {
75
  width: 100%;
76
  border-collapse: collapse;
77
+ background: white;
 
 
 
 
 
78
  }
79
+ th, td {
80
+ border: 1px solid black;
81
+ padding: 8px;
82
  text-align: center;
83
  }
84
+ th {
85
+ background: #4CAF50;
86
+ color: white;
 
 
 
 
 
 
 
 
 
 
 
87
  }
88
  """
89
 
90
+ # Gradio Interface
91
  iface = gr.Interface(
92
  fn=get_weather,
93
  inputs=gr.Textbox(label="🌍 Enter City Name"),
94
  outputs=gr.HTML(),
95
+ title="🌀️Climate change model to forecast future weather patterns",
96
  description="Enter a city name to get **temperature, humidity, wind speed, and cloud cover** for the next **5 days** (grouped by date).",
97
  theme="default",
98
  css=custom_css
99
  )
100
 
101
  if __name__ == "__main__":
102
+ iface.launch()