Status page: data loader (station, dates, Analyse)

A form at the top of the Status page: station dropdown (folders of data/), start and end dates
(end excluded, as --au), and an Analyse button that builds the page for that selection and opens it.
The code is in detectors-view/data-loader/chargeur.py: the form, and its local server (port 8766)
that serves out/ and data/ like the common one, plus /analyse, which runs status.py each time.

python3 detectors-view/status.py without arguments, or detectors-view/status.sh, opens the form
alone; with a station folder it builds the page as before, now opened through the loader server.
common.page.open_served takes another port and server script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-10-29 13:07:13 +01:00
1 parent 3678997321
commit aa2578873d
5 files changed
+103 -14

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
"""Le data loader de la page Status (detectors-view/status.py) : une station de data/ (menu déroulant), une date de début et
une date de fin (exclue, comme --au ; vides : toute la période), et le bouton « Analyse », qui fait la page Status de la
sélection et l'ouvre. Le formulaire est en haut de chaque page Status, et seul dans out/data-loader.html. Son serveur local
(http://127.0.0.1:8766, lancé en tâche de fond) sert out/ et data/ comme celui de common.page, plus /analyse : status.py lancé
à chaque fois, donc toujours à jour de son code, puis renvoi vers sa page.
Usage : python3 detectors-view/status.py, ou detectors-view/status.sh (ouvre le formulaire seul)
"""
import html
import http.server
import subprocess
import sys
from pathlib import Path
from urllib.parse import parse_qs, quote, urlsplit
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "src"))
from common.page import ROOT, _Pages, open_served # noqa: E402 (bascule seule sur .venv si plotly manque)
PORT = 8766 # pas celui de common.page (8765), qui sert les autres pages
STATUS = Path(__file__).resolve().parents[1] / "status.py"
def stations():
return sorted(d.name for d in (ROOT / "data").iterdir() if d.is_dir())
def formulaire(station="", du="", au=""):
"""Le formulaire, en anglais (D-023) ; il ne marche que servi par ce serveur (ouvrir)."""
options = "".join(f"<option{' selected' * (s == station)}>{html.escape(s)}</option>" for s in stations())
return f"""<form action="/analyse" style="display:flex;gap:16px;align-items:center;flex-wrap:wrap;margin-bottom:12px"
onsubmit="this.querySelector('button').textContent = 'Analysing…'">
<label>Station <select name="station">{options}</select></label>
<label>From <input type="date" name="du" value="{html.escape(du)}"></label>
<label>To (excluded) <input type="date" name="au" value="{html.escape(au)}"></label>
<button>Analyse</button></form>"""
def accueil():
"""Le formulaire seul, dans out/data-loader.html -> son chemin."""
chemin = ROOT / "out" / "data-loader.html"
chemin.parent.mkdir(exist_ok=True)
chemin.write_text(f"""<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Status loader</title>
<style>body{{font-family:system-ui,sans-serif;margin:16px;background:#fff;color:#1a1a19}}</style></head><body>
<h2>Status</h2>{formulaire()}</body></html>""", encoding="utf-8")
return chemin
def ouvrir(chemin):
"""Ouvre la page par ce serveur, lancé en tâche de fond s'il ne répond pas."""
open_served(chemin, PORT, __file__)
class Chargeur(_Pages):
def do_GET(self):
url = urlsplit(self.path)
if url.path != "/analyse":
return super().do_GET()
q = {k: v[0] for k, v in parse_qs(url.query).items()}
station = q.get("station")
if station not in stations(): # le nom devient un chemin : une station de data/, rien d'autre
return self.send_error(400, "Unknown station")
args = [sys.executable, str(STATUS), str(ROOT / "data" / station), "--sans-ouvrir"]
args += [f"--{k}={q[k]}" for k in ("du", "au") if q.get(k)]
r = subprocess.run(args, capture_output=True, text=True)
if r.returncode:
return self.send_error(500, "status.py failed", r.stderr[-3000:])
page = Path(r.stdout.strip().splitlines()[-1]).relative_to(ROOT)
self.send_response(303)
self.send_header("Location", "/" + quote(page.as_posix()))
self.end_headers()
if __name__ == "__main__":
if sys.argv[1:] == ["serve"]:
http.server.ThreadingHTTPServer(("127.0.0.1", PORT), Chargeur).serve_forever()
else:
ouvrir(accueil())