Files
Astrape/gibil/classes/webui.py
T
rpotter6298 9d15860f0b first_commit
2026-04-25 20:35:25 +02:00

255 lines
6.7 KiB
Python

from __future__ import annotations
from os import environ
from gibil.classes.env_loader import EnvLoader
from gibil.classes.weather_sample_data import WeatherSampleData
from gibil.classes.weather_store import WeatherStore, WeatherStoreConfigurationError
from gibil.classes.weather_display import WeatherDisplay
class WebUI:
"""Composes Astrape web modules into one page."""
def __init__(self) -> None:
self.weather_display = WeatherDisplay()
def render_page(self) -> str:
return f"""<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Astrape</title>
<style>
:root {{
color-scheme: light;
--bg: #11151c;
--surface: #181e27;
--panel: #1f2733;
--ink: #edf2f7;
--muted: #9aa8ba;
--line: #344052;
--field: #121821;
}}
* {{
box-sizing: border-box;
}}
body {{
margin: 0;
background: var(--bg);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
line-height: 1.4;
}}
header {{
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 22px 28px;
border-bottom: 1px solid var(--line);
background: var(--surface);
}}
h1, h2, p {{
margin: 0;
}}
h1 {{
font-size: 22px;
font-weight: 700;
}}
h2 {{
font-size: 18px;
font-weight: 700;
}}
p {{
color: var(--muted);
font-size: 13px;
}}
main {{
width: min(1280px, 100%);
margin: 0 auto;
padding: 24px;
}}
.panel {{
background: var(--surface);
border: 1px solid var(--line);
border-radius: 8px;
padding: 18px;
}}
.panel-heading {{
display: grid;
grid-template-columns: minmax(180px, auto) 1fr;
gap: 24px;
margin-bottom: 18px;
}}
.control-row {{
display: flex;
align-items: end;
justify-content: flex-end;
flex-wrap: wrap;
gap: 14px;
}}
label {{
display: grid;
gap: 6px;
color: var(--muted);
font-size: 12px;
font-weight: 600;
}}
select {{
min-width: 150px;
border: 1px solid var(--line);
border-radius: 6px;
background: var(--field);
color: var(--ink);
padding: 8px;
font: inherit;
}}
.legend-control {{
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
gap: 10px 12px;
color: var(--muted);
font-size: 12px;
font-weight: 600;
}}
.legend-title {{
line-height: 1;
}}
.horizon-options {{
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px 12px;
}}
.horizon-option {{
display: flex;
align-items: center;
gap: 8px;
color: var(--ink);
font-size: 13px;
font-weight: 500;
line-height: 1.2;
}}
.horizon-option input {{
width: 14px;
height: 14px;
margin: 0;
}}
.horizon-option .horizon-value {{
width: 54px;
height: 28px;
border: 1px solid var(--line);
border-radius: 6px;
background: var(--field);
color: var(--ink);
padding: 4px 6px;
font: inherit;
}}
.legend-swatch {{
width: 22px;
height: 3px;
border-radius: 999px;
flex: 0 0 auto;
}}
.truth-swatch {{
background: #f8fafc;
}}
.chart-shell {{
position: relative;
min-height: 420px;
border: 1px solid var(--line);
border-radius: 6px;
overflow: hidden;
background: var(--panel);
}}
canvas {{
display: block;
width: 100%;
height: 420px;
}}
@media (max-width: 760px) {{
header, .panel-heading, .control-row {{
display: grid;
}}
.legend-control, .horizon-options {{
justify-content: flex-start;
}}
main {{
padding: 14px;
}}
select {{
width: 100%;
}}
}}
</style>
</head>
<body>
<header>
<h1>Astrape</h1>
<p>Gibil web UI</p>
</header>
<main>
{self.weather_display.render()}
</main>
<script>
let astrapeUiVersion = null;
async function watchUiVersion() {{
const response = await fetch("/api/ui-version", {{ cache: "no-store" }});
const payload = await response.json();
if (astrapeUiVersion === null) {{
astrapeUiVersion = payload.version;
return;
}}
if (astrapeUiVersion !== payload.version) {{
window.location.reload();
}}
}}
setInterval(watchUiVersion, 1000);
watchUiVersion();
</script>
</body>
</html>"""
def weather_payload(self) -> str:
EnvLoader().load()
if environ.get("ASTRAPE_WEB_SAMPLE_DATA") == "1":
return self.weather_display.data_payload(WeatherSampleData().build())
try:
dataset = WeatherStore.from_env().load_display_dataset()
except WeatherStoreConfigurationError:
dataset = None
return self.weather_display.data_payload(dataset)