Skip to content

Commit 68e9657

Browse files
authored
Merge pull request #738 from gazpachoking/any-mapping-attrs
[PR] Allow any Mapping children to specify attributes. fix #737
2 parents 96ea074 + 3e043a5 commit 68e9657

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

fasthtml/components.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
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 typing import Literal, Mapping, Optional
2323

2424
from fastcore.utils import *
2525
from fastcore.xml import *
@@ -84,7 +84,7 @@ def attrmap_x(o):
8484

8585
# %% ../nbs/api/01_components.ipynb
8686
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))
87+
ds,c = partition(c, risinstance(Mapping))
8888
for d in ds: kwargs = {**kwargs, **d}
8989
if ft_cls is None: ft_cls = fh_cfg.ft_cls
9090
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,7 @@
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 typing import Literal, Mapping, Optional\n",
3333
"\n",
3434
"from fastcore.utils import *\n",
3535
"from fastcore.xml import *\n",
@@ -50,6 +50,7 @@
5050
"metadata": {},
5151
"outputs": [],
5252
"source": [
53+
"from collections import UserDict\n",
5354
"from lxml import html as lx\n",
5455
"from pprint import pprint"
5556
]
@@ -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)