Skip to content

Commit ca7c9de

Browse files
committed
rm async
1 parent a1c6d02 commit ca7c9de

File tree

7 files changed

+116
-21
lines changed

7 files changed

+116
-21
lines changed

01_todo_app/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Here is how we setup the form for adding new todos:
6363
def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", **kw)
6464

6565
@rt("/")
66-
async def get():
66+
def get():
6767
add = Form(Group(mk_input(), Button("Add")),
6868
hx_post="/", target_id='todo-list', hx_swap="beforeend")
6969
card = Card(Ul(*todos(), id='todo-list'),
@@ -80,7 +80,7 @@ Editing a todo item is handled by this route which takes the todo's id as a para
8080

8181
```python
8282
@rt("/edit/{id}")
83-
async def get(id:int):
83+
def get(id:int):
8484
res = Form(Group(Input(id="title"), Button("Save")),
8585
Hidden(id="id"), CheckboxX(id="done", label='Done'),
8686
hx_put="/", target_id=tid(id), id="edit")
@@ -95,7 +95,7 @@ Similarly, we can do the same for deleting todos, except we use the `delete` met
9595

9696
```python
9797
@rt("/todos/{id}")
98-
async def delete(id:int):
98+
def delete(id:int):
9999
todos.delete(id)
100100
return clear(id_curr)
101101
```

01_todo_app/main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __ft__(self:Todo):
1818
def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", required=True, **kw)
1919

2020
@rt("/")
21-
async def get():
21+
def get():
2222
add = Form(Group(mk_input(), Button("Add")),
2323
hx_post="/", target_id='todo-list', hx_swap="beforeend")
2424
card = Card(Ul(*todos(), id='todo-list'),
@@ -27,25 +27,25 @@ async def get():
2727
return Title(title), Main(H1(title), card, cls='container')
2828

2929
@rt("/todos/{id}")
30-
async def delete(id:int):
30+
def delete(id:int):
3131
todos.delete(id)
3232
return clear(id_curr)
3333

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

3737
@rt("/edit/{id}")
38-
async def get(id:int):
38+
def get(id:int):
3939
res = Form(Group(Input(id="title"), Button("Save")),
4040
Hidden(id="id"), CheckboxX(id="done", label='Done'),
4141
hx_put="/", target_id=tid(id), id="edit")
4242
return fill_form(res, todos.get(id))
4343

4444
@rt("/")
45-
async def put(todo: Todo): return todos.upsert(todo), clear(id_curr)
45+
def put(todo: Todo): return todos.upsert(todo), clear(id_curr)
4646

4747
@rt("/todos/{id}")
48-
async def get(id:int):
48+
def get(id:int):
4949
todo = todos.get(id)
5050
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
5151
target_id=tid(todo.id), hx_swap="outerHTML")

htmx/counter-rsjs.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from fasthtml.common import *
2+
3+
'''
4+
code = """
5+
me("[data-counter-output]", el).textContent++);
6+
"""
7+
scr = RsJsScript('counter', increment=('click', code))
8+
'''
9+
10+
scr = Script('''
11+
proc_htmx("[data-counter]", el => {
12+
me("[data-counter-increment]", el).on("click",
13+
e => me("[data-counter-output]", el).textContent++);
14+
});''')
15+
16+
app,rt = fast_app(hdrs=(scr,), live=True)
17+
18+
def Incrementer(start=0, btn_txt='Increment'):
19+
return Section(data_counter=True)(
20+
Output(start, id='my-output', data_counter_output=True),
21+
Button(btn_txt, data_counter_increment=True))
22+
23+
@rt("/")
24+
def get():
25+
return Titled('RSJS demo',
26+
Incrementer(),
27+
Incrementer(5, 'Do it')
28+
)
29+
30+
serve()
31+

htmx/counter-surreal.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fasthtml.common import *
2+
3+
app,rt = fast_app(live=True)
4+
5+
def Incrementer(start=0, btn_txt='Increment'):
6+
scr = On('''
7+
let sec = e.closest('section');
8+
me('output', sec).value++;
9+
''')
10+
return Section()(
11+
Output(start),
12+
Button(btn_txt, scr))
13+
14+
@rt("/")
15+
def get():
16+
return Titled('Surreal counter',
17+
Incrementer(),
18+
Incrementer(5, 'Do it')
19+
)
20+
21+
serve()
22+

htmx/svgswap.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from fasthtml.common import *
2+
from fasthtml.svg import *
3+
4+
app,rt = fast_app(live=True)
5+
6+
@rt
7+
def index():
8+
return Titled("HTMX SVG swaps",
9+
Svg(
10+
get_circle(0),
11+
Rect(x=5, y=85, width=10, height=10, fill="purple"),
12+
width=100, height=100, viewBox="0 0 100 100"
13+
),
14+
Hidden(id='ref', value='0'),
15+
Button('Multi', hx_get=change, hx_include='previous input'),
16+
Div(id='dest'),
17+
)
18+
19+
def get_circle(ref):
20+
opts = [
21+
Circle(id='circle1', cx=50, cy=50, r=20,
22+
hx_swap_oob='true', hx_get=foo, hx_swap='outerHTML', hx_select='svg>*'),
23+
G(hx_swap_oob='true', id='circle1')(
24+
Circle(id='circle1', r='45', cx='50', cy='50', fill='red'),
25+
Circle(id='circle1', r='25', cx='70', cy='70', fill='green')
26+
)
27+
]
28+
return opts[ref]
29+
30+
@rt
31+
def foo(): return Svg(Rect(x=50, y=50, width=30, height=30, fill='blue'))
32+
33+
@rt
34+
def change(ref:int):
35+
return (
36+
'Multi' if ref else 'Blue',
37+
Template(Svg(get_circle(1-ref))),
38+
Hidden(id='ref', value=1-ref, hx_swap_oob='true')
39+
)
40+
41+
serve()
42+

todos1/main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,33 @@ def __ft__(self:Todo):
1616
def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", **kw)
1717

1818
@rt("/")
19-
async def get():
19+
def get():
2020
add = Form(Group(mk_input(), Button("Add")),
2121
hx_post="/", target_id='todo-list', hx_swap="beforeend")
2222
card = Card(Ul(*todos(), id='todo-list'),
2323
header=add, footer=Div(id=id_curr)),
2424
return Titled('Todo list', card)
2525

2626
@rt("/todos/{id}")
27-
async def delete(id:int):
27+
def delete(id:int):
2828
todos.delete(id)
2929
return clear(id_curr)
3030

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

3434
@rt("/edit/{id}")
35-
async def get(id:int):
35+
def get(id:int):
3636
res = Form(Group(Input(id="title"), Button("Save")),
3737
Hidden(id="id"), CheckboxX(id="done", label='Done'),
3838
hx_put="/", target_id=tid(id), id="edit")
3939
return fill_form(res, todos.get(id))
4040

4141
@rt("/")
42-
async def put(todo: Todo): return todos.upsert(todo), clear(id_curr)
42+
def put(todo: Todo): return todos.upsert(todo), clear(id_curr)
4343

4444
@rt("/todos/{id}")
45-
async def get(id:int):
45+
def get(id:int):
4646
todo = todos.get(id)
4747
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
4848
target_id=tid(todo.id), hx_swap="outerHTML")

todos2-hf/main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New
2626
def clr_details(): return Div(hx_swap_oob='innerHTML', id='current-todo')
2727

2828
@rt("/")
29-
async def get(request, auth):
29+
def get(request, auth):
3030
add = Form(Group(mk_input(), Button("Add")),
3131
hx_post="/", target_id='todo-list', hx_swap="beforeend")
3232
card = Card(Ul(*todos(), id='todo-list'),
@@ -36,25 +36,25 @@ async def get(request, auth):
3636
return Title(title), Main(top, card, cls='container')
3737

3838
@rt("/todos/{id}")
39-
async def delete(id:int):
39+
def delete(id:int):
4040
todos.delete(id)
4141
return clr_details()
4242

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

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

5353
@rt("/")
54-
async def put(todo: Todo): return todos.upsert(todo), clr_details()
54+
def put(todo: Todo): return todos.upsert(todo), clr_details()
5555

5656
@rt("/todos/{id}")
57-
async def get(id:int):
57+
def get(id:int):
5858
todo = todos[id]
5959
btn = Button('delete', hx_delete=f'/todos/{todo.id}',
6060
target_id=tid(todo.id), hx_swap="outerHTML")

0 commit comments

Comments
 (0)