Skip to content

Commit

Permalink
remove adv_app async
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 6, 2024
1 parent 3c78d94 commit 5095fd4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def __ft__(self):
def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", **kw)

@app.get("/")
async def get_todos(req):
def get_todos(req):
add = Form(Group(mk_input(), Button("Add")),
hx_post="/", target_id=id_list, hx_swap="beforeend")
card = Card(Ul(*TODO_LIST, id=id_list),
header=add, footer=Div(id=id_curr)),
return Titled('Todo list', card)

@app.post("/")
async def add_item(todo:TodoItem):
def add_item(todo:TodoItem):
todo.id = len(TODO_LIST)+1
TODO_LIST.append(todo)
return todo, mk_input(hx_swap_oob='true')
Expand All @@ -40,7 +40,7 @@ def clr_details(): return Div(hx_swap_oob='innerHTML', id=id_curr)
def find_todo(id): return next(o for o in TODO_LIST if o.id==id)

@app.get("/edit/{id}")
async def edit_item(id:int):
def edit_item(id:int):
todo = find_todo(id)
res = Form(Group(Input(id="title"), Button("Save")),
Hidden(id="id"), CheckboxX(id="done", label='Done'),
Expand All @@ -49,17 +49,17 @@ async def edit_item(id:int):
return res

@app.put("/")
async def update(todo: TodoItem):
def update(todo: TodoItem):
fill_dataclass(todo, find_todo(todo.id))
return todo, clr_details()

@app.delete("/todos/{id}")
async def del_todo(id:int):
def del_todo(id:int):
TODO_LIST.remove(find_todo(id))
return clr_details()

@app.get("/todos/{id}")
async def get_todo(id:int):
def get_todo(id:int):
todo = find_todo(id)
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
target_id=tid(todo.id), hx_swap="outerHTML")
Expand Down
14 changes: 7 additions & 7 deletions examples/db_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def tid(id): return f'todo-{id}'
rt = app.route

@rt("/{fname:path}.{ext:static}")
async def get(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')
def get(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')

@patch
def __ft__(self:Todo):
Expand All @@ -27,33 +27,33 @@ def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New
def clr_details(): return Div(hx_swap_oob='innerHTML', id=id_curr)

@rt("/")
async def get(request):
def get(request):
add = Form(Group(mk_input(), Button("Add")),
hx_post="/", target_id='todo-list', hx_swap="beforeend")
card = Card(Ul(*todos(), id='todo-list'),
header=add, footer=Div(id=id_curr)),
return Titled('Todo list', card)

@rt("/todos/{id}")
async def delete(id:int):
def delete(id:int):
todos.delete(id)
return clr_details()

@rt("/")
async def post(todo:Todo): return todos.insert(todo), mk_input(hx_swap_oob='true')
def post(todo:Todo): return todos.insert(todo), mk_input(hx_swap_oob='true')

@rt("/edit/{id}")
async def get(id:int):
def get(id:int):
res = Form(Group(Input(id="title"), Button("Save")),
Hidden(id="id"), CheckboxX(id="done", label='Done'),
hx_put="/", target_id=tid(id), id="edit")
return fill_form(res, todos[id])

@rt("/")
async def put(todo: Todo): return todos.update(todo), clr_details()
def put(todo: Todo): return todos.update(todo), clr_details()

@rt("/todos/{id}")
async def get(id:int):
def get(id:int):
todo = todos[id]
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
target_id=tid(todo.id), hx_swap="outerHTML")
Expand Down
10 changes: 5 additions & 5 deletions examples/pep8_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def logout(sess):

# FastHTML uses Starlette's path syntax, and adds a `static` type.
@app.get("/{fname:path}.{ext:static}")
async def static(fname: str, ext: str):
def static(fname: str, ext: str):
return fh.FileResponse(f'{fname}.{ext}')


Expand Down Expand Up @@ -145,7 +145,7 @@ def delete(id: int):


@app.get("/edit/{id}")
async def edit(id: int):
def edit(id: int):
# The `hx_put` attribute tells HTMX to send a PUT request when the form is submitted.
res = fh.Form(
fh.Group(fh.Input(id="title"), fh.Button("Save")),
Expand All @@ -161,20 +161,20 @@ async def edit(id: int):


@app.put("/")
async def put(todo: Todo):
def put(todo: Todo):
return todos.upsert(todo), clr_details()


@app.post("/")
async def post(todo: Todo):
def post(todo: Todo):
# This is used to clear the input field after adding the new todo.
new_inp = fh.Input(id="new-title", name="title", placeholder="New Todo", hx_swap_oob='true')
# `insert` returns the inserted todo, which is appended to the start of the list.
return todos.insert(todo), new_inp


@app.get("/todos/{id}")
async def get_todo(id: int):
def get_todo(id: int):
todo = todos[id]
btn = fh.Button('delete', hx_delete=f'/todos/{todo.id}', target_id=f'todo-{todo.id}', hx_swap="outerHTML")
# The "markdown" class is used here because that's the CSS selector we used in the JS earlier.
Expand Down
14 changes: 7 additions & 7 deletions examples/user_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def before(auth): todos.xtra(name=auth)
rt = app.route

@rt("/{fname:path}.{ext:static}")
async def get(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')
def get(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')

@patch
def __ft__(self:Todo):
Expand All @@ -39,7 +39,7 @@ def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New
def clr_details(): return Div(hx_swap_oob='innerHTML', id=id_curr)

@rt("/")
async def get(request, auth):
def get(request, auth):
add = Form(Group(mk_input(), Button("Add")),
hx_post="/", target_id='todo-list', hx_swap="beforeend")
card = Card(Ul(*todos(), id='todo-list'),
Expand All @@ -48,27 +48,27 @@ async def get(request, auth):
return Titled(f"{auth}'s todo list", top, card)

@rt("/todos/{id}")
async def delete(id:int):
def delete(id:int):
todos.delete(id)
return clr_details()

@rt("/")
async def post(todo:Todo):
def post(todo:Todo):
return todos.insert(todo), mk_input(hx_swap_oob='true')

@rt("/edit/{id}")
async def get(id:int):
def get(id:int):
res = Form(Group(Input(id="title"), Button("Save")),
Hidden(id="id"), CheckboxX(id="done", label='Done'),
hx_put="/", target_id=tid(id), id="edit")
return fill_form(res, todos[id])

@rt("/")
async def put(todo: Todo):
def put(todo: Todo):
return todos.upsert(todo), clr_details()

@rt("/todos/{id}")
async def get(id:int):
def get(id:int):
todo = todos[id]
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
target_id=tid(todo.id), hx_swap="outerHTML")
Expand Down

0 comments on commit 5095fd4

Please sign in to comment.