Spaces:
Runtime error
Runtime error
import requests | |
import streamlit as st | |
from datetime import datetime | |
# Define function to fetch weather data | |
def fetch_weather_data(api_key, city): | |
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" | |
response = requests.get(url) | |
data = response.json() | |
return data | |
# Define function to format time | |
def format_time(timestamp): | |
dt_object = datetime.fromtimestamp(timestamp) | |
return dt_object.strftime("%Y-%m-%d %H:%M:%S") | |
# Set title | |
st.title("Weather App") | |
# Get API key and city from user | |
api_key = st.text_input("1aafc3163909c1493596da9340e00aee") | |
city = st.text_input("Enter the name of a city") | |
# Fetch weather data and display it | |
if api_key and city: | |
data = fetch_weather_data(api_key, city) | |
if data["cod"] == 200: | |
st.write(f"Weather in {city}: {data['weather'][0]['description']}") | |
st.write(f"Temperature: {data['main']['temp']}°C") | |
st.write(f"Feels like: {data['main']['feels_like']}°C") | |
st.write(f"Humidity: {data['main']['humidity']}%") | |
st.write(f"Wind speed: {data['wind']['speed']} m/s") | |
st.write(f"Sunrise: {format_time(data['sys']['sunrise'])}") | |
st.write(f"Sunset: {format_time(data['sys']['sunset'])}") | |
else: | |
st.write("Invalid API key or city name") | |