Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
-
from datetime import datetime
|
| 4 |
|
| 5 |
# OpenWeather API details
|
| 6 |
BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
|
|
@@ -13,10 +12,10 @@ def get_weather(city):
|
|
| 13 |
|
| 14 |
# Error handling
|
| 15 |
if response.get("cod") != "200":
|
| 16 |
-
return "β
|
| 17 |
|
| 18 |
weather_info = {}
|
| 19 |
-
|
| 20 |
# Extracting one data point per day
|
| 21 |
for entry in response["list"]:
|
| 22 |
date = entry["dt_txt"].split(" ")[0] # Extract only the date part
|
|
@@ -29,51 +28,90 @@ def get_weather(city):
|
|
| 29 |
"Rain": f"{entry.get('rain', {}).get('3h', 0)} mm",
|
| 30 |
"Cloud Cover": f"{entry['clouds']['all']}%" # Extracting cloud data
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
# Format output as
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
for date, details in weather_info.items():
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
custom_css = """
|
| 45 |
body {
|
| 46 |
-
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
|
| 47 |
background-size: cover;
|
| 48 |
-
color: black;
|
| 49 |
}
|
| 50 |
-
|
| 51 |
.gradio-container {
|
| 52 |
display: flex;
|
| 53 |
justify-content: center;
|
| 54 |
align-items: center;
|
| 55 |
height: 100vh;
|
| 56 |
}
|
| 57 |
-
|
| 58 |
.weather-box {
|
| 59 |
-
background: rgba(255, 255, 255, 0.
|
| 60 |
padding: 20px;
|
| 61 |
border-radius: 10px;
|
| 62 |
-
width:
|
| 63 |
-
max-width:
|
| 64 |
-
box-shadow: 0px
|
| 65 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
"""
|
| 71 |
|
| 72 |
# Gradio Interface
|
| 73 |
iface = gr.Interface(
|
| 74 |
fn=get_weather,
|
| 75 |
inputs=gr.Textbox(label="π Enter City Name"),
|
| 76 |
-
outputs=
|
| 77 |
title="π€οΈ Weather Forecast",
|
| 78 |
description="Enter a city name to get temperature, humidity, pressure, wind speed, rain, and cloud cover for the next **5 days**.",
|
| 79 |
theme="default",
|
|
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
# OpenWeather API details
|
| 5 |
BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
|
|
|
|
| 12 |
|
| 13 |
# Error handling
|
| 14 |
if response.get("cod") != "200":
|
| 15 |
+
return "<div class='weather-box'>β <b>Invalid city name! Please enter a correct city.</b></div>"
|
| 16 |
|
| 17 |
weather_info = {}
|
| 18 |
+
|
| 19 |
# Extracting one data point per day
|
| 20 |
for entry in response["list"]:
|
| 21 |
date = entry["dt_txt"].split(" ")[0] # Extract only the date part
|
|
|
|
| 28 |
"Rain": f"{entry.get('rain', {}).get('3h', 0)} mm",
|
| 29 |
"Cloud Cover": f"{entry['clouds']['all']}%" # Extracting cloud data
|
| 30 |
}
|
| 31 |
+
|
| 32 |
+
# Format output as an HTML table with dark, bold text
|
| 33 |
+
table_html = """
|
| 34 |
+
<div class='weather-box'>
|
| 35 |
+
<h2>π
5-Day Weather Forecast for {}</h2>
|
| 36 |
+
<table>
|
| 37 |
+
<tr>
|
| 38 |
+
<th>Date</th>
|
| 39 |
+
<th>Temperature</th>
|
| 40 |
+
<th>Humidity</th>
|
| 41 |
+
<th>Pressure</th>
|
| 42 |
+
<th>Wind Speed</th>
|
| 43 |
+
<th>Rain</th>
|
| 44 |
+
<th>Cloud Cover</th>
|
| 45 |
+
</tr>
|
| 46 |
+
""".format(city)
|
| 47 |
|
| 48 |
for date, details in weather_info.items():
|
| 49 |
+
table_html += f"""
|
| 50 |
+
<tr>
|
| 51 |
+
<td>{date}</td>
|
| 52 |
+
<td>{details['Temperature']}</td>
|
| 53 |
+
<td>{details['Humidity']}</td>
|
| 54 |
+
<td>{details['Pressure']}</td>
|
| 55 |
+
<td>{details['Wind Speed']}</td>
|
| 56 |
+
<td>{details['Rain']}</td>
|
| 57 |
+
<td>{details['Cloud Cover']}</td>
|
| 58 |
+
</tr>
|
| 59 |
+
"""
|
| 60 |
|
| 61 |
+
table_html += "</table></div>"
|
| 62 |
|
| 63 |
+
return table_html
|
| 64 |
+
|
| 65 |
+
# **Adding Dark-Themed Styling**
|
| 66 |
custom_css = """
|
| 67 |
body {
|
| 68 |
+
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') no-repeat center center fixed;
|
| 69 |
background-size: cover;
|
| 70 |
+
color: black; /* Set text to black */
|
| 71 |
}
|
|
|
|
| 72 |
.gradio-container {
|
| 73 |
display: flex;
|
| 74 |
justify-content: center;
|
| 75 |
align-items: center;
|
| 76 |
height: 100vh;
|
| 77 |
}
|
|
|
|
| 78 |
.weather-box {
|
| 79 |
+
background: rgba(255, 255, 255, 0.85); /* White with slight transparency */
|
| 80 |
padding: 20px;
|
| 81 |
border-radius: 10px;
|
| 82 |
+
width: 85%;
|
| 83 |
+
max-width: 700px;
|
| 84 |
+
box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.4);
|
| 85 |
text-align: center;
|
| 86 |
+
font-weight: bold;
|
| 87 |
+
}
|
| 88 |
+
.weather-box table {
|
| 89 |
+
width: 100%;
|
| 90 |
+
border-collapse: collapse;
|
| 91 |
+
font-weight: bold;
|
| 92 |
+
}
|
| 93 |
+
.weather-box th, .weather-box td {
|
| 94 |
+
border: 2px solid black;
|
| 95 |
+
padding: 10px;
|
| 96 |
+
text-align: center;
|
| 97 |
+
font-weight: bold;
|
| 98 |
+
color: black;
|
| 99 |
+
}
|
| 100 |
+
.weather-box th {
|
| 101 |
+
background-color: black;
|
| 102 |
+
color: white;
|
| 103 |
+
}
|
| 104 |
+
h2 {
|
| 105 |
+
color: black;
|
| 106 |
+
font-weight: bold;
|
| 107 |
}
|
|
|
|
|
|
|
|
|
|
| 108 |
"""
|
| 109 |
|
| 110 |
# Gradio Interface
|
| 111 |
iface = gr.Interface(
|
| 112 |
fn=get_weather,
|
| 113 |
inputs=gr.Textbox(label="π Enter City Name"),
|
| 114 |
+
outputs="html",
|
| 115 |
title="π€οΈ Weather Forecast",
|
| 116 |
description="Enter a city name to get temperature, humidity, pressure, wind speed, rain, and cloud cover for the next **5 days**.",
|
| 117 |
theme="default",
|