91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
import plotly.graph_objects as go
|
|
import requests
|
|
import streamlit as st
|
|
from plotly.subplots import make_subplots
|
|
|
|
from auth import require_auth
|
|
from data_processing import TARGET_HIGH, TARGET_LOW
|
|
from nightscout_client import NightscoutConfigError
|
|
from ui_common import (
|
|
add_threshold_lines,
|
|
get_colors,
|
|
load_range_data,
|
|
render_completeness_warning,
|
|
select_period_days,
|
|
)
|
|
|
|
st.set_page_config(page_title="Glucose dashboard — Dagoverzicht", page_icon="📊", layout="wide")
|
|
require_auth()
|
|
|
|
st.title("Dagoverzicht")
|
|
st.caption("Eén glucosegrafiek per kalenderdag (FR4).")
|
|
|
|
start, end, days = select_period_days(default_label="14 dagen")
|
|
if days > 31:
|
|
st.info(
|
|
"Bij langere periodes wordt de pagina lang en kan laden traag zijn — "
|
|
"voor het bekijken van individuele dagen is 14 of 30 dagen prettiger."
|
|
)
|
|
|
|
try:
|
|
glucose, _treatments = load_range_data(start, end)
|
|
except NightscoutConfigError as exc:
|
|
st.error(str(exc))
|
|
st.stop()
|
|
except (requests.RequestException, ValueError) as exc:
|
|
st.error(f"Kon geen data ophalen van Nightscout: {exc}")
|
|
st.stop()
|
|
|
|
if glucose.empty:
|
|
st.info("Geen glucosemetingen gevonden in deze periode.")
|
|
st.stop()
|
|
|
|
render_completeness_warning(glucose, start, end)
|
|
|
|
colors = get_colors()
|
|
glucose = glucose.copy()
|
|
glucose["date"] = glucose["timestamp"].dt.date
|
|
glucose["hour"] = glucose["timestamp"].dt.hour + glucose["timestamp"].dt.minute / 60
|
|
|
|
dates = sorted(glucose["date"].unique())
|
|
row_height = 150
|
|
fig = make_subplots(
|
|
rows=len(dates),
|
|
cols=1,
|
|
shared_xaxes=True,
|
|
subplot_titles=[d.strftime("%A %d-%m-%Y") for d in dates],
|
|
vertical_spacing=min(0.4 / max(len(dates) - 1, 1), 0.05),
|
|
)
|
|
|
|
for i, day in enumerate(dates, start=1):
|
|
day_df = glucose[glucose["date"] == day]
|
|
fig.add_hrect(
|
|
y0=TARGET_LOW, y1=TARGET_HIGH, fillcolor=colors["band"], line_width=0, layer="below", row=i, col=1
|
|
)
|
|
fig.add_trace(
|
|
go.Scatter(
|
|
x=day_df["hour"],
|
|
y=day_df["mmol"],
|
|
mode="lines",
|
|
line={"color": colors["glucose"], "width": 2},
|
|
showlegend=False,
|
|
hovertemplate="%{y:.1f} mmol/L<extra></extra>",
|
|
),
|
|
row=i,
|
|
col=1,
|
|
)
|
|
fig.update_yaxes(title_text="mmol/L", gridcolor=colors["grid"], row=i, col=1)
|
|
add_threshold_lines(fig, colors, row=i)
|
|
|
|
fig.update_xaxes(title_text="Uur van de dag", range=[0, 24], dtick=4, gridcolor=colors["grid"])
|
|
fig.update_layout(
|
|
paper_bgcolor="rgba(0,0,0,0)",
|
|
plot_bgcolor="rgba(0,0,0,0)",
|
|
font_color=colors["text"],
|
|
height=row_height * len(dates),
|
|
showlegend=False,
|
|
margin={"l": 10, "r": 10, "t": 30, "b": 10},
|
|
)
|
|
st.plotly_chart(fig, width="stretch")
|
|
st.caption(f"{len(dates)} dagen · {len(glucose)} metingen")
|