Skip to content

Commit e3f96f9

Browse files
committed
move components to fasthtml
1 parent 7c83a74 commit e3f96f9

File tree

3 files changed

+21
-193
lines changed

3 files changed

+21
-193
lines changed

fastcore/_modidx.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,9 @@
534534
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
535535
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
536536
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
537-
'fastcore.xml': { 'fastcore.xml.Checkbox': ('xml.html#checkbox', 'fastcore/xml.py'),
538-
'fastcore.xml.Hidden': ('xml.html#hidden', 'fastcore/xml.py'),
539-
'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
537+
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
538+
'fastcore.xml.XT._repr_markdown_': ('xml.html#xt._repr_markdown_', 'fastcore/xml.py'),
540539
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
541-
'fastcore.xml.fill_dataclass': ('xml.html#fill_dataclass', 'fastcore/xml.py'),
542-
'fastcore.xml.fill_form': ('xml.html#fill_form', 'fastcore/xml.py'),
543-
'fastcore.xml.set_val': ('xml.html#set_val', 'fastcore/xml.py'),
544540
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
545541
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
546542
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),

fastcore/xml.py

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22

33
# %% auto 0
4-
__all__ = ['named', 'voids', 'XT', 'xt', 'Checkbox', 'Hidden', 'to_xml', 'set_val', 'fill_form', 'fill_dataclass', 'Html', 'Head',
5-
'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code', 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5',
6-
'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol',
7-
'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup',
8-
'Form', 'Input', 'Textarea', 'Button', 'Select', 'Option', 'Label', 'Fieldset', 'Legend', 'Details',
9-
'Summary', 'Main', 'Header', 'Footer', 'Section', 'Article', 'Aside', 'Figure', 'Figcaption', 'Mark',
10-
'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video', 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script',
11-
'Noscript', 'Template', 'Slot']
4+
__all__ = ['named', 'voids', 'XT', 'xt', 'to_xml', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code', 'Div',
5+
'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup',
6+
'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr',
7+
'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button', 'Select', 'Option', 'Label',
8+
'Fieldset', 'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer', 'Section', 'Article', 'Aside',
9+
'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video', 'Audio', 'Source',
10+
'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']
1211

1312
# %% ../nbs/11_xml.ipynb 2
1413
from .utils import *
@@ -33,7 +32,7 @@ class XT(list): patch
3332
def xt(tag:str, *c, **kw):
3433
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
3534
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
36-
kw = {_attrmap(k):str(v) for k,v in kw.items()}
35+
kw = {_attrmap(k):str(v) for k,v in kw.items() if v is not None}
3736
if tag in named and 'id' in kw and 'name' not in kw: kw['name'] = kw['id']
3837
return XT([tag.lower(),c,kw])
3938

@@ -52,18 +51,9 @@ def xt(tag:str, *c, **kw):
5251
for o in _all_: _g[o] = partial(xt, o.lower())
5352

5453
# %% ../nbs/11_xml.ipynb 13
55-
def Checkbox(value:bool=False, **kw):
56-
checked = {"checked":"1"} if value else {}
57-
return Input(type="checkbox", **checked, **kw)
58-
59-
# %% ../nbs/11_xml.ipynb 14
60-
def Hidden(value:str="", **kw):
61-
return Input(type="hidden", value=value, **kw)
62-
63-
# %% ../nbs/11_xml.ipynb 15
6454
voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())
6555

66-
# %% ../nbs/11_xml.ipynb 16
56+
# %% ../nbs/11_xml.ipynb 14
6757
def to_xml(elm, lvl=0):
6858
"Convert `xt` element tree into an XML string"
6959
if isinstance(elm, tuple): return '\n'.join(to_xml(o) for o in elm)
@@ -86,25 +76,9 @@ def to_xml(elm, lvl=0):
8676
if tag not in voids: res += f'{sp}{cltag}\n'
8777
return res
8878

