|
| 1 | +"""Tier 3 canonical example — an interactive length converter. |
| 2 | +
|
| 3 | +SSR + client-side Python (Pyodide bundle). Demonstrates `@app.local` |
| 4 | +reactive state, `data-bind-value` SSR hydration, `@app.client.callback` |
| 5 | +on `oninput`, `@app.client.on("ready")` for boot-time restore, |
| 6 | +`@app.server.rpc` with float args + dict return, and |
| 7 | +`violetear.storage.store` for cross-reload persistence. |
| 8 | +
|
| 9 | +Run: |
| 10 | +
|
| 11 | + python examples/03_interactive.py |
| 12 | +
|
| 13 | +Then open http://localhost:8000 in a browser. Edit any of the three |
| 14 | +fields (meters / feet / inches) — the other two update live. Toggle to |
| 15 | +"precise" mode and the conversion routes through a server RPC for more |
| 16 | +decimals. Refresh the page; the last values restore from localStorage. |
| 17 | +""" |
| 18 | + |
| 19 | +from dataclasses import dataclass |
| 20 | + |
| 21 | +from violetear import App, Document, StyleSheet |
| 22 | +from violetear.color import Colors |
| 23 | +from violetear.dom import Event |
| 24 | +from violetear.storage import store |
| 25 | +from violetear.units import px, rem |
| 26 | + |
| 27 | + |
| 28 | +app = App(title="Length Converter") |
| 29 | + |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# Reactive state |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +@app.local |
| 37 | +@dataclass |
| 38 | +class UiState: |
| 39 | + meters: float = 1.0 |
| 40 | + feet: float = 3.281 |
| 41 | + inches: float = 39.37 |
| 42 | + mode: str = "quick" |
| 43 | + |
| 44 | + |
| 45 | +# Rough client-only constants (quick mode). |
| 46 | +QUICK_FT_PER_M = 3.281 |
| 47 | +QUICK_IN_PER_M = 39.37 |
| 48 | + |
| 49 | + |
| 50 | +# --------------------------------------------------------------------------- |
| 51 | +# Server RPC — precise mode |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | + |
| 54 | + |
| 55 | +@app.server.rpc |
| 56 | +async def precise_convert(meters: float) -> dict: |
| 57 | + return { |
| 58 | + "feet": meters * 3.28083989501, |
| 59 | + "inches": meters * 39.3700787402, |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | +# Client-side helpers + callbacks |
| 65 | +# --------------------------------------------------------------------------- |
| 66 | + |
| 67 | + |
| 68 | +@app.client |
| 69 | +async def save_state(): |
| 70 | + store.last_state = { |
| 71 | + "meters": float(UiState.meters), |
| 72 | + "feet": float(UiState.feet), |
| 73 | + "inches": float(UiState.inches), |
| 74 | + "mode": str(UiState.mode), |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +@app.client |
| 79 | +async def recompute_from_meters(m: float): |
| 80 | + if str(UiState.mode) == "precise": |
| 81 | + result = await precise_convert(meters=m) |
| 82 | + UiState.feet = float(result["feet"]) |
| 83 | + UiState.inches = float(result["inches"]) |
| 84 | + else: |
| 85 | + UiState.feet = m * QUICK_FT_PER_M |
| 86 | + UiState.inches = m * QUICK_IN_PER_M |
| 87 | + |
| 88 | + |
| 89 | +@app.client.callback |
| 90 | +async def on_meters_change(event: Event): |
| 91 | + try: |
| 92 | + v = float(event.target.value) |
| 93 | + except (ValueError, TypeError): |
| 94 | + return |
| 95 | + UiState.meters = v |
| 96 | + await recompute_from_meters(v) |
| 97 | + await save_state() |
| 98 | + |
| 99 | + |
| 100 | +@app.client.callback |
| 101 | +async def on_feet_change(event: Event): |
| 102 | + try: |
| 103 | + v = float(event.target.value) |
| 104 | + except (ValueError, TypeError): |
| 105 | + return |
| 106 | + UiState.feet = v |
| 107 | + # Anchor on meters so precise/quick paths share one code path. |
| 108 | + if str(UiState.mode) == "precise": |
| 109 | + m = v / 3.28083989501 |
| 110 | + result = await precise_convert(meters=m) |
| 111 | + UiState.meters = m |
| 112 | + UiState.inches = float(result["inches"]) |
| 113 | + else: |
| 114 | + m = v / QUICK_FT_PER_M |
| 115 | + UiState.meters = m |
| 116 | + UiState.inches = m * QUICK_IN_PER_M |
| 117 | + await save_state() |
| 118 | + |
| 119 | + |
| 120 | +@app.client.callback |
| 121 | +async def on_inches_change(event: Event): |
| 122 | + try: |
| 123 | + v = float(event.target.value) |
| 124 | + except (ValueError, TypeError): |
| 125 | + return |
| 126 | + UiState.inches = v |
| 127 | + if str(UiState.mode) == "precise": |
| 128 | + m = v / 39.3700787402 |
| 129 | + result = await precise_convert(meters=m) |
| 130 | + UiState.meters = m |
| 131 | + UiState.feet = float(result["feet"]) |
| 132 | + else: |
| 133 | + m = v / QUICK_IN_PER_M |
| 134 | + UiState.meters = m |
| 135 | + UiState.feet = m * QUICK_FT_PER_M |
| 136 | + await save_state() |
| 137 | + |
| 138 | + |
| 139 | +@app.client.callback |
| 140 | +async def on_mode_change(event: Event): |
| 141 | + UiState.mode = str(event.target.value) |
| 142 | + # Re-derive the other two from current meters so the displayed values |
| 143 | + # immediately reflect the precision of the chosen mode. |
| 144 | + await recompute_from_meters(float(UiState.meters)) |
| 145 | + await save_state() |
| 146 | + |
| 147 | + |
| 148 | +@app.client.on("ready") |
| 149 | +async def restore(): |
| 150 | + saved = store.last_state |
| 151 | + if saved is None: |
| 152 | + return |
| 153 | + # Each lookup on a missing key returns None — skip in that case. |
| 154 | + if saved.meters is not None: |
| 155 | + UiState.meters = float(saved.meters) |
| 156 | + if saved.feet is not None: |
| 157 | + UiState.feet = float(saved.feet) |
| 158 | + if saved.inches is not None: |
| 159 | + UiState.inches = float(saved.inches) |
| 160 | + if saved.mode is not None: |
| 161 | + UiState.mode = str(saved.mode) |
| 162 | + |
| 163 | + |
| 164 | +# --------------------------------------------------------------------------- |
| 165 | +# Stylesheet — flat class selectors only (gap 7.1) |
| 166 | +# --------------------------------------------------------------------------- |
| 167 | + |
| 168 | + |
| 169 | +sheet = StyleSheet(normalize=True) |
| 170 | + |
| 171 | +sheet.select("body").font( |
| 172 | + size=rem(1.0), family="system-ui, -apple-system, 'Segoe UI', sans-serif" |
| 173 | +).color(Colors.DarkSlateGray).background(Colors.WhiteSmoke).padding(rem(2.5)) |
| 174 | + |
| 175 | +sheet.select(".page").rules(max_width="520px").margin("auto").background( |
| 176 | + Colors.White |
| 177 | +).padding(rem(2.5)).rounded(px(8)).border(px(1), Colors.Gainsboro) |
| 178 | + |
| 179 | +sheet.select(".page-title").font(size=rem(2.0), weight=700).color(Colors.Indigo).margin( |
| 180 | + bottom=rem(0.25) |
| 181 | +) |
| 182 | + |
| 183 | +sheet.select(".subtitle").color(Colors.SlateGray).font(size=rem(1.0)).margin( |
| 184 | + bottom=rem(2.0) |
| 185 | +) |
| 186 | + |
| 187 | +sheet.select(".converter").flexbox(direction="column", gap=px(14)) |
| 188 | + |
| 189 | +sheet.select(".field").flexbox(direction="column", gap=px(4)) |
| 190 | +sheet.select(".field-label").font(size=rem(0.875), weight=600).color( |
| 191 | + Colors.DarkSlateGray |
| 192 | +) |
| 193 | +sheet.select(".field-input").rules( |
| 194 | + padding="8px 10px", |
| 195 | + border=f"1px solid {Colors.Gainsboro}", |
| 196 | + font_size="1rem", |
| 197 | + font_family="ui-monospace, monospace", |
| 198 | +).rounded(px(4)) |
| 199 | + |
| 200 | +sheet.select(".mode-row").flexbox(direction="row", gap=px(16), align="center").margin( |
| 201 | + top=rem(1.0) |
| 202 | +) |
| 203 | +sheet.select(".mode-option").flexbox(direction="row", gap=px(6), align="center").font( |
| 204 | + size=rem(0.9) |
| 205 | +).color(Colors.DarkSlateGray) |
| 206 | +sheet.select(".mode-input").rules(cursor="pointer") |
| 207 | + |
| 208 | +sheet.select(".footer").margin(top=rem(1.5)).color(Colors.SlateGray).font( |
| 209 | + size=rem(0.8), family="ui-monospace, monospace" |
| 210 | +) |
| 211 | +sheet.select(".footer-mode").font(weight=700).color(Colors.Indigo) |
| 212 | + |
| 213 | + |
| 214 | +# --------------------------------------------------------------------------- |
| 215 | +# View |
| 216 | +# --------------------------------------------------------------------------- |
| 217 | + |
| 218 | + |
| 219 | +@app.view("/") |
| 220 | +def index(): |
| 221 | + doc = Document(title="Length Converter") |
| 222 | + doc.style(href="/style.css", sheet=sheet) |
| 223 | + |
| 224 | + with doc.body as body: |
| 225 | + with body.div(classes="page") as page: |
| 226 | + page.h1(text="Length Converter").classes("page-title") |
| 227 | + page.p( |
| 228 | + text="Edit any field — the other two update live. Switch modes for precision." |
| 229 | + ).classes("subtitle") |
| 230 | + |
| 231 | + with page.div(classes="converter") as form: |
| 232 | + # Meters |
| 233 | + with form.div(classes="field") as field: |
| 234 | + with field.label(text="Meters", classes="field-label") as lbl: |
| 235 | + lbl.input(classes="field-input").attrs( |
| 236 | + type="number", step="0.01" |
| 237 | + ).value(UiState.meters).on("input", on_meters_change) |
| 238 | + |
| 239 | + # Feet |
| 240 | + with form.div(classes="field") as field: |
| 241 | + with field.label(text="Feet", classes="field-label") as lbl: |
| 242 | + lbl.input(classes="field-input").attrs( |
| 243 | + type="number", step="0.01" |
| 244 | + ).value(UiState.feet).on("input", on_feet_change) |
| 245 | + |
| 246 | + # Inches |
| 247 | + with form.div(classes="field") as field: |
| 248 | + with field.label(text="Inches", classes="field-label") as lbl: |
| 249 | + lbl.input(classes="field-input").attrs( |
| 250 | + type="number", step="0.01" |
| 251 | + ).value(UiState.inches).on("input", on_inches_change) |
| 252 | + |
| 253 | + # Mode toggle — two radios sharing name="mode". |
| 254 | + with form.div(classes="mode-row") as modes: |
| 255 | + with modes.label(text="quick", classes="mode-option") as lbl: |
| 256 | + lbl.input(classes="mode-input").attrs( |
| 257 | + type="radio", |
| 258 | + name="mode", |
| 259 | + value="quick", |
| 260 | + checked="checked", |
| 261 | + ).on("change", on_mode_change) |
| 262 | + with modes.label(text="precise", classes="mode-option") as lbl: |
| 263 | + lbl.input(classes="mode-input").attrs( |
| 264 | + type="radio", name="mode", value="precise" |
| 265 | + ).on("change", on_mode_change) |
| 266 | + |
| 267 | + with page.div(classes="footer") as footer: |
| 268 | + footer.span(text="mode: ") |
| 269 | + footer.span(classes="footer-mode").text(UiState.mode) |
| 270 | + |
| 271 | + return doc |
| 272 | + |
| 273 | + |
| 274 | +if __name__ == "__main__": |
| 275 | + app.run() |
0 commit comments