Skip to content

Commit

Permalink
Through GSFH 50:12
Browse files Browse the repository at this point in the history
  • Loading branch information
audreyfeldroy committed Aug 4, 2024
1 parent a1db4a3 commit 674cf6c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions apps/getting_started_w_fh/04_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from fasthtml.common import *


def render(todo):
tid = f"todo-{todo.id}"
toggle = A("Toggle", hx_get=f"/toggle/{todo.id}", target_id=tid)
delete = A("Delete", hx_delete=f"/{todo.id}", hx_swap="outerHTML", target_id=tid)
return Li(toggle, delete, todo.title + (" ✅" if todo.done else ""), id=tid)


app, rt, todos, Todo = fast_app(
"todos.db", live=True, render=render, id=int, title=str, done=bool, pk="id"
)


@rt("/")
def get():
frm = Form(Group(Input(placeholder="Add a new todo", name='title'), Button("Add")),
hx_post="/", target_id="todo-list", hx_swap="beforeend")
return Titled(
"Todos",
Card(
Ul(*todos(), id="todo-list"),
header=frm,
),
)

@rt("/")
def post(todo: Todo): return todos.insert(todo)

@rt("/{tid}")
def delete(tid: int): todos.delete(tid)


@rt("/toggle/{tid}")
def get(tid: int):
todo = todos[tid]
todo.done = not todo.done
todos.update(todo)
return todo


serve()

0 comments on commit 674cf6c

Please sign in to comment.