89-
# %% ../nbs/11_xml.ipynb 21
90-
def set_val(tag, attr, val):
91-
if attr.get('type', '') in ('checkbox','radio'):
92-
if val: attr['checked'] = '1'
93-
else: attr.pop('checked', '')
94-
else: attr['value'] = val
95-
96-
# %% ../nbs/11_xml.ipynb 22
97-
def fill_form(form, obj):
98-
"Modifies form in-place and returns it"
99-
inps = {attrs['id']:(tag,attrs) for tag,c,attrs in form[1] if 'id' in attrs}
100-
for nm,val in asdict(obj).items():
101-
if nm in inps:
102-
tag,attr = inps[nm]
103-
set_val(tag, attr, val)
104-
return form
105-
106-
# %% ../nbs/11_xml.ipynb 24
107-
def fill_dataclass(src, dest):
108-
"Modifies dataclass in-place and returns it"
109-
for nm,val in asdict(src).items(): setattr(dest, nm, val)
110-
return dest
79+
# %% ../nbs/11_xml.ipynb 16
80+
@patch
81+
def _repr_markdown_(self:XT):
82+
try: from IPython import display
83+
except ImportError: return repr(self)
84+
return f'```html\n{to_xml(self)}\n```'

nbs/11_xml.ipynb

Lines changed: 4 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 36,
25+
"execution_count": 2,
2626
"id": "55944805",
2727
"metadata": {},
2828
"outputs": [],
@@ -93,7 +93,7 @@
9393
"def xt(tag:str, *c, **kw):\n",
9494
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
9595
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
96-
" kw = {_attrmap(k):str(v) for k,v in kw.items()}\n",
96+
" kw = {_attrmap(k):str(v) for k,v in kw.items() if v is not None}\n",
9797
" if tag in named and 'id' in kw and 'name' not in kw: kw['name'] = kw['id']\n",
9898
" return XT([tag.lower(),c,kw])"
9999
]
@@ -187,31 +187,6 @@
187187
"pprint(Button(id='btn'))"
188188
]
189189
},
190-
{
191-
"cell_type": "code",
192-
"execution_count": 11,
193-
"id": "430aa9d9",
194-
"metadata": {},
195-
"outputs": [],
196-
"source": [
197-
"#| export\n",
198-
"def Checkbox(value:bool=False, **kw):\n",
199-
" checked = {\"checked\":\"1\"} if value else {}\n",
200-
" return Input(type=\"checkbox\", **checked, **kw)"
201-
]
202-
},
203-
{
204-
"cell_type": "code",
205-
"execution_count": 12,
206-
"id": "461bcf58",
207-
"metadata": {},
208-
"outputs": [],
209-
"source": [
210-
"#| export\n",
211-
"def Hidden(value:str=\"\", **kw):\n",
212-
" return Input(type=\"hidden\", value=value, **kw)"
213-
]
214-
},
215190
{
216191
"cell_type": "code",
217192
"execution_count": 13,
@@ -295,6 +270,7 @@
295270
"metadata": {},
296271
"outputs": [],
297272
"source": [
273+
"#| export\n",
298274
"@patch\n",
299275
"def _repr_markdown_(self:XT):\n",
300276
" try: from IPython import display\n",
@@ -361,124 +337,6 @@
361337
"samp"
362338
]
363339
},
364-
{
365-
"cell_type": "code",
366-
"execution_count": 25,
367-
"id": "0e5a0e2e",
368-
"metadata": {},
369-
"outputs": [],
370-
"source": [
371-
"#| export\n",
372-
"def set_val(tag, attr, val):\n",
373-
" if attr.get('type', '') in ('checkbox','radio'):\n",
374-
" if val: attr['checked'] = '1'\n",
375-
" else: attr.pop('checked', '')\n",
376-
" else: attr['value'] = val"
377-
]
378-
},
379-
{
380-
"cell_type": "code",
381-
"execution_count": 32,
382-
"id": "f0c83f26",
383-
"metadata": {},
384-
"outputs": [],
385-
"source": [
386-
"#| export\n",
387-
"def fill_form(form, obj):\n",
388-
" \"Modifies form in-place and returns it\"\n",
389-
" inps = {attrs['id']:(tag,attrs) for tag,c,attrs in form[1] if 'id' in attrs}\n",
390-
" for nm,val in asdict(obj).items():\n",
391-
" if nm in inps:\n",
392-
" tag,attr = inps[nm]\n",
393-
" set_val(tag, attr, val)\n",
394-
" return form"
395-
]
396-
},
397-
{
398-
"cell_type": "code",
399-
"execution_count": 33,
400-
"id": "caef04d9",
401-
"metadata": {},
402-
"outputs": [
403-
{
404-
"data": {
405-
"text/markdown": [
406-
"```html\n",
407-
"<form>\n",
408-
" <input id=\"title\" name=\"title\" value=\"Profit\">\n",
409-
" <input type=\"checkbox\" id=\"done\" name=\"done\" checked=\"1\">\n",
410-
" <input type=\"hidden\" value=\"2\" id=\"id\" name=\"id\">\n",
411-
" <button>\n",
412-
"Save\n",
413-
" </button>\n",
414-
"</form>\n",
415-
"\n",
416-
"```"
417-
],
418-
"text/plain": [
419-
"['form',\n",
420-
" (['input', (), {'id': 'title', 'name': 'title', 'value': 'Profit'}],\n",
421-
" ['input',\n",
422-
" (),\n",
423-
" {'type': 'checkbox', 'id': 'done', 'name': 'done', 'checked': '1'}],\n",
424-
" ['input', (), {'type': 'hidden', 'value': 2, 'id': 'id', 'name': 'id'}],\n",
425-
" ['button', ('Save',), {}]),\n",
426-
" {}]"
427-
]
428-
},
429-
"execution_count": 33,
430-
"metadata": {},
431-
"output_type": "execute_result"
432-
}
433-
],
434-
"source": [
435-
"@dataclass\n",
436-
"class TodoItem:\n",
437-
" title:str; id:int; done:bool\n",
438-
" \n",
439-
"todo = TodoItem(id=2, title=\"Profit\", done=True)\n",
440-
"form = Form(Input(id=\"title\"), Checkbox(id=\"done\"),\n",
441-
" Hidden(id=\"id\"), Button(\"Save\"))\n",
442-
"fill_form(form, todo)"
443-
]
444-
},
445-
{
446-
"cell_type": "code",
447-
"execution_count": 39,
448-
"id": "8b171490",
449-
"metadata": {},
450-
"outputs": [],
451-
"source": [
452-
"#|export\n",
453-
"def fill_dataclass(src, dest):\n",
454-
" \"Modifies dataclass in-place and returns it\"\n",
455-
" for nm,val in asdict(src).items(): setattr(dest, nm, val)\n",
456-
" return dest"
457-
]
458-
},
459-
{
460-
"cell_type": "code",
461-
"execution_count": 40,
462-
"id": "77e3f785",
463-
"metadata": {},
464-
"outputs": [
465-
{
466-
"data": {
467-
"text/plain": [
468-
"TodoItem(title='Profit', id=2, done=True)"
469-
]
470-
},
471-
"execution_count": 40,
472-
"metadata": {},
473-
"output_type": "execute_result"
474-
}
475-
],
476-
"source": [
477-
"nt = TodoItem('', 0, False)\n",
478-
"fill_dataclass(todo, nt)\n",
479-
"nt"
480-
]
481-
},
482340
{
483341
"cell_type": "markdown",
484342
"id": "df973d4e",
@@ -489,7 +347,7 @@
489347
},
490348
{
491349
"cell_type": "code",
492-
"execution_count": 41,
350+
"execution_count": 1,
493351
"id": "ad32b076",
494352
"metadata": {},
495353
"outputs": [],

0 commit comments

Comments
 (0)