ankita0512ghosh commited on
Commit
64db85d
·
1 Parent(s): e0f58e4
Files changed (2) hide show
  1. app.py +83 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ import json
4
+
5
+ def get_weather(city):
6
+ """Gets the current weather forecast for the given city."""
7
+
8
+ # Get the API key from OpenWeatherMap.
9
+ API_KEY = "58bb081f22fea521a4a3cd7ccb24aa88"
10
+
11
+ # Make a request to the OpenWeatherMap API.
12
+ response = requests.get(
13
+ "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}".format(city, API_KEY)
14
+ )
15
+
16
+ # Check for errors.
17
+ if response.status_code != 200:
18
+ raise Exception("Error getting weather data: {}".format(response.status_code))
19
+
20
+ # Parse the JSON response.
21
+ weather_data = json.loads(response.content.decode("utf-8"))
22
+
23
+ # Return the current weather forecast.
24
+ return weather_data["weather"][0]["description"], weather_data["main"]["temp"], weather_data["main"]["pressure"], weather_data["main"]["humidity"]
25
+
26
+ #main function
27
+ if __name__ == "__main__":
28
+
29
+ # Create a title for the app.
30
+ st.title("Weather Forecast")
31
+
32
+ # Get the city name from the user.
33
+ city = st.text_input("Enter a city name: ")
34
+
35
+ # Show the weather forecast for the city.
36
+ if city:
37
+ weather_description, temperature, pressure, humidity = get_weather(city)
38
+
39
+ # Add a background image.
40
+ st.markdown(f"""<style>.stApp {{
41
+ background-image: url("https://images.unsplash.com/photo-1474540412665-1cdae210ae6b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8Y2FsbXxlbnwwfHwwfHx8MA%3D%3D&w=1000&q=80");
42
+ background-attachment: fixed;
43
+ background-size: cover
44
+ }}</style>""",unsafe_allow_html=True)
45
+
46
+ # Add a heading.
47
+ st.header("Weather in **{}** ".format(city))
48
+
49
+ # Add a paragraph.
50
+ st.markdown("The weather in **{}** is **{}** and the temperature is **{}** Kelvin Unit.".format(city, weather_description, temperature))
51
+
52
+ col1, col2, col3 = st.columns(3)
53
+
54
+ # Add a button to convert the temperature to Celsius.
55
+ with col1:
56
+ convert_to_celsius = st.button("Convert to Celsius")
57
+
58
+ if convert_to_celsius:
59
+ temperature_in_celsius = float("{:.2f}".format(temperature - 273.15))
60
+ st.markdown(
61
+ f"""
62
+ The temperature in **{city}** is **{weather_description}** and the temperature is **{temperature_in_celsius}** degrees Celsius.
63
+ """
64
+ )
65
+
66
+ #Add button to convert the temperature to Fahrenheit
67
+ with col2:
68
+ convert_to_fahrenheit = st.button("Convert to Fahrenheit")
69
+
70
+ if convert_to_fahrenheit:
71
+ temperature_in_fahrenheit = float("{:.2f}".format((temperature - 273.15) * 9 / 5 + 32))
72
+ st.markdown(
73
+ f"""
74
+ The temperature in **{city}** is **{weather_description}** and the temperature is **{temperature_in_fahrenheit}** degrees Fahrenheit.
75
+ """
76
+ )
77
+
78
+ #Add pressure and humidity
79
+ with col3:
80
+ p_and_h = st.button("Pressure and Humidity")
81
+
82
+ if p_and_h:
83
+ st.markdown("The pressure is **{}** hPa and the humidity is **{}**%.".format(pressure, humidity))
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit==1.22.0
2
+ altair<5