Spaces:
Sleeping
Sleeping
giangimene
CommandCodeBot
Dashboard: degradazione batteria, timezone, lock, costi persistenza, ICE comparison
d7b8cd6 Download ui_components.py from giangimene/Dasboard: direct link, hf CLI and curl.
- Browser
- Download file 8.18 kB
-
https://huggingface.co/spaces/giangimene/Dasboard/resolve/main/ui_components.py
- Command line
-
hf download hf://spaces/giangimene/Dasboard/ui_components.py
-
curl -L -o ui_components.py https://huggingface.co/spaces/giangimene/Dasboard/resolve/main/ui_components.py
8.18 kB
| """ | |
| ui_components.py β Componenti Streamlit riutilizzabili: card, mappa, veicolo. | |
| """ | |
| from typing import Any | |
| import streamlit as st | |
| from utils import safe_get, format_number, format_datetime | |
| try: | |
| import pydeck as pdk | |
| except Exception: | |
| pdk = None | |
| # βββ Status pill HTML βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def status_pill_html(text: str, kind: str = "info") -> str: | |
| return f'<span class="status-pill status-{kind}">{text}</span>' | |
| # βββ KPI card βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def render_kpi_card(title: str, value: str, subtitle: str = "", small: bool = False) -> None: | |
| value_class = "card-value-small" if small else "card-value" | |
| st.markdown( | |
| f""" | |
| <div class="card"> | |
| <div class="card-title">{title}</div> | |
| <div class="{value_class}">{value}</div> | |
| <div class="card-sub">{subtitle}</div> | |
| </div> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| def render_info_tile(title: str, value: str, subtitle: str = "") -> None: | |
| """Alias per render_kpi_card con testo piΓΉ piccolo.""" | |
| render_kpi_card(title, value, subtitle, small=True) | |
| # βββ FIX #6 β Barra batteria colorata con soglie dinamiche βββββββββββββββββββ | |
| def render_battery_bar(level: float | None) -> None: | |
| """ | |
| Barra di progresso batteria con colore condizionale: | |
| - Verde > 50% | |
| - Arancio 20β50% | |
| - Rosso < 20% | |
| Sostituisce il grezzo st.progress() nativo. | |
| """ | |
| if level is None: | |
| st.markdown( | |
| '<div style="height:22px;border-radius:12px;background:#333;margin:8px 0;">' | |
| '<div style="padding:2px 10px;color:#b9b9b9;font-size:0.8rem;">N/D</div></div>', | |
| unsafe_allow_html=True, | |
| ) | |
| return | |
| pct = max(0.0, min(float(level), 100.0)) | |
| if pct > 50: | |
| color = "#1fa971" | |
| glow = "rgba(31,169,113,0.45)" | |
| elif pct > 20: | |
| color = "#ff9f1c" | |
| glow = "rgba(255,159,28,0.45)" | |
| else: | |
| color = "#e5484d" | |
| glow = "rgba(229,72,77,0.55)" | |
| # Animazione pulse solo quando in carica non Γ¨ richiesta qui | |
| # ma il colore rosso giΓ segnala urgenza | |
| st.markdown( | |
| f""" | |
| <div style=" | |
| background:#242424; | |
| border-radius:12px; | |
| height:22px; | |
| width:100%; | |
| overflow:hidden; | |
| border:1px solid #333; | |
| margin:8px 0; | |
| position:relative; | |
| "> | |
| <div style=" | |
| width:{pct:.1f}%; | |
| height:100%; | |
| background: linear-gradient(90deg, {color}cc, {color}); | |
| border-radius:12px; | |
| box-shadow: 0 0 8px {glow}; | |
| transition: width 0.6s ease; | |
| display:flex; | |
| align-items:center; | |
| justify-content:flex-end; | |
| padding-right:8px; | |
| "> | |
| <span style="color:#fff;font-weight:800;font-size:0.78rem;text-shadow:0 1px 3px #0008;"> | |
| {pct:.0f}% | |
| </span> | |
| </div> | |
| </div> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| # βββ Mappa GPS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def build_google_maps_url(lat: Any, lon: Any) -> str | None: | |
| if lat is None or lon is None: | |
| return None | |
| return f"https://www.google.com/maps?q={lat},{lon}" | |
| def build_waze_url(lat: Any, lon: Any) -> str | None: | |
| if lat is None or lon is None: | |
| return None | |
| return f"https://waze.com/ul?ll={lat},{lon}&navigate=yes" | |
| def build_chargemap_url(lat: Any, lon: Any) -> str | None: | |
| """Link a Chargemap β mostra le colonnine vicine.""" | |
| if lat is None or lon is None: | |
| return None | |
| return f"https://chargemap.com/map#/{lat},{lon},15" | |
| def build_plugshare_url(lat: Any, lon: Any) -> str | None: | |
| """Link a PlugShare β stazioni di ricarica nelle vicinanze.""" | |
| if lat is None or lon is None: | |
| return None | |
| return f"https://plugshare.com/?lat={lat}&lng={lon}" | |
| def render_map(lat: Any, lon: Any, zoom: int = 15, autonomia_km: float | None = None) -> None: | |
| """ | |
| FIX #9 β Mappa migliorata con pydeck: | |
| - Marker rosso per la posizione del veicolo | |
| - Cerchio di autonomia residua opzionale | |
| - Link a Google Maps e Waze | |
| """ | |
| if lat is None or lon is None: | |
| st.warning("Dati GPS non disponibili.") | |
| return | |
| import pandas as pd | |
| df = pd.DataFrame({"lat": [float(lat)], "lon": [float(lon)]}) | |
| if pdk is None: | |
| st.map(df, zoom=zoom) | |
| return | |
| layers: list[Any] = [] | |
| # Cerchio raggio autonomia (FIX #9 β feature nuova) | |
| if autonomia_km and autonomia_km > 0: | |
| radius_m = int(autonomia_km * 1000) | |
| layers.append( | |
| pdk.Layer( | |
| "ScatterplotLayer", | |
| data=df, | |
| get_position="[lon, lat]", | |
| get_radius=radius_m, | |
| get_fill_color=[255, 204, 0, 18], | |
| get_line_color=[255, 204, 0, 120], | |
| stroked=True, | |
| line_width_min_pixels=2, | |
| pickable=False, | |
| ) | |
| ) | |
| # Marker veicolo | |
| layers.append( | |
| pdk.Layer( | |
| "ScatterplotLayer", | |
| data=df, | |
| get_position="[lon, lat]", | |
| get_radius=35, | |
| get_fill_color=[255, 40, 40, 220], | |
| pickable=True, | |
| auto_highlight=True, | |
| ) | |
| ) | |
| tooltip = { | |
| "html": "<b>π Il tuo veicolo</b><br/>Lat: {lat}<br/>Lon: {lon}", | |
| "style": {"backgroundColor": "#111", "color": "white", "borderRadius": "8px", "padding": "8px"}, | |
| } | |
| view_state = pdk.ViewState( | |
| latitude=float(lat), longitude=float(lon), zoom=zoom, pitch=0 | |
| ) | |
| st.pydeck_chart( | |
| pdk.Deck(layers=layers, initial_view_state=view_state, tooltip=tooltip) | |
| ) | |
| # βββ Scheda veicolo βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def render_vehicle_card(vin: str, cockpit: Any, details: Any, lock_status: Any) -> None: | |
| raw_details = getattr(details, "raw_data", {}) if details else {} | |
| img_url = None | |
| for asset in raw_details.get("assets", []): | |
| if asset.get("viewpoint") == "mybrand_5": | |
| for r in asset.get("renditions", []): | |
| if r.get("resolutionType") == "ONE_MYRENAULT_LARGE": | |
| img_url = r.get("url") | |
| break | |
| if img_url: | |
| st.image(img_url, caption="Il tuo veicolo", use_container_width=True) | |
| modello_label = raw_details.get("model", {}).get("label", "Renault") | |
| versione_raw = raw_details.get("version", {}).get("label", "N/D") | |
| motore_label = raw_details.get("engineType", "N/D") | |
| batt_label = raw_details.get("batteryType", "N/D") | |
| st.markdown(f"### π {modello_label} {versione_raw}") | |
| r1c1, r1c2 = st.columns(2) | |
| with r1c1: | |
| st.write(f"**Modello:** {modello_label}") | |
| st.write(f"**Targa:** {raw_details.get('registrationNumber', 'N/D')}") | |
| st.write(f"**VIN:** {vin}") | |
| with r1c2: | |
| st.write(f"**Batteria:** {batt_label}") | |
| st.write(f"**Motore:** {motore_label}") | |
| st.write(f"**Odometro:** {format_number(safe_get(cockpit, 'totalMileage'), ' km')}") | |
| st.markdown("---") | |
| st.markdown("**π Storia dell'auto**") | |
| h1, h2 = st.columns(2) | |
| with h1: | |
| st.info(f"ποΈ **Prodotta il:**\n\n{raw_details.get('manufacturingDate', 'N/D')}") | |
| with h2: | |
| st.success(f"π **Immatricolata il:**\n\n{raw_details.get('registrationDate', 'N/D')}") | |