Skip to content

Commit a9a765b

Browse files
committed
Allow any Mapping children to specify attributes. fix #737
1 parent ad42bb8 commit a9a765b

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

fasthtml/components.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
# %% ../nbs/api/01_components.ipynb
2020
from dataclasses import dataclass, asdict, is_dataclass, make_dataclass, replace, astuple, MISSING
2121
from bs4 import BeautifulSoup, Comment
22-
from typing import Literal, Optional
22+
from collections import UserDict
23+
from typing import Literal, Mapping, Optional
2324

2425
from fastcore.utils import *
2526
from fastcore.xml import *
@@ -84,7 +85,7 @@ def attrmap_x(o):
8485

8586
# %% ../nbs/api/01_components.ipynb
8687
def ft_html(tag: str, *c, id=None, cls=None, title=None, style=None, attrmap=None, valmap=None, ft_cls=None, **kwargs):
87-
ds,c = partition(c, risinstance(dict))
88+
ds,c = partition(c, risinstance(Mapping))
8889
for d in ds: kwargs = {**kwargs, **d}
8990
if ft_cls is None: ft_cls = fh_cfg.ft_cls
9091
if attrmap is None: attrmap=fh_cfg.attrmap

nbs/api/01_components.ipynb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"#| export\n",
3030
"from dataclasses import dataclass, asdict, is_dataclass, make_dataclass, replace, astuple, MISSING\n",
3131
"from bs4 import BeautifulSoup, Comment\n",
32-
"from typing import Literal, Optional\n",
32+
"from collections import UserDict\n",
33+
"from typing import Literal, Mapping, Optional\n",
3334
"\n",
3435
"from fastcore.utils import *\n",
3536
"from fastcore.xml import *\n",
@@ -311,7 +312,7 @@
311312
"source": [
312313
"#| export\n",
313314
"def ft_html(tag: str, *c, id=None, cls=None, title=None, style=None, attrmap=None, valmap=None, ft_cls=None, **kwargs):\n",
314-
" ds,c = partition(c, risinstance(dict))\n",
315+
" ds,c = partition(c, risinstance(Mapping))\n",
315316
" for d in ds: kwargs = {**kwargs, **d}\n",
316317
" if ft_cls is None: ft_cls = fh_cfg.ft_cls\n",
317318
" if attrmap is None: attrmap=fh_cfg.attrmap\n",
@@ -377,6 +378,26 @@
377378
"ft_html('a', {'@click.away':1})"
378379
]
379380
},
381+
{
382+
"cell_type": "code",
383+
"execution_count": null,
384+
"id": "979e9bc8614fb3c1",
385+
"metadata": {},
386+
"outputs": [
387+
{
388+
"data": {
389+
"text/markdown": "```html\n<a @click.away=\"1\"></a>\n```",
390+
"text/plain": [
391+
"a((),{'@click.away': 1})"
392+
]
393+
},
394+
"execution_count": null,
395+
"metadata": {},
396+
"output_type": "execute_result"
397+
}
398+
],
399+
"source": "ft_html('a', UserDict({'@click.away':1}))"
400+
},
380401
{
381402
"cell_type": "code",
382403
"execution_count": null,

0 commit comments

Comments
 (0)