Hello
Im new into Reflex and I am struggling with something I want to to. I currently have a page which has a grid of multiple rooms status. Each room has a number and a color representing a risk level. When page loads for the first time, colors are updated based on the roomId. I would like the color to be constantly updating every X minutes.
Here are some portions of my code.
Here i obtain the information from an external source
from Website.data.get_sql_data import get_info_room
@rx.var
def get_room_info(self) -> dict:
data_room_info = get_info_room(self.id_room)
result_dict = {
"NumDocumento": data_room_info[0]['NumDocumento'],
"Nom": data_room_info[0]['Nom'],
"CodImagen": data_room_info[0]['CodImagen'],
"FechaUltima": data_room_info[0]['FechaInicio'], # Si todas son iguales
"NivelRiesgo": data_room_info[0]['NivelAlerta'],
"DescripcionRiesgo": data_room_info[0]['Descripcion'],
"Etiquetas": [
{
"IdCategoriaEtiqueta": item['IdCategoriaEtiqueta'],
"IdEtiqueta": item['IdEtiqueta'],
"NombreEtiqueta": item['NombreEtiqueta']
}
for item in data_room_info
]
}
result = result_dict
return result
Here i define the colors based on the risk for each room
@rx.var
def load_data_room_card(self) -> list[dict[str, str]]:
data_number_rooms = get_number_rooms(self.ubicacion)
dict_alerta_color = {
"3": styles.YellowColor.DARK,
"2": styles.YellowColor.MEDIUM,
"1": styles.YellowColor.LIGHT,
"0": styles.BlueColor.LIGHT,
"-1": styles.GrayColor.MEDIUM,
}
match_rooms_color = [
{
'IdCama': int(room['IdCama']),
'NumHabitacion': int(room['NumHabitacion']),
'color': dict_alerta_color[str(room['NivRiesgo'])]
}
for room in data_number_rooms
]
return match_rooms_color
Here I create the grid for each room. Note the ‘bg’ element.
def grid_card_items(id_room: int, text: int, image: str, bg) -> rx.Component:
return rx.link(
rx.button(
rx.flex(
rx.icon(
image,
width = "100%",
height = "100px",
color = styles.BlueColor.LIGHT,
),
rx.divider(
bg = styles.GrayColor.LIGHT,
height = "1px",
),
rx.text(
text,
size = "8",
width = "100%",
border_radius = "10px",
color = styles.BlackColor.COLOR,
weight = "medium",
bg = bg,
),
width = "100%",
height = "100%",
align_items = "center",
spacing = "2",
direction = "column",
),
width = "100%",
height = "140px",
padding = "8px",
border = "1px solid rgba(58, 58, 58, .8)",
border_radius = "10px",
bg = styles.GrayColor.DARK,
_hover = {
# "opacity": 1,
"bg": styles.GrayColor.MEDIUM,
},
on_click = InfoRoom.click_room_card(id_room, text),
),
href = "/vision_details",
)
here i create the entire grid
def hosp_grid_floor_6(icon: str) -> rx.Component:
return rx.grid(
rx.foreach(
InfoRoom.load_data_room_card,
lambda item: grid_card_items(
id_room=item["IdCama"], # Reflex ya tiene el entero aquí
text=item["NumHabitacion"], # Reflex ya tiene el entero aquí
image=icon,
bg=item["color"]
),
),
width = "100%",
spacing = "4",
padding_top = "16px",
grid_template_columns = [
"repeat(1, 1fr)",
"repeat(2, 1fr)",
"repeat(3, 1fr)",
"repeat(4, 1fr)",
"repeat(5, 1fr)",
"repeat(6, 1fr)",
],
)