Skip to content

Commit

Permalink
move components to fasthtml
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 18, 2024
1 parent 7c83a74 commit e3f96f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 193 deletions.
8 changes: 2 additions & 6 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,9 @@
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
'fastcore.xml': { 'fastcore.xml.Checkbox': ('xml.html#checkbox', 'fastcore/xml.py'),
'fastcore.xml.Hidden': ('xml.html#hidden', 'fastcore/xml.py'),
'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
'fastcore.xml.XT._repr_markdown_': ('xml.html#xt._repr_markdown_', 'fastcore/xml.py'),
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
'fastcore.xml.fill_dataclass': ('xml.html#fill_dataclass', 'fastcore/xml.py'),
'fastcore.xml.fill_form': ('xml.html#fill_form', 'fastcore/xml.py'),
'fastcore.xml.set_val': ('xml.html#set_val', 'fastcore/xml.py'),
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
Expand Down
56 changes: 15 additions & 41 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.

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

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

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

# %% ../nbs/11_xml.ipynb 13
def Checkbox(value:bool=False, **kw):
checked = {"checked":"1"} if value else {}
return Input(type="checkbox", **checked, **kw)

# %% ../nbs/11_xml.ipynb 14
def Hidden(value:str="", **kw):
return Input(type="hidden", value=value, **kw)

# %% ../nbs/11_xml.ipynb 15
voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())

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

# %% ../nbs/11_xml.ipynb 21
def set_val(tag, attr, val):
if attr.get('type', '') in ('checkbox','radio'):
if val: attr['checked'] = '1'
else: attr.pop('checked', '')
else: attr['value'] = val

# %% ../nbs/11_xml.ipynb 22
def fill_form(form, obj):
"Modifies form in-place and returns it"
inps = {attrs['id']:(tag,attrs) for tag,c,attrs in form[1] if 'id' in attrs}
for nm,val in asdict(obj).items():
if nm in inps:
tag,attr = inps[nm]
set_val(tag, attr, val)
return form

# %% ../nbs/11_xml.ipynb 24
def fill_dataclass(src, dest):
"Modifies dataclass in-place and returns it"
for nm,val in asdict(src).items(): setattr(dest, nm, val)
return dest
# %% ../nbs/11_xml.ipynb 16
@patch
def _repr_markdown_(self:XT):
try: from IPython import display
except ImportError: return repr(self)
return f'```html\n{to_xml(self)}\n```'
150 changes: 4 additions & 146 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 2,
"id": "55944805",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -93,7 +93,7 @@
"def xt(tag:str, *c, **kw):\n",
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
" kw = {_attrmap(k):str(v) for k,v in kw.items()}\n",
" kw = {_attrmap(k):str(v) for k,v in kw.items() if v is not None}\n",
" if tag in named and 'id' in kw and 'name' not in kw: kw['name'] = kw['id']\n",
" return XT([tag.lower(),c,kw])"
]
Expand Down Expand Up @@ -187,31 +187,6 @@
"pprint(Button(id='btn'))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "430aa9d9",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def Checkbox(value:bool=False, **kw):\n",
" checked = {\"checked\":\"1\"} if value else {}\n",
" return Input(type=\"checkbox\", **checked, **kw)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "461bcf58",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def Hidden(value:str=\"\", **kw):\n",
" return Input(type=\"hidden\", value=value, **kw)"
]
},
{
"cell_type": "code",
"execution_count": 13,
Expand Down Expand Up @@ -295,6 +270,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@patch\n",
"def _repr_markdown_(self:XT):\n",
" try: from IPython import display\n",
Expand Down Expand Up @@ -361,124 +337,6 @@
"samp"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "0e5a0e2e",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def set_val(tag, attr, val):\n",
" if attr.get('type', '') in ('checkbox','radio'):\n",
" if val: attr['checked'] = '1'\n",
" else: attr.pop('checked', '')\n",
" else: attr['value'] = val"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "f0c83f26",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def fill_form(form, obj):\n",
" \"Modifies form in-place and returns it\"\n",
" inps = {attrs['id']:(tag,attrs) for tag,c,attrs in form[1] if 'id' in attrs}\n",
" for nm,val in asdict(obj).items():\n",
" if nm in inps:\n",
" tag,attr = inps[nm]\n",
" set_val(tag, attr, val)\n",
" return form"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "caef04d9",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<form>\n",
" <input id=\"title\" name=\"title\" value=\"Profit\">\n",
" <input type=\"checkbox\" id=\"done\" name=\"done\" checked=\"1\">\n",
" <input type=\"hidden\" value=\"2\" id=\"id\" name=\"id\">\n",
" <button>\n",
"Save\n",
" </button>\n",
"</form>\n",
"\n",
"```"
],
"text/plain": [
"['form',\n",
" (['input', (), {'id': 'title', 'name': 'title', 'value': 'Profit'}],\n",
" ['input',\n",
" (),\n",
" {'type': 'checkbox', 'id': 'done', 'name': 'done', 'checked': '1'}],\n",
" ['input', (), {'type': 'hidden', 'value': 2, 'id': 'id', 'name': 'id'}],\n",
" ['button', ('Save',), {}]),\n",
" {}]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@dataclass\n",
"class TodoItem:\n",
" title:str; id:int; done:bool\n",
" \n",
"todo = TodoItem(id=2, title=\"Profit\", done=True)\n",
"form = Form(Input(id=\"title\"), Checkbox(id=\"done\"),\n",
" Hidden(id=\"id\"), Button(\"Save\"))\n",
"fill_form(form, todo)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "8b171490",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"def fill_dataclass(src, dest):\n",
" \"Modifies dataclass in-place and returns it\"\n",
" for nm,val in asdict(src).items(): setattr(dest, nm, val)\n",
" return dest"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "77e3f785",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TodoItem(title='Profit', id=2, done=True)"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nt = TodoItem('', 0, False)\n",
"fill_dataclass(todo, nt)\n",
"nt"
]
},
{
"cell_type": "markdown",
"id": "df973d4e",
Expand All @@ -489,7 +347,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 1,
"id": "ad32b076",
"metadata": {},
"outputs": [],
Expand Down

0 comments on commit e3f96f9

Please sign in to comment.