Spaces:
Sleeping
Sleeping
File size: 2,709 Bytes
1505b55 71961f4 1505b55 71961f4 1505b55 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import requests
import os
from dotenv import load_dotenv
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# Load your API key from .env file
load_dotenv()
API_KEY = os.getenv("OPENWEATHER_API_KEY")
def get_weatherData(city, unit):
url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": API_KEY,
"units": unit
}
response = requests.get(url, params=params)
data = response.json()
return data
# Up to 40 hours worth, 3-hour intervals for 5 days
def get_dailyForecast(city, unit, count):
url = "https://api.openweathermap.org/data/2.5/forecast?"
params = {
"q": city,
"appid": API_KEY,
"units": unit,
"cnt": count
}
response = requests.get(url, params = params)
data = response.json()
return data["list"]
def forecastToDF(data_list):
temps = []
times = []
for item in data_list:
times.append(item["dt_txt"])
temps.append(item["main"]["temp"])
df = pd.DataFrame({"Time": times, "Temperature": temps})
df["Time"] = pd.to_datetime(df["Time"])
return df
def get_weather(data, units):
city_name = data["name"]
temp = data["main"]["temp"]
humidity = data["main"]["humidity"]
description = data["weather"][0]["description"]
if (units == "Kelvin"):
return f"In {city_name}, it is {temp} {units[0]} with {description}"
else:
return f"In {city_name}, it is {temp} \u00B0{units[0]} with {description}"
def get_icon(data):
icon_code = data["weather"][0]["icon"]
return f"http://openweathermap.org/img/wn/{icon_code}@2x.png"
# STREAMLIT SECTION STARTS HERE
st.title("Weather App")
st.sidebar.header("Weather Settings")
# Sidebar configs (User input)
city = st.sidebar.text_input(label="City", value="Durham")
units = st.sidebar.selectbox("Units:", ["Kelvin", "Celsius", "Fahrenheit"])
unit_map = {"Celsius": "metric", "Fahrenheit": "imperial", "Kelvin": "standard"}
count = st.sidebar.slider("Forecast Step (3-hour intervals)", 1, 40, 20)
# Data retrieval
data = get_weatherData(city, unit_map[units])
data_list = get_dailyForecast(city, unit_map[units], count)
icon_url = get_icon(data)
# Displaying information
st.image(icon_url, width=100)
st.write(get_weather(data, units))
df = forecastToDF(data_list)
# line plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(df["Time"], df["Temperature"], marker="o", linestyle="-", color="r")
ax.set_title(f"Temperature Forecast for {city} (3-Hour Intervals)", fontsize=14)
ax.set_xlabel("Time", fontsize=12)
ax.set_ylabel(f"Temperature ({units})", fontsize=12)
plt.xticks(rotation=90)
st.pyplot(fig)
|