weather_app / app.py
GenAiPA's picture
Update app.py
9e18246 verified
import gradio as gr
import requests
import config
WEATHER_KEY = so.getenv('openweather')
def get_weather(city):
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_KEY}&units=metric"
try:
response = requests.get(url)
weather_data = response.json()
# extracting weather data
tempreature = weather_data["main"]["temp"]
humidity = weather_data["main"]["humidity"]
temp_min = weather_data["main"]["temp_min"]
temp_max = weather_data["main"]["temp_max"]
feels = weather_data["main"]["feels_like"]
wind_speed = weather_data["wind"]["speed"]
# Formatting the result i want to show
result = (
f"weather in {city} \n"
f"The tempreature is {tempreature}°C\n"
f"Max Temp {temp_max}°C : Min Temp {temp_min}°C\n"
f"Feels like {feels}°C\n"
f"humidity {humidity}°C\n"
f"Wind Speed {wind_speed} m/s\n"
)
except requests.exceptions.RequestException as e:
result = f"Error occured: {str(e)}"
return result
# city_name = input("Enter the name of the city:")
# get_weather(city_name)
# weather_pa_grado = gr.Interface(fn=get_weather, inputs="text", outputs="text")
# if __name__ == "__main__":
# weather_pa_grado.launch()
weather_pa_gradio = gr.Interface(
fn=get_weather,
inputs=gr.Textbox(label = "Enter your city name to check Weather"),
outputs=gr.Textbox(label="Your City Weather"),
title = "Weather App",
description = "Weather App by Parvez Alam",
theme = "dark"
)
weather_pa_gradio.launch()