{# The health pages' building blocks (blocks.py): one macro per block, named after its id
(dots → underscores). Each gets V = the views the page computed ({src: view}: overview,
body, sleep, heart, activity — only those its blocks need). A tile macro gives the tile's
inside (tab.html wraps it: the wrapper is the tile); a card macro gives its section.
Nothing to show → empty output (the page hides the block; edit mode shows a placeholder).
Import "with context" (v: today, range, ranges, grain, grains, body, prof). #}
{% import "health/_parts.html" as hp with context %}
{% from "health/_watch.html" import hm, clock with context %}
{% set n, sgn, dm, chart = hp.n, hp.sgn, hp.dm, hp.chart %}
{% macro tip(text, t) %}{{ text }}{{ t }}{% endmacro %}
{% macro cls(c) %}{% if c %}{{ c[0] }}{% endif %}{% endmacro %}
{% macro when(B, k) %}{% if B.cur[k] %}{{ dm(B.cur[k].day) }}{% if B.cur[k].stale %} · too old to use{% endif %}{% endif %}{% endmacro %}
{% macro dhref(S) %}?r={{ S.range }}&g={{ S.grain }}&d={% endmacro %}
{# ======================= "last night / last workout" (watch.overview, src "overview") —
blocks of Sleep and Activity, meant for the Overview ======================= #}
{% macro sleep_last_score(V) %}{% set o = V.overview %}
Sleep score{{ o.night.score if o.night else "—" }}{% if o.night %}last night · {{ hm(o.night.asleep) }} asleep{% endif %}{% if o.score7 %} · 7-night average {{ o.score7 | round | int }}{% endif %}{% endmacro %}
{% macro sleep_last_night(V) %}{% set o = V.overview %}{% if o.night %}{% set t = o.night %}
{{ n(w.avg_hr, 0) if w.avg_hr else "—" }} avg · {{ w.max_hr or "—" }} max
Calories
{{ n(w.kcal, 0) if w.kcal is not none else "—" }} active{% if w.kcal_device %} · Mi {{ n(w.kcal_device, 0) }}{% endif %}
Load
{{ n(w.load, 0) if w.load else "—" }}{% if w.effect %} · {{ w.effect }}{% endif %}
Zones
{{ hp.zone_strip(w.zone_pct) }}
{{ hp.hr_graph(o.graph, "hl-hr hl-hr-s") }}
{% endif %}{% endmacro %}
{# ======================= body ======================= #}
{% macro body_weight(V) %}{% set B = V.body %}{% if B.rows %}
Weight{{ n(B.cur.weight.v if B.cur.weight else none) }} kg
{% if B.weight_change %}{{ sgn(B.weight_change.delta) }} kg in {{ B.weight_change.days }} days{% endif %}{{ when(B, "weight") }}{% endif %}{% endmacro %}
{% macro body_bmi(V) %}{% set B = V.body %}{% set b, p = B.body, B.prof %}{% if B.rows %}
BMI{{ n(b.bmi) }}{% if b.bmi_class %}{{ hp.bmi_pop(b.bmi_class) }}{% else %}{{ "needs height" if not p.height else "needs weight" }}{% endif %}
{% if b.healthy %}normal range {{ n(b.healthy[0], 0) }}–{{ n(b.healthy[1], 0) }} kg{% endif %}{% endif %}{% endmacro %}
{% macro body_waist(V) %}{% set B = V.body %}{% set b = B.body %}{% if B.rows %}
Waist{{ n(B.cur.waist.v if B.cur.waist else none) }} cm
{% if B.waist_change %}{{ sgn(B.waist_change.delta) }} cm in {{ B.waist_change.days }} days · {% endif %}{{ b.waist_class or "" }}{{ when(B, "waist") }}{% endif %}{% endmacro %}
{% macro body_fat(V) %}{% set B = V.body %}{% set b = B.body %}{% if B.rows %}
Body fat{% if b.fat_mid %}{{ n(b.fat_mid) }} %{% else %}—{% endif %}
{% if b.fat_range %}{{ n(b.fat_range[0]) }}–{{ n(b.fat_range[1]) }} % · {{ b.fat_n }} formulas{% elif b.fat_n == 1 %}1 formula{% else %}add waist, neck, hips{% endif %}
{% if b.fat_class %}{{ hp.fat_pop(b.fat_class) }}{% endif %}{% endif %}{% endmacro %}
{% macro body_charts(V) %}{% set B = V.body %}{% if B.rows %}
{% for k in ("weight", "waist", "bmi", "fat") %}
{{ chart(B.charts[k], k) }}
{% endfor %}
{% endif %}{% endmacro %}
{% macro body_fat_table(V) %}{% set B = V.body %}{% set b = B.body %}{% if B.rows %}
{{ tip("Body fat", "Tape-measure formulas only — no BMI-based ones (they count muscle as fat). None of the tape measurements includes the chest (Navy: waist, hips, neck). All are estimates (a few % either way); the trend over months says more than one number. Not medical advice.") }} {% if b.fat_mid %}middle value {{ n(b.fat_mid) }} %{% if b.fat_range %} · range {{ n(b.fat_range[0]) }}–{{ n(b.fat_range[1]) }} %{% endif %}{% if b.fat_class %} · {{ hp.fat_pop(b.fat_class) }}{% endif %}{% endif %}
{% for f in b.fat %}
{{ f.name }}{{ f.note }}
{% if f.value is not none %}
{{ n(f.value) }} %
{{ hp.fat_pop(f.class) if f.class else "" }}
{% else %}
—
needs {{ f.missing | join(", ") }}
{% endif %}
{% endfor %}
{% endif %}{% endmacro %}
{% macro body_ratios(V) %}{% set B = V.body %}{% set b, p = B.body, B.prof %}{% if B.rows %}
Ratios & energy
Waist ÷ height
{% if b.whtr %}{{ n(b.whtr, 2) }} · {{ b.whtr_class }} (under 0.5 is the usual aim){% else %}needs waist{{ " and height" if not p.height }}{% endif %}
Waist ÷ hips
{% if b.whr %}{{ n(b.whr, 2) }} · {{ b.whr_class }} (WHO: under {{ "0.85" if p.sex == "female" else "0.90" }}){% else %}needs waist and hips{% endif %}
{% if b.fat_kg %}
Fat · lean
{{ n(b.fat_kg) }} kg · {{ n(b.lean_kg) }} kg (from the body-fat middle value)
{% endif %}
{% endif %}{% endmacro %}
{# ======================= sleep ======================= #}
{% macro sleep_asleep(V) %}{% set S = V.sleep %}{% if S.night %}
Asleep{{ hm(S.avg.asleep) }}average · goal {{ "%g" | format(S.goal) }} h{% endif %}{% endmacro %}
{% macro sleep_deep(V) %}{% set S = V.sleep %}{% if S.night %}
Deep sleep{% if S.avg.deep is not none %}{{ S.avg.deep | round | int }} %{% else %}—{% endif %}typical 13–23 %{% endif %}{% endmacro %}
{% macro sleep_score(V) %}{% set S = V.sleep %}{% if S.night %}
Score{{ S.avg.score | round | int if S.avg.score is not none else "—" }}efficiency {{ ((S.avg.eff or 0) * 100) | round | int }} %{% endif %}{% endmacro %}
{% macro sleep_times(V) %}{% set S = V.sleep %}{% if S.night %}
Usual times{{ clock(S.avg.bed) }} → {{ clock(S.avg.wake) }}bed → up{% endif %}{% endmacro %}
{% macro sleep_stages(V) %}{% set S = V.sleep %}{% if S.night %}
{{ hp.title("Stages per " ~ ("night" if S.grain == "d" else "week"), "Each bar is one night (weekly: the average night of that week), split into the band's stages; naps of that day on top. Dashed line: your sleep goal. Click a night to open it.") }}
{% for st in ("deep", "light", "asleep", "awake", "nap") %}{{ S.stage_names[st] }}{% endfor %}
{% set c = S.stack %}
{% if c %}{% else %}
Nothing in this period.
{% endif %}
{% endif %}{% endmacro %}
{% macro sleep_window(V) %}{% set S = V.sleep %}{% if S.night %}
{{ hp.title("Bedtime → wake-up" ~ (" · average per week" if S.grain == "w" else ""), "One bar per night from falling asleep to waking up (weekly: the week's average bedtime → average wake-up). Red = under 7 h asleep. Click a night to open it.") }}
{{ hp.window(S.window, "/health/sleep" ~ dhref(S)) }}
{% endif %}{% endmacro %}
{% macro sleep_score_chart(V) %}{% set S = V.sleep %}{% if S.night %}
{{ hp.title("Score per " ~ ("night" if S.grain == "d" else "week"), "Our 0–100 sleep score (duration 50, deep sleep 20, efficiency 15, interruptions 15). Weekly = the average. Click a night to open it.") }}
{{ hp.chart(S.score_chart, "score", ("/health/sleep" ~ dhref(S)) if S.grain == "d" else None) }}
{% endif %}{% endmacro %}
{% macro sleep_findings(V) %}{% set S = V.sleep %}{% if S.night and S.findings %}
{{ hp.title("What stands out", "Worked out from your nights in this period by fixed rules (no AI): averages compared with common guidance. From the band's sleep tracking — its stages are an estimate, not a sleep lab. Not medical advice.") }}
{% for k, text in S.findings %}
{{ k }}
{{ text }}
{% endfor %}
{% endif %}{% endmacro %}
{% macro sleep_weekday(V) %}{% set S = V.sleep %}{% if S.night %}
{{ hp.title("By weekday", "Grouped by the evening you went to bed.") }}
Asleep
Bed
Score
{% for r in S.weekdays %}
{{ r.name }}
{{ hm(r.asleep) }}
{{ clock(r.bed) }}
{{ r.score | round | int if r.score is not none else "—" }}
{% endfor %}
{% endif %}{% endmacro %}
{# ======================= heart ======================= #}
{% macro heart_resting(V) %}{% set Hh = V.heart %}{% if Hh.last %}
Resting{{ n(Hh.rest7, 0) if Hh.rest7 else "—" }} bpm7-day average · 30 days {{ n(Hh.rest30, 0) if Hh.rest30 else "—" }}{% endif %}{% endmacro %}
{% macro heart_lowest(V) %}{% set Hh = V.heart %}{% if Hh.last %}
Lowest, 30 days{{ n(Hh.low30, 0) }} bpmaverage · lowest {{ Hh.lowest30 or "—" }} · 7 days {{ n(Hh.low7, 0) }}{% endif %}{% endmacro %}
{% macro heart_highest(V) %}{% set Hh = V.heart %}{% if Hh.last %}
Highest, 30 days{{ Hh.high30 or "—" }} bpmworkouts, mostly{% endif %}{% endmacro %}
{% macro heart_bp(V) %}{% set x = V.heart.vit %}
Blood pressure
{% if x.last_bp %}{{ x.last_bp.sys }}/{{ x.last_bp.dia }} mmHg
{{ cls(x.last_bp_class) }}{% if x.bp30 and x.bp30[2] > 1 %} · 30-day average {{ x.bp30[0] }}/{{ x.bp30[1] }} ({{ x.bp30[2] }}){% endif %}
{{ dm(x.last_bp.t.date()) }} {{ x.last_bp.t.strftime("%H:%M") }}
{% else %}—{% endif %}{% endmacro %}
{% macro heart_ox(V) %}{% set x = V.heart.vit %}
Blood oxygen
{% if x.last_ox %}{{ "%g" | format(x.last_ox.spo2) }} %
{{ cls(x.last_ox_class) }} · spot check
{{ dm(x.last_ox.t.date()) }} {{ x.last_ox.t.strftime("%H:%M") }}
{% else %}—none yet{% endif %}{% endmacro %}
{% macro heart_chart(V) %}{% set Hh = V.heart %}
{% if Hh.last %}
{{ hp.title("Heart rate per " ~ {"h": "hour", "d": "day", "w": "week"}[Hh.grain], "Lowest and highest reading in each period, and the band's resting heart rate (Mi Fitness's daily value). Heart rate is read every 10 minutes all day, every minute when active. A low a few beats above your normal for several days often means poor sleep, stress, illness coming on — or the second half of the cycle.") }}
{{ hp.chart(Hh.chart, "heart") }}
{% else %}
No heart-rate data yet — it comes from the watch import.
{% endif %}{% endmacro %}
{% macro heart_bp_card(V) %}{% set x = V.heart.vit %}
{{ hp.title("Blood pressure", "Upper (systolic) and lower (diastolic) number, mmHg — the day's or week's average. ESC 2024 categories: under 120 / 70 non-elevated (green band), 120–139 / 70–89 elevated, 140 / 90 and up hypertension (red band), 180 / 110 and up severe; a reading takes the higher category of its two numbers. One reading says little: the average of several, on different days, sitting and rested, is what counts. Not medical advice.") }}
{% endif %}
{% endmacro %}
{% macro heart_ox_card(V) %}{% set x = V.heart.vit %}
{{ hp.title("Blood oxygen (SpO₂)", "Spot checks from the band (it doesn't measure all day) and readings you add. At rest 95–100 % is normal (green band), 91–94 % low, 90 % or less very low — see a doctor if that repeats. Wrist sensors read a couple of % low now and then (cold hands, movement, a loose strap): a single low reading isn't a verdict.") }}
{% if x.ox_chart %}{{ hp.chart(x.ox_chart, "oxygen") }}
{% else %}
No oxygen readings in this period{% if x.last_ox %} — the last one was {{ dm(x.last_ox.t.date()) }}{% endif %}.
{% endif %}
{% endmacro %}
{# ======================= activity ======================= #}
{% macro act_steps(V) %}{% set A = V.activity %}
Steps{{ n(A.steps_avg, 0) if A.steps_avg else "—" }}a day · {{ A.steps_days }} day{{ "s" if A.steps_days != 1 }} ({{ A.steps_band }} from the band){% endmacro %}
{% macro act_energy(V) %}{% set A = V.activity %}
Energy (estimate){{ n(A.energy_avg, 0) if A.energy_avg else "—" }} kcal
a day: resting {{ n(A.bmr, 0) }} + walking {{ n(A.walk_avg, 0) }} + workouts
How it's estimatedResting: Mifflin–St Jeor (weight, height, age, sex). Walking: steps outside workouts × your step length (41 % of height) × 0.5 kcal per kg per km. Workouts: active calories from heart rate (% of heart-rate reserve × your VO₂max × weight). Everyday movement that isn't steps isn't counted, so the real number is likely a bit higher.{% endmacro %}
{% macro act_workouts(V) %}{% set A = V.activity %}
Workouts{{ A.n }}{{ hm(A.minutes) }} · {{ n(A.kcal, 0) }} kcal active{% endmacro %}
{% macro act_fitness(V) %}{% set tr = V.activity.training[1] %}{% if tr %}
Fitness · form{{ n(tr.fitness, 0) }} · {{ tr.form_label[0] }}
fatigue {{ n(tr.fatigue, 0) }} · form {{ "%+.0f" | format(tr.form) }}
Fitness, fatigue, formFitness = your training load averaged over 6 weeks, fatigue = over 1 week (Banister's model). Form = fitness − fatigue: {{ tr.form_label[1] }}. Over +10 fresh · −10 to −30 building · below −30 overload.{% endif %}{% endmacro %}
{% macro act_vitality(V) %}{% set vi = V.activity.vitality[1] %}
Vitality{{ n(vi.week, 0) }} / {{ vi.goal }}intensity minutes, last 7 days
VitalityMinutes at moderate effort (40–59 % of heart-rate reserve) + 2 × minutes at vigorous effort (60 %+), from the whole day's heart rate. WHO / ACSM recommend at least 150 a week.{% endmacro %}
{% macro act_steps_chart(V) %}{% set A = V.activity %}
{{ hp.title("Steps", "The band's count when it has one; on days it doesn't, the phone's — never both added up. Weekly = average per day.") }}
{{ hp.chart(A.steps_chart, "steps") }}
{% endmacro %}
{% macro act_energy_chart(V) %}{% set A = V.activity %}
{{ hp.title("Energy", "Estimated burn per day (weekly = average per day): resting + walking + workouts. Not available per hour.") }}
{{ hp.chart(A.energy_chart, "energy") }}
{% endmacro %}
{% macro act_workout_time(V) %}{% set A = V.activity %}
{{ hp.title("Workout time", "Minutes of workouts per " ~ {"h": "hour", "d": "day", "w": "week"}[A.grain] ~ ".") }}
{{ hp.chart(A.week_chart, "workouts") }}
{% endmacro %}
{% macro act_training(V) %}{% set A = V.activity %}
{{ hp.title("Training", "Fitness (6-week load), fatigue (1-week load) and form (fitness − fatigue). Load per workout = Banister's TRIMP: minutes × effort, hard minutes weighted much more. Rising fitness = getting fitter; form far below 0 = time to rest.") }}
{{ hp.chart(A.training[0], "training") }}
{% endmacro %}
{% macro act_vitality_chart(V) %}{% set A = V.activity %}
{{ hp.title("Vitality", "Intensity minutes per " ~ ("week" if A.grain == "w" else "day") ~ ": moderate + 2 × vigorous, from all-day heart rate. Shaded: on track for 150 a week.") }}
{{ hp.chart(A.vitality[0], "vitality") }}
{% endmacro %}
{% macro act_workouts_list(V) %}{% set A = V.activity %}
{% set tip = ("Mi Fitness said " ~ ((A.device_vs_ours * 100) | round | int) ~ " % of our calories in total for the same workouts. " if A.device_vs_ours else "") ~ ("Check on " ~ A.walk_check.n ~ " GPS walks: ours = " ~ ((A.walk_check.ratio * 100) | round | int) ~ " % of the walking equation (speed + weight — very accurate on level ground). " if A.walk_check else "") ~ "Active = on top of resting, like watches show. Load = TRIMP; effect = load vs your usual (average of the 6 weeks before): easy under 0.6×, maintaining to 0.9×, improving to 1.3×, very hard above. Click a workout for its heart rate and zones." %}