| 6 | |
| 7 | |
| 8 | class SkyCast: |
| 9 | def __init__(self): |
| 10 | self.geolocation_model = GeolocationModel() |
| 11 | self.api_key = "your_api_key_here" |
| 12 | self.base_url = "https://api.weatherbit.io/v2.0/current" |
| 13 | self.weather_option = "Today's Weather" |
| 14 | |
| 15 | def title(self): |
| 16 | st.markdown( |
| 17 | '<div style="border: 1px solid #ccc; padding: 20px; border-radius: 10px;">' |
| 18 | '<h1 style="text-align: center; color: #0080FF;">SkyCast 🌤️</h1>' |
| 19 | "</div>", |
| 20 | unsafe_allow_html=True, |
| 21 | ) |
| 22 | |
| 23 | def input(self): |
| 24 | st.sidebar.title("Weather Options") |
| 25 | self.weather_option = st.sidebar.radio( |
| 26 | "Choose Weather Option", |
| 27 | options=["Today's Weather", "Forecast Weather"], |
| 28 | key="weather_option", |
| 29 | ) |
| 30 | |
| 31 | if self.weather_option == "Today's Weather": |
| 32 | self.display_today_weather() |
| 33 | else: |
| 34 | self.display_forecast_weather() |
| 35 | |
| 36 | def display_today_weather(self): |
| 37 | latitude, longitude = None, None |
| 38 | # Manually enter the city name |
| 39 | city_name = st.sidebar.text_input("Enter City Name", key="city_name") |
| 40 | if st.sidebar.button("Submit"): |
| 41 | latitude, longitude = self.geolocation_model.get_location_by_name(city_name) |
| 42 | else: |
| 43 | # If the submit button is not clicked, do not proceed further |
| 44 | return |
| 45 | |
| 46 | st.markdown( |
| 47 | f"<h2 style='text-align: center;'>Today's Weather</h2>", |
| 48 | unsafe_allow_html=True, |
| 49 | ) |
| 50 | |
| 51 | if latitude is not None and longitude is not None: |
| 52 | # Make API request |
| 53 | response = self.get_weather_data(latitude, longitude) |
| 54 | if response is not None: |
| 55 | weather_data = response["data"][0] |
| 56 | |
| 57 | # Weather details container |
| 58 | datetime_value = weather_data["datetime"] |
| 59 | datetime_value = datetime_value[:-3] |
| 60 | # Date and time |
| 61 | st.markdown( |
| 62 | f'<h3 style="text-align: center;">{weather_data["city_name"]}, {weather_data["country_code"]}: {datetime_value}</h3>', |
| 63 | unsafe_allow_html=True, |
| 64 | ) |
| 65 | |