mirror of
https://github.com/Death916/deathclock.git
synced 2026-04-10 03:04:40 -07:00
added sports scores need better formatting
This commit is contained in:
parent
69d546ed6e
commit
b119b907d8
1 changed files with 63 additions and 32 deletions
|
|
@ -4,6 +4,8 @@ import reflex as rx
|
|||
from datetime import datetime, timezone
|
||||
import asyncio
|
||||
import time
|
||||
# --- Import typing for hints ---
|
||||
from typing import List, Dict, Any
|
||||
from rxconfig import config
|
||||
# --- Import your Weather utility ---
|
||||
from utils.weather import Weather
|
||||
|
|
@ -25,20 +27,24 @@ class State(rx.State):
|
|||
current_time: str = "" # Note: rx.moment replaces the need for this if used for display
|
||||
alarm_time: str = ""
|
||||
alarms: list = []
|
||||
news: list = [] # Placeholder
|
||||
nba_scores: list = [] # Placeholder
|
||||
mlb_scores: list = [] # Placeholder
|
||||
# --- 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
|
||||
_mlb_client: list | None = None # This will be set in the constructor
|
||||
_nba_client: list | None = None # This will be set in the constructor
|
||||
# --- Add type hints for clients ---
|
||||
_mlb_client: mlbScores | None = None
|
||||
_nba_client: NBAScores | None = None
|
||||
# ----------------------------------
|
||||
|
||||
# --- Initialize Utility Client ---
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# Initialize the weather client
|
||||
# Initialize background clients
|
||||
try:
|
||||
|
||||
self._weather_client = Weather()
|
||||
|
|
@ -55,7 +61,6 @@ class State(rx.State):
|
|||
self.mlb_scores = ""
|
||||
self.nba_scores = ""
|
||||
|
||||
|
||||
# --- on_load Handler ---
|
||||
async def start_background_tasks(self):
|
||||
"""Starts the weather background task when the page loads."""
|
||||
|
|
@ -64,13 +69,11 @@ class State(rx.State):
|
|||
# *** FIX: Return a list containing the handler reference ***
|
||||
return [State.fetch_weather, State.fetch_sports]
|
||||
|
||||
|
||||
# --- Sports Background Task ---
|
||||
|
||||
@rx.event(background=True)
|
||||
async def fetch_sports(self):
|
||||
|
||||
|
||||
# Fetches sports scores periodically
|
||||
while True:
|
||||
try:
|
||||
|
|
@ -130,6 +133,7 @@ class State(rx.State):
|
|||
logging.error(f"Error in fetch_news background task: {e}", exc_info=True)
|
||||
await asyncio.sleep(500)
|
||||
|
||||
|
||||
"""
|
||||
# --- Weather Background Task ---
|
||||
@rx.event(background=True)
|
||||
|
|
@ -159,6 +163,7 @@ class State(rx.State):
|
|||
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}")
|
||||
yield # Update frontend
|
||||
|
||||
else:
|
||||
logging.warning("get_weather_screenshot returned None. State not updated.")
|
||||
# Optionally update status
|
||||
|
|
@ -174,7 +179,6 @@ class State(rx.State):
|
|||
|
||||
await asyncio.sleep(WEATHER_FETCH_INTERVAL)
|
||||
|
||||
|
||||
def index() -> rx.Component:
|
||||
|
||||
return rx.container(
|
||||
|
|
@ -195,7 +199,21 @@ def index() -> rx.Component:
|
|||
rx.flex(
|
||||
rx.card(
|
||||
rx.box(
|
||||
rx.text("SPORTS GO HERE"),
|
||||
rx.text("NBA Scores"),
|
||||
|
||||
rx.foreach(
|
||||
State.nba_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']})"),
|
||||
),
|
||||
spacing="1",
|
||||
padding="2",
|
||||
),
|
||||
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
|
|
@ -226,7 +244,20 @@ def index() -> rx.Component:
|
|||
|
||||
rx.card(
|
||||
rx.box(
|
||||
rx.text("Other sports"),
|
||||
rx.text("MLB Scores"),
|
||||
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']})"),
|
||||
),
|
||||
spacing="1",
|
||||
padding="2",
|
||||
),
|
||||
|
||||
),
|
||||
# Placeholder
|
||||
),
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue