Skip to content

Commit

Permalink
fixes #412
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 10, 2024
1 parent 3b8c9db commit 4fe050b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
16 changes: 9 additions & 7 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New

@app.get("/")
def get_todos(req):
add = Form(Group(mk_input(), Button("Add")),
hx_post="/", target_id=id_list, hx_swap="beforeend")
add = Form(hx_post="/", target_id=id_list, hx_swap="beforeend")(
Group(mk_input(), Button("Add"))
)
card = Card(Ul(*TODO_LIST, id=id_list),
header=add, footer=Div(id=id_curr)),
return Titled('Todo list', card)
Expand All @@ -42,11 +43,12 @@ def find_todo(id): return next(o for o in TODO_LIST if o.id==id)
@app.get("/edit/{id}")
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'),
hx_put="/", target_id=tid(id), id="edit")
fill_form(res, todo)
return res
res = Form(hx_put="/", target_id=tid(id), id="edit")(
Group(Input(id="title"), Button("Save")),
Hidden(id="id"),
CheckboxX(id="done", label='Done'),
)
return fill_form(res, todo)

@app.put("/")
def update(todo: TodoItem):
Expand Down
11 changes: 3 additions & 8 deletions examples/pep8_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ def before(req, sess):
@app.get("/login")
def login():
# This creates a form with two input fields, and a submit button.
frm = fh.Form(
frm = fh.Form(action='/login', method='post')(
fh.Input(id='name', placeholder='Name'),
fh.Input(id='pwd', type='password', placeholder='Password'),
fh.Button('login'),
action='/login',
method='post'
)
return fh.Titled("Login", frm)

Expand Down Expand Up @@ -147,14 +145,11 @@ def delete(id: int):
@app.get("/edit/{id}")
def edit(id: int):
# The `hx_put` attribute tells HTMX to send a PUT request when the form is submitted.
res = fh.Form(
res = fh.Form(hx_put="/", target_id=f'todo-{id}', id="edit")(
fh.Group(fh.Input(id="title"), fh.Button("Save")),
fh.Hidden(id="id"),
fh.CheckboxX(id="done", label='Done'),
fh.Textarea(id="details", name="details", rows=10),
hx_put="/",
target_id=f'todo-{id}',
id="edit"
)
# `fill_form` populates the form with existing todo data, and returns the result.
return fh.fill_form(res, todos[id])
Expand Down Expand Up @@ -182,4 +177,4 @@ def get_todo(id: int):
return fh.Div(fh.H2(todo.title), fh.Div(todo.details, cls="markdown"), btn)


fh.serve(port=8000)
fh.serve()
2 changes: 2 additions & 0 deletions fasthtml/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def attrmap_x(o):

# %% ../nbs/api/01_components.ipynb
def ft_html(tag: str, *c, id=None, cls=None, title=None, style=None, attrmap=None, valmap=None, ft_cls=FT, **kwargs):
ds,c = partition(c, risinstance(dict))
for d in ds: kwargs = {**kwargs, **d}
if attrmap is None: attrmap=fh_cfg.attrmap
if valmap is None: valmap =fh_cfg.valmap
kwargs['id'],kwargs['cls'],kwargs['title'],kwargs['style'] = id,cls,title,style
Expand Down
1 change: 1 addition & 0 deletions fasthtml/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from fastcore.utils import *
from fastcore.meta import delegates
from fastcore.xml import FT
from .common import *
from .components import *
from .xtend import *

Expand Down
29 changes: 29 additions & 0 deletions nbs/api/01_components.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@
"source": [
"#| export\n",
"def ft_html(tag: str, *c, id=None, cls=None, title=None, style=None, attrmap=None, valmap=None, ft_cls=FT, **kwargs):\n",
" ds,c = partition(c, risinstance(dict))\n",
" for d in ds: kwargs = {**kwargs, **d}\n",
" if attrmap is None: attrmap=fh_cfg.attrmap\n",
" if valmap is None: valmap =fh_cfg.valmap\n",
" kwargs['id'],kwargs['cls'],kwargs['title'],kwargs['style'] = id,cls,title,style\n",
Expand Down Expand Up @@ -258,6 +260,33 @@
"ft_html('a', **{'@click.away':1})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89d9960a",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<a @click.away=\"1\"></a>\n",
"\n",
"```"
],
"text/plain": [
"a((),{'@click.away': 1})"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft_html('a', {'@click.away':1})"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 1 addition & 0 deletions nbs/api/05_svg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"from fastcore.utils import *\n",
"from fastcore.meta import delegates\n",
"from fastcore.xml import FT\n",
"from fasthtml.common import *\n",
"from fasthtml.components import *\n",
"from fasthtml.xtend import *"
]
Expand Down

0 comments on commit 4fe050b

Please sign in to comment.