From bbbcbdfc451c4cfa18982ffae25604109b1d0c94 Mon Sep 17 00:00:00 2001 From: Death916 Date: Thu, 24 Apr 2025 04:35:38 -0700 Subject: [PATCH] fix card spacing --- deathclock/deathclock.py | 107 ++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 57 deletions(-) diff --git a/deathclock/deathclock.py b/deathclock/deathclock.py index 661f043..9699ff4 100644 --- a/deathclock/deathclock.py +++ b/deathclock/deathclock.py @@ -12,7 +12,7 @@ from utils.weather import Weather from utils.scores import NBAScores, mlbScores from utils.news import News -# from utils.alarm import Alarm +# from utils.alarm import Alarm # Commented out import import logging # --- Constants --- @@ -27,20 +27,17 @@ class State(rx.State): current_time: str = "" # Note: rx.moment replaces the need for this if used for display alarm_time: str = "" alarms: list = [] - # --- Add type hints --- news: List[Dict[str, Any]] = [] nba_scores: List[Dict[str, Any]] = [] mlb_scores: List[Dict[str, Any]] = [] - # ---------------------- _news_client: News | None = None # This will be set in the constructor last_weather_update: str = "Never" weather_img: str = WEATHER_IMAGE_PATH _weather_client: Weather | None = None # This will be set in the constructor - # --- Add type hints for clients --- _mlb_client: mlbScores | None = None _nba_client: NBAScores | None = None last_sports_update: float = 0.0 - # ---------------------------------- + # --- Initialize Utility Client --- def __init__(self, *args, **kwargs): @@ -66,9 +63,9 @@ class State(rx.State): # --- on_load Handler --- async def start_background_tasks(self): """Starts the weather background task when the page loads.""" - rx.remove_local_storage("chakra-ui-color-mode") #trying to test themes remove after + rx.remove_local_storage("chakra-ui-color-mode") # trying to test themes remove after logging.info("Triggering background tasks: Weather") - # *** FIX: Return a list containing the handler reference *** + # Return a list containing the handler references return [State.fetch_weather, State.fetch_sports] # --- Sports Background Task --- @@ -80,13 +77,12 @@ class State(rx.State): while True: try: logging.info("Fetching sports scores...") - # Fetch MLB and NBA scores - #check if sports has updated in last 5 minutes if so skip + # check if sports has updated in last 5 minutes if so skip if self.last_sports_update and (time.time() - self.last_sports_update) < 300: logging.info("Sports scores already updated within the last 5 minutes. Skipping fetch.") await asyncio.sleep(300) continue - + mlb_scores = await self._mlb_client.get_scores() logging.info(f"MLB Scores: {mlb_scores}") # Check if MLB scores are empty @@ -104,7 +100,7 @@ class State(rx.State): self.nba_scores = [] yield - # Update state with fetched scores + # Update state with fetched scores async with self: self.mlb_scores = mlb_scores self.nba_scores = nba_scores @@ -116,7 +112,7 @@ class State(rx.State): logging.error(f"Error in fetch_sports background task: {e}", exc_info=True) await asyncio.sleep(500) - # format sports scores for display + # (Commented out news fetcher) """ @rx.event(background=True) async def fetch_news(self): @@ -165,9 +161,7 @@ class State(rx.State): if img_web_path: async with self: - timestamp = int(time.time()) - # Update state with cache-busting query param - # Ensure img_web_path is like "/weather.jpg" + timestamp = int(time.time()) # Unused timestamp, kept as per instruction self.weather_img = f"{img_web_path}" self.last_weather_update = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC') logging.info(f"State.weather_img updated to: {self.weather_img}") @@ -201,7 +195,7 @@ def index() -> rx.Component: font_size="4xl", font_weight="bold", color="white", - background_color="#6f42c1", # Original color + background_color="#6f42c1", ), @@ -226,83 +220,87 @@ def index() -> rx.Component: ), rx.card( - rx.vstack( # Added vstack for title/image/status + rx.vstack( rx.heading("Weather", size="4"), rx.image( src=State.weather_img, alt="Current weather conditions for Sacramento", - width="100%", # Keep original width setting - height="auto", # Keep original height setting + width="100%", + height="auto", - object_fit="contain", # Adjust fit as needed - border_radius="var(--radius-3)", # Use theme radius + object_fit="contain", + border_radius="var(--radius-3)", ), rx.text( f"Last Update: {State.last_weather_update}", size="1", color_scheme="gray", - padding_top="0.5em" # Add some space + padding_top="0.5em" ), - align="center", # Center heading/image/text + align="center", spacing="2", ) ), + # --- Modified MLB Scores Card --- rx.card( rx.box( rx.text("MLB Scores"), - rx.foreach( + # Add rx.vstack here to control spacing of foreach items + rx.vstack( + rx.foreach( State.mlb_scores, - lambda score: rx.vstack( - rx.card( - rx.text(f"{score['away_team']} {score['away_score']} @ " - f"{score['home_team']} {score['home_score']} " - f"(Status: {score['status']})", - size="1", - ), + # Lambda now returns the styled card directly + lambda score: rx.card( + rx.text(f"{score.get('away_team','?')} {score.get('away_score','-')} @ " # Use .get() for safety + f"{score.get('home_team','?')} {score.get('home_score','-')} " + f"({score.get('status','?')})", + size="1", ), - spacing="1", - padding="2", size="1", - variant="ghost" - - + padding="1", + variant="surface", + width="100%", ), - + ), + spacing="1", + align_items="stretch", + width="100%", ), - ), ), - spacing="3", # Add spacing between cards + # --- End of Modified MLB Scores Card --- + + spacing="3", width="100%", - justify="center", # Center cards horizontally - align="stretch", # Stretch cards vertically if needed + justify="center", + align="stretch", ), align="center", - spacing="4", # Spacing between clock and flex container + spacing="4", ), ), - # Original container settings - padding="2rem", # Add some padding - max_width="1200px", # Limit width - margin="0 auto", # Center container + + padding="2rem", + max_width="1200px", + margin="0 auto", ), -''' +''' # Commented out style block style = { - "background_color": "black", # Darker purple + "background_color": "black", "color": "#ffffff", "box_shadow": "0 4px 8px rgba(0, 0, 0, 0.2)", "transition": "background-color 0.3s ease, color 0.3s ease", "hover": { - "background_color": "#3a2b4d", # Darker shade on hover + "background_color": "#3a2b4d", "color": "#ffffff", }, } -''' +''' # Commented out style block app = rx.App( theme=rx.theme( appearance="dark", @@ -312,7 +310,7 @@ app = rx.App( gray_color="mauve", has_background=True, ), - #style=style #using them + #style=style # using theme instead ) @@ -321,10 +319,5 @@ app = rx.App( app.add_page( index, title="DeathClock", # Example title - on_load=State.start_background_tasks - # Trigger tasks when this page loads + on_load=State.start_background_tasks # Trigger tasks when this page loads ) - -# The original TypeError is resolved by returning [State.fetch_weather] -# from State.start_background_tasks. -# The image display is fixed by binding rx.image src to State.weather_img.