Spaces:
Sleeping
Sleeping
File size: 5,129 Bytes
036b3a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# FastHTML Best Practices
FastHTML applications are different to applications using FastAPI/react,
Django, etc. Don’t assume that FastHTML best practices are the same as
those for other frameworks. Best practices embody the fast.ai
philosophy: remove ceremony, leverage smart defaults, and write code
that’s both concise and clear. The following are some particular
opportunities that both humans and language models sometimes miss:
## Database Table Creation
**Before:**
``` python
todos = db.t.todos
if not todos.exists():
todos.create(id=int, task=str, completed=bool, created=str, pk='id')
```
**After:**
``` python
class Todo: id:int; task:str; completed:bool; created:str
todos = db.create(Todo)
```
FastLite’s `create()` is idempotent - it creates the table if needed and
returns the table object either way. Using a dataclass-style definition
is cleaner and more Pythonic. The `id` field is automatically the
primary key.
## Route Naming Conventions
**Before:**
``` python
@rt("/")
def get(): return Titled("Todo List", ...)
@rt("/add")
def post(task: str): ...
```
**After:**
``` python
@rt
def index(): return Titled("Todo List", ...) # Special name for "/"
@rt
def add(task: str): ... # Function name becomes route
```
Use `@rt` without arguments and let the function name define the route.
The special name `index` maps to `/`.
## Query Parameters over Path Parameters
**Before:**
``` python
@rt("/toggle/{todo_id}")
def post(todo_id: int): ...
# URL: /toggle/123
```
**After:**
``` python
@rt
def toggle(id: int): ...
# URL: /toggle?id=123
```
Query parameters are more idiomatic in FastHTML and avoid duplicating
param names in the path.
## Leverage Return Values
<div class="column-body-outset">
**Before:**
``` python
@rt
def add(task: str):
new_todo = todos.insert(task=task, completed=False, created=datetime.now().isoformat())
return todo_item(todos[new_todo])
@rt
def toggle(id: int):
todo = todos[id]
todos.update(completed=not todo.completed, id=id)
return todo_item(todos[id])
```
**After:**
``` python
@rt
def add(task: str):
return todo_item(todos.insert(task=task, completed=False, created=datetime.now().isoformat()))
@rt
def toggle(id: int):
return todo_item(todos.update(completed=not todos[id].completed, id=id))
```
Both `insert()` and `update()` return the affected object, enabling
functional chaining.
</div>
## Use `.to()` for URL Generation
**Before:**
``` python
hx_post=f"/toggle?id={todo.id}"
```
**After:**
``` python
hx_post=toggle.to(id=todo.id)
```
The `.to()` method generates URLs with type safety and is
refactoring-friendly.
## PicoCSS comes free
**Before:**
``` python
style = Style("""
.todo-container { max-width: 600px; margin: 0 auto; padding: 20px; }
/* ... many more lines ... */
""")
```
**After:**
``` python
# Just use semantic HTML - Pico styles it automatically
Container(...), Article(...), Card(...), Group(...)
```
`fast_app()` includes PicoCSS by default. Use semantic HTML elements
that Pico styles automatically. Use MonsterUI (like shadcn, but for
FastHTML) for more complex UI needs.
## Smart Defaults
**Before:**
``` python
return Titled("Todo List", Container(...))
if __name__ == "__main__":
serve()
```
**After:**
``` python
return Titled("Todo List", ...) # Container is automatic
serve() # No need for if __name__ guard
```
`Titled` already wraps content in a `Container`, and `serve()` handles
the main check internally.
## FastHTML Handles Iterables
**Before:**
``` python
Section(*[todo_item(todo) for todo in all_todos], id="todo-list")
```
**After:**
``` python
Section(map(todo_item, all_todos), id="todo-list")
```
FastHTML components accept iterables directly - no need to unpack with
`*`.
## Functional Patterns
List comprehensions are great, but `map()` is often cleaner for simple
transformations, especially when combined with FastHTML’s iterable
handling.
## Minimal Code
**Before:**
``` python
@rt
def delete(id: int):
# Delete from database
todos.delete(id)
# Return empty response
return ""
```
**After:**
``` python
@rt
def delete(id: int): todos.delete(id)
```
- Skip comments when code is self-documenting
- Don’t return empty strings - `None` is returned by default
- Use a single line for a single idea.
## Use POST for All Mutations
**Before:**
``` python
hx_delete=f"/delete?id={todo.id}"
```
**After:**
``` python
hx_post=delete.to(id=todo.id)
```
FastHTML routes handle only GET and POST by default. Using only these
two verbs is more idiomatic and simpler.
## Modern HTMX Event Syntax
**Before:**
``` python
hx_on="htmx:afterRequest: this.reset()"
```
**After:**
``` python
hx_on__after_request="this.reset()"
```
This works because:
- `hx-on="event: code"` is deprecated; `hx-on-event="code"` is preferred
- FastHTML converts `_` to `-` (so `hx_on__after_request` becomes
`hx-on--after-request`)
- `::` in HTMX can be used as a shortcut for `:htmx:`.
- HTMX natively accepts `-` instead of `:` (so `-htmx-` works like
`:htmx:`)
- HTMX accepts e.g `after-request` as an alternative to camelCase
`afterRequest`
|