typesdigital commited on
Commit
b2924a6
·
1 Parent(s): 527a822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -1,24 +1,36 @@
1
  import requests
2
- import json
 
3
 
4
- # Get an API key from https://openweathermap.org/api
5
- api_key = "1aafc3163909c1493596da9340e00aee"
 
 
 
 
6
 
7
- # Make a request to the OpenWeatherMap API
8
- url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid={}".format(api_key)
9
- response = requests.get(url)
 
10
 
11
- # Parse the response
12
- if response.status_code == 200:
13
- # The request was successful
14
- data = json.loads(response.content)
15
- temperature = data['main']['temp'] - 273.15
16
- description = data['weather'][0]['description'].title()
17
- wind_speed = data['wind']['speed']
18
- print("Temperature: {} °C".format(temperature))
19
- print("Weather Description: {}".format(description))
20
- print("Wind Speed: {} m/s".format(wind_speed))
21
- else:
22
- # The request failed
23
- print("Error: {}".format(response.json()['message']))
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import requests
2
+ import streamlit as st
3
+ from datetime import datetime
4
 
5
+ # Define function to fetch weather data
6
+ def fetch_weather_data(api_key, city):
7
+ url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
8
+ response = requests.get(url)
9
+ data = response.json()
10
+ return data
11
 
12
+ # Define function to format time
13
+ def format_time(timestamp):
14
+ dt_object = datetime.fromtimestamp(timestamp)
15
+ return dt_object.strftime("%Y-%m-%d %H:%M:%S")
16
 
17
+ # Set title
18
+ st.title("Weather App")
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # Get API key and city from user
21
+ api_key = st.text_input("1aafc3163909c1493596da9340e00aee")
22
+ city = st.text_input("Enter the name of a city")
23
+
24
+ # Fetch weather data and display it
25
+ if api_key and city:
26
+ data = fetch_weather_data(api_key, city)
27
+ if data["cod"] == 200:
28
+ st.write(f"Weather in {city}: {data['weather'][0]['description']}")
29
+ st.write(f"Temperature: {data['main']['temp']}°C")
30
+ st.write(f"Feels like: {data['main']['feels_like']}°C")
31
+ st.write(f"Humidity: {data['main']['humidity']}%")
32
+ st.write(f"Wind speed: {data['wind']['speed']} m/s")
33
+ st.write(f"Sunrise: {format_time(data['sys']['sunrise'])}")
34
+ st.write(f"Sunset: {format_time(data['sys']['sunset'])}")
35
+ else:
36
+ st.write("Invalid API key or city name")