|
| 1 | +"""Tier 2 canonical example — a server-rendered guestbook. |
| 2 | +
|
| 3 | +SSR-only, no client-side Python. Demonstrates `App`, `@app.view` for GET |
| 4 | +routes, raw `@app.api.post` with FastAPI's `Form(...)` for form-driven |
| 5 | +mutation, `doc.style(href=..., sheet=...)` for auto-served CSS, and the |
| 6 | +`with element as builder:` markup pattern. |
| 7 | +
|
| 8 | +Run: |
| 9 | +
|
| 10 | + python examples/02_ssr.py |
| 11 | +
|
| 12 | +Then open http://localhost:8000 in a browser. Submit the form and the new |
| 13 | +entry shows up on reload (303 redirect back to `/`). The store is in-memory |
| 14 | +and resets on restart — intentional simplicity. |
| 15 | +""" |
| 16 | + |
| 17 | +from datetime import datetime |
| 18 | + |
| 19 | +from fastapi import Form |
| 20 | +from fastapi.responses import RedirectResponse |
| 21 | + |
| 22 | +from violetear import App, Document, HTML, StyleSheet |
| 23 | +from violetear.color import Colors |
| 24 | +from violetear.units import px, rem |
| 25 | + |
| 26 | + |
| 27 | +app = App(title="Guestbook") |
| 28 | + |
| 29 | +# In-memory store. Each entry is {name, message, timestamp}. Resets on restart. |
| 30 | +entries: list[dict] = [] |
| 31 | + |
| 32 | + |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | +# Stylesheet — flat class selectors only (no descendant combinators yet) |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | + |
| 37 | +sheet = StyleSheet(normalize=True) |
| 38 | + |
| 39 | +sheet.select("body").font( |
| 40 | + size=rem(1.0), family="system-ui, -apple-system, 'Segoe UI', sans-serif" |
| 41 | +).color(Colors.DarkSlateGray).background(Colors.WhiteSmoke).padding(rem(2.5)) |
| 42 | + |
| 43 | +sheet.select(".page").rules(max_width="640px").margin("auto").background( |
| 44 | + Colors.White |
| 45 | +).padding(rem(2.5)).rounded(px(8)).border(px(1), Colors.Gainsboro) |
| 46 | + |
| 47 | +sheet.select(".page-title").font(size=rem(2.0), weight=700).color(Colors.Indigo).margin( |
| 48 | + bottom=rem(0.25) |
| 49 | +) |
| 50 | + |
| 51 | +sheet.select(".subtitle").color(Colors.SlateGray).font(size=rem(1.0)).margin( |
| 52 | + bottom=rem(2.0) |
| 53 | +) |
| 54 | + |
| 55 | +sheet.select(".section-heading").font(size=rem(1.25), weight=600).color( |
| 56 | + Colors.DarkSlateGray |
| 57 | +).margin(top=rem(2.0), bottom=rem(1.0)) |
| 58 | + |
| 59 | +# Form |
| 60 | +sheet.select(".guestbook-form").flexbox(direction="column", gap=px(12)) |
| 61 | +sheet.select(".form-field").flexbox(direction="column", gap=px(4)) |
| 62 | +sheet.select(".form-label").font(size=rem(0.875), weight=600).color( |
| 63 | + Colors.DarkSlateGray |
| 64 | +) |
| 65 | +sheet.select(".form-input").rules( |
| 66 | + padding="8px 10px", border=f"1px solid {Colors.Gainsboro}", font_size="1rem" |
| 67 | +).rounded(px(4)) |
| 68 | +sheet.select(".form-textarea").rules( |
| 69 | + padding="8px 10px", |
| 70 | + border=f"1px solid {Colors.Gainsboro}", |
| 71 | + font_size="1rem", |
| 72 | + min_height="80px", |
| 73 | + font_family="inherit", |
| 74 | +).rounded(px(4)) |
| 75 | +sheet.select(".form-submit").rules( |
| 76 | + padding="10px 16px", |
| 77 | + border="none", |
| 78 | + cursor="pointer", |
| 79 | + font_size="1rem", |
| 80 | + font_weight=600, |
| 81 | +).background(Colors.Indigo).color(Colors.White).rounded(px(4)) |
| 82 | + |
| 83 | +# Entries |
| 84 | +sheet.select(".entries").flexbox(direction="column", gap=px(12)) |
| 85 | +sheet.select(".entries-empty").color(Colors.SlateGray).font(size=rem(0.875)).rules( |
| 86 | + font_style="italic" |
| 87 | +) |
| 88 | +sheet.select(".entry").padding(rem(1.0)).background(Colors.WhiteSmoke).rounded( |
| 89 | + px(6) |
| 90 | +).border(px(1), Colors.Gainsboro) |
| 91 | +sheet.select(".entry-header").flexbox( |
| 92 | + direction="row", gap=px(12), align="baseline" |
| 93 | +).margin(bottom=rem(0.5)) |
| 94 | +sheet.select(".entry-name").font(weight=700).color(Colors.Indigo) |
| 95 | +sheet.select(".entry-time").font( |
| 96 | + size=rem(0.75), family="ui-monospace, monospace" |
| 97 | +).color(Colors.SlateGray) |
| 98 | +sheet.select(".entry-message").color(Colors.DarkSlateGray).rules( |
| 99 | + white_space="pre-wrap", line_height=1.5 |
| 100 | +) |
| 101 | + |
| 102 | + |
| 103 | +# --------------------------------------------------------------------------- |
| 104 | +# Routes |
| 105 | +# --------------------------------------------------------------------------- |
| 106 | + |
| 107 | + |
| 108 | +@app.view("/") |
| 109 | +def index(): |
| 110 | + doc = Document(title="Guestbook") |
| 111 | + doc.style(href="/style.css", sheet=sheet) |
| 112 | + |
| 113 | + with doc.body as body: |
| 114 | + with body.div(classes="page") as page: |
| 115 | + page.h1(text="Guestbook").classes("page-title") |
| 116 | + page.p( |
| 117 | + text="Leave a note. It will stay until the server restarts." |
| 118 | + ).classes("subtitle") |
| 119 | + |
| 120 | + # New-entry form — POSTs to /entries (handled by FastAPI below). |
| 121 | + # We use the nested-label pattern (<label>Text<input/></label>) instead |
| 122 | + # of for=/id= pairing — violetear's .attrs() renders kwargs literally, so |
| 123 | + # `for_=` leaks as `for_="..."` in the HTML (see gap 7.4). |
| 124 | + page.h2(text="Sign the book").classes("section-heading") |
| 125 | + with page.form(classes="guestbook-form").attrs( |
| 126 | + method="post", action="/entries" |
| 127 | + ) as form: |
| 128 | + with form.div(classes="form-field") as field: |
| 129 | + with field.label(text="Name", classes="form-label") as lbl: |
| 130 | + lbl.input(classes="form-input").attrs( |
| 131 | + type="text", name="name", required="required" |
| 132 | + ) |
| 133 | + with form.div(classes="form-field") as field: |
| 134 | + with field.label(text="Message", classes="form-label") as lbl: |
| 135 | + lbl.textarea(classes="form-textarea").attrs( |
| 136 | + name="message", required="required" |
| 137 | + ) |
| 138 | + form.button(text="Add entry", classes="form-submit").attrs( |
| 139 | + type="submit" |
| 140 | + ) |
| 141 | + |
| 142 | + # Existing entries — newest first. |
| 143 | + page.h2(text="Entries").classes("section-heading") |
| 144 | + with page.div(classes="entries") as entry_list: |
| 145 | + if not entries: |
| 146 | + entry_list.div( |
| 147 | + text="No entries yet — be the first.", |
| 148 | + classes="entries-empty", |
| 149 | + ) |
| 150 | + else: |
| 151 | + for entry in reversed(entries): |
| 152 | + with entry_list.div(classes="entry") as card: |
| 153 | + with card.div(classes="entry-header") as header: |
| 154 | + header.span(text=entry["name"], classes="entry-name") |
| 155 | + header.span( |
| 156 | + text=entry["timestamp"], classes="entry-time" |
| 157 | + ) |
| 158 | + card.div(text=entry["message"], classes="entry-message") |
| 159 | + |
| 160 | + return doc |
| 161 | + |
| 162 | + |
| 163 | +@app.api.post("/entries") |
| 164 | +async def add_entry(name: str = Form(...), message: str = Form(...)): |
| 165 | + entries.append( |
| 166 | + { |
| 167 | + "name": name.strip(), |
| 168 | + "message": message.strip(), |
| 169 | + "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"), |
| 170 | + } |
| 171 | + ) |
| 172 | + return RedirectResponse(url="/", status_code=303) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + app.run() |
0 commit comments