-
Notifications
You must be signed in to change notification settings - Fork 37
[ENG-5878] weather app template #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94343cd
weather app template
LineIndent 96f3f63
ruff weather
LineIndent dc712dc
Merge branch 'main' into ahmad/weatherstack-app
LineIndent 929c649
update backend logic
LineIndent 5f983f0
remove Apple Double meta data
LineIndent e76085e
remove Apple Double meta data
LineIndent f0720a4
ruff
LineIndent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| __pycache__/ | ||
| .web | ||
| assets/external/ | ||
| *.py[cod] | ||
| *.db | ||
| .states | ||
| .DS_Store | ||
| .idea/ |
Binary file not shown.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| reflex>=0.7.10 | ||
| httpx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import reflex as rx | ||
|
|
||
| config = rx.Config(app_name="weatherstack_app") |
Empty file.
Empty file.
36 changes: 36 additions & 0 deletions
36
weatherstack_app/weatherstack_app/components/preset_cards.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import reflex as rx | ||
|
|
||
| from weatherstack_app.states.weather_state import WeatherState | ||
|
|
||
|
|
||
| def card(flag: str, title: str, city: str) -> rx.Component: | ||
| return rx.el.button( | ||
| rx.el.div( | ||
| rx.el.span(flag, class_name="text-xl"), | ||
| rx.el.p(title, class_name="font-medium text-black text-base"), | ||
| class_name="flex flex-row gap-2 items-center", | ||
| ), | ||
| type="button", | ||
| class_name=( | ||
| "flex flex-col gap-1 border bg-white hover:bg-gray-100 " | ||
| "shadow-sm px-4 py-3.5 rounded-xl text-start transition-colors flex-1" | ||
| ), | ||
| on_click=WeatherState.get_weather_from_preset(city), | ||
| ) | ||
|
|
||
|
|
||
| def preset_cards() -> rx.Component: | ||
| return rx.el.div( | ||
| rx.el.div( | ||
| card("🇯🇵", "Tokyo, Japan", "Japan"), | ||
| card("🇫🇷", "Paris, France", "France"), | ||
| card("🇺🇸", "New York, USA", "USA"), | ||
| card("🇦🇺", "Sydney, Australia", "Australia"), | ||
| card("🇩🇪", "Berlin, Germany", "Germany"), | ||
| card("🇧🇷", "São Paulo, Brazil", "Brazil"), | ||
| card("🇨🇦", "Toronto, Canada", "Canada"), | ||
| card("🇮🇳", "Mumbai, India", "India"), | ||
| class_name="gap-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 w-full", | ||
| ), | ||
| class_name="flex flex-col justify-center items-center gap-8 w-full max-w-[55rem] px-6 md:pt-12", | ||
| ) |
111 changes: 111 additions & 0 deletions
111
weatherstack_app/weatherstack_app/components/weather_display.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import reflex as rx | ||
|
|
||
| from weatherstack_app.states.weather_state import WeatherState | ||
|
|
||
|
|
||
| def weather_display() -> rx.Component: | ||
| return rx.el.div( | ||
| rx.cond( | ||
| WeatherState.loading, | ||
| rx.el.div( | ||
| rx.spinner(class_name="text-blue-500"), | ||
| rx.el.p( | ||
| "Loading weather data...", | ||
| class_name="text-lg text-gray-600", | ||
| ), | ||
| class_name="flex flex-col items-center justify-center p-6 bg-white rounded-lg shadow-md", | ||
| ), | ||
| rx.cond( | ||
| WeatherState.error_message != "", | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Error:", | ||
| class_name="font-semibold text-red-600", | ||
| ), | ||
| rx.el.p( | ||
| WeatherState.error_message, | ||
| class_name="text-red-500", | ||
| ), | ||
| class_name="p-6 bg-red-50 rounded-lg shadow-md border border-red-200", | ||
| ), | ||
| rx.cond( | ||
| WeatherState.display_weather, | ||
| rx.el.div( | ||
| rx.el.p( | ||
| f"{WeatherState.weather_data['location']['name']}, {WeatherState.weather_data['location']['country']}", | ||
| class_name="text-3xl font-bold text-gray-800 mb-4", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.img( | ||
| src=WeatherState.weather_icon_url, | ||
| alt="Weather icon", | ||
| class_name="w-20 h-20 mb-2", | ||
| ), | ||
| rx.el.p( | ||
| WeatherState.weather_description_text, | ||
| class_name="text-xl text-gray-700 capitalize", | ||
| ), | ||
| class_name="flex flex-col items-center mb-4", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Temperature:", | ||
| class_name="text-md text-gray-600", | ||
| ), | ||
| rx.el.p( | ||
| f"{WeatherState.weather_data['current']['temperature']}°C", | ||
| class_name="text-2xl font-semibold text-blue-600", | ||
| ), | ||
| class_name="p-4 bg-blue-50 rounded-lg shadow-sm text-center", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Humidity:", | ||
| class_name="text-md text-gray-600", | ||
| ), | ||
| rx.el.p( | ||
| f"{WeatherState.weather_data['current']['humidity']}%", | ||
| class_name="text-2xl font-semibold text-green-600", | ||
| ), | ||
| class_name="p-4 bg-green-50 rounded-lg shadow-sm text-center", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Feels Like:", | ||
| class_name="text-md text-gray-600", | ||
| ), | ||
| rx.el.p( | ||
| f"{WeatherState.weather_data['current']['feelslike']}°C", | ||
| class_name="text-2xl font-semibold text-orange-600", | ||
| ), | ||
| class_name="p-4 bg-orange-50 rounded-lg shadow-sm text-center", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Wind Speed:", | ||
| class_name="text-md text-gray-600", | ||
| ), | ||
| rx.el.p( | ||
| f"{WeatherState.weather_data['current']['wind_speed']} km/h", | ||
| class_name="text-2xl font-semibold text-purple-600", | ||
| ), | ||
| class_name="p-4 bg-purple-50 rounded-lg shadow-sm text-center", | ||
| ), | ||
| class_name="grid grid-cols-1 md:grid-cols-2 gap-4 w-full", | ||
| ), | ||
| rx.button("Reset", on_click=WeatherState.reset_app), | ||
| class_name="p-6 rounded-lg border shadow-sm border w-full max-w-2xl justify-center", | ||
| ), | ||
| rx.el.div( | ||
| rx.el.p( | ||
| "Enter a city to get the weather forecast.", | ||
| class_name="text-lg text-gray-500", | ||
| ), | ||
| class_name="p-6 bg-white rounded-lg shadow-md", | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| class_name="mt-8 w-full flex justify-center", | ||
| ) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
172 changes: 172 additions & 0 deletions
172
weatherstack_app/weatherstack_app/states/weather_state.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| from typing import List, TypedDict | ||
|
|
||
| import httpx | ||
| import reflex as rx | ||
|
|
||
| WEATHERSTACK_API_KEY = "YOUR_WEATHERSTACK_API_KEY" | ||
| WEATHERSTACK_API_URL = "http://api.weatherstack.com/current" | ||
|
|
||
|
|
||
| class Location(TypedDict): | ||
| name: str | ||
| country: str | ||
| region: str | ||
| lat: str | ||
| lon: str | ||
| timezone_id: str | ||
| localtime: str | ||
| localtime_epoch: int | ||
| utc_offset: str | ||
|
|
||
|
|
||
| class CurrentWeather(TypedDict): | ||
| observation_time: str | ||
| temperature: int | ||
| weather_code: int | ||
| weather_icons: List[str] | ||
| weather_descriptions: List[str] | ||
| wind_speed: int | ||
| wind_degree: int | ||
| wind_dir: str | ||
| pressure: int | ||
| precip: float | ||
| humidity: int | ||
| cloudcover: int | ||
| feelslike: int | ||
| uv_index: int | ||
| visibility: int | ||
| is_day: str | ||
|
|
||
|
|
||
| class WeatherRequest(TypedDict): | ||
| type: str | ||
| query: str | ||
| language: str | ||
| unit: str | ||
|
|
||
|
|
||
| class WeatherData(TypedDict): | ||
| request: WeatherRequest | None | ||
| location: Location | None | ||
| current: CurrentWeather | None | ||
|
|
||
|
|
||
| class WeatherState(rx.State): | ||
| city: str = "" | ||
| weather_data: WeatherData | None = None | ||
| loading: bool = False | ||
| error_message: str = "" | ||
| api_key: str = WEATHERSTACK_API_KEY | ||
|
|
||
| @rx.event | ||
| def reset_app(self): | ||
| self.reset() | ||
| return | ||
|
|
||
| @rx.event | ||
| def handle_form_submit(self, form_data: dict): | ||
| self.city = form_data.get("city", "").strip() | ||
| if not self.city: | ||
| self.error_message = "City name cannot be empty." | ||
| self.weather_data = None | ||
| return | ||
| self.error_message = "" | ||
| return WeatherState.get_weather | ||
|
|
||
| @rx.event | ||
| def get_weather_from_preset(self, city: str): | ||
| self.city = city | ||
| if not self.city: | ||
| self.error_message = "City name cannot be empty." | ||
| self.weather_data = None | ||
| return | ||
| self.error_message = "" | ||
| return WeatherState.get_weather | ||
|
|
||
| @rx.event(background=True) | ||
| async def get_weather(self): | ||
| async with self: | ||
| if not self.city: | ||
| self.error_message = "City name cannot be empty." | ||
| self.loading = False | ||
| return | ||
| if self.api_key == "YOUR_WEATHERSTACK_API_KEY": | ||
| self.error_message = "Please replace 'YOUR_WEATHERSTACK_API_KEY' with your actual WeatherStack API key in app/states/weather_state.py." | ||
| self.weather_data = None | ||
| self.loading = False | ||
| return | ||
| self.loading = True | ||
| self.error_message = "" | ||
| self.weather_data = None | ||
|
|
||
| try: | ||
| params = { | ||
| "access_key": self.api_key, | ||
| "query": self.city, | ||
| } | ||
|
|
||
| async with httpx.AsyncClient(timeout=10) as client: | ||
| response = await client.get(WEATHERSTACK_API_URL, params=params) | ||
| response.raise_for_status() | ||
| data = response.json() | ||
|
|
||
| async with self: | ||
| if "error" in data: | ||
| self.error_message = data["error"].get( | ||
| "info", | ||
| "An error occurred while fetching weather data.", | ||
| ) | ||
| self.weather_data = None | ||
| elif "current" in data and "location" in data: | ||
| self.weather_data = data | ||
| self.error_message = "" | ||
| else: | ||
| self.error_message = "Unexpected API response format." | ||
| self.weather_data = None | ||
|
|
||
| except httpx.RequestError as e: | ||
| async with self: | ||
| self.error_message = f"Network error: {e}" | ||
| self.weather_data = None | ||
| except Exception as e: | ||
| async with self: | ||
| self.error_message = f"An unexpected error occurred: {e}" | ||
| self.weather_data = None | ||
| finally: | ||
| async with self: | ||
| self.loading = False | ||
|
|
||
| def set_city(self, city: str): | ||
| self.city = city.strip() | ||
| self.error_message = "" | ||
|
|
||
| @rx.var | ||
| def display_weather(self) -> bool: | ||
| return ( | ||
| self.weather_data is not None | ||
| and self.weather_data.get("current") is not None | ||
| and (self.weather_data.get("location") is not None) | ||
| and (not self.error_message) | ||
| ) | ||
|
|
||
| @rx.var | ||
| def weather_icon_url(self) -> str: | ||
| if ( | ||
| self.display_weather | ||
| and self.weather_data | ||
| and self.weather_data.get("current") | ||
| and self.weather_data["current"].get("weather_icons") | ||
| ): | ||
| return self.weather_data["current"]["weather_icons"][0] | ||
| return "" | ||
|
|
||
| @rx.var | ||
| def weather_description_text(self) -> str: | ||
| if ( | ||
| self.display_weather | ||
| and self.weather_data | ||
| and self.weather_data.get("current") | ||
| and self.weather_data["current"].get("weather_descriptions") | ||
| ): | ||
| return ", ".join(self.weather_data["current"]["weather_descriptions"]) | ||
| return "" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to make sure this is updated after deploying the app
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup will change this today after re-deploy