Skip to content

Commit

Permalink
A hx tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 18, 2024
1 parent ed461a2 commit 304f4d4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
37 changes: 37 additions & 0 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,43 @@
"reg_re_param(\"path\", \".*?\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "420d2107",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"\\nclass NotFoundException(HTTPException):\\n def __init__(self): return super().__init__(404)\\n\\nasync def not_found(request: Request, exc: NotFoundException):\\n return HTMLResponse(content='not found', status_code=exc.status_code)\\n\\nexception_handlers = { NotFoundException: not_found }\\n\\nrt = Router(headtags=[htmxscr, picocss])\\n\\ndef wrap_root(resp, headtags):\\n title = Title('Page')\\n if isinstance(resp, tuple): title,resp = resp\\n return Html(Head(title, *headtags), resp)\\n\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\"\"\"\n",
"class NotFoundException(HTTPException):\n",
" def __init__(self): return super().__init__(404)\n",
"\n",
"async def not_found(request: Request, exc: NotFoundException):\n",
" return HTMLResponse(content='not found', status_code=exc.status_code)\n",
"\n",
"exception_handlers = { NotFoundException: not_found }\n",
"\n",
"rt = Router(headtags=[htmxscr, picocss])\n",
"\n",
"def wrap_root(resp, headtags):\n",
" title = Title('Page')\n",
" if isinstance(resp, tuple): title,resp = resp\n",
" return Html(Head(title, *headtags), resp)\n",
"\"\"\"\n"
]
},
{
"cell_type": "markdown",
"id": "474e14b4",
Expand Down
62 changes: 53 additions & 9 deletions 01_components.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 71,
"id": "56db45b6",
"metadata": {},
"outputs": [],
Expand All @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 72,
"id": "8e2d405b",
"metadata": {},
"outputs": [],
Expand All @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 73,
"id": "6c3dcdbc",
"metadata": {},
"outputs": [],
Expand All @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 74,
"id": "430aa9d9",
"metadata": {},
"outputs": [],
Expand All @@ -58,7 +58,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 75,
"id": "2d089687",
"metadata": {},
"outputs": [],
Expand All @@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 76,
"id": "3d9e3af6",
"metadata": {},
"outputs": [
Expand All @@ -84,7 +84,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 60,
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -95,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 77,
"id": "461bcf58",
"metadata": {},
"outputs": [],
Expand All @@ -105,6 +105,50 @@
" return Input(type=\"hidden\", value=value, **kw)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "eb8a9099",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def A(txt, hx_get, target_id=None, hx_swap=None, href='#', **kw):\n",
" if target_id: target_id = '#'+target_id\n",
" return xt('a', txt, href=href, hx_get=hx_get, hx_target=target_id, hx_swap=hx_swap, **kw)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "6632a92e",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<a href=\"#\" hx-get=\"/get\" hx-target=\"#id\" hx-swap=\"None\">\n",
"text\n",
"</a>\n",
"\n",
"```"
],
"text/plain": [
"['a',\n",
" ('text',),\n",
" {'href': '#', 'hx-get': '/get', 'hx-target': '#id', 'hx-swap': 'None'}]"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A('text', '/get', 'id')"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down Expand Up @@ -262,7 +306,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 84,
"id": "d211e8e2",
"metadata": {},
"outputs": [],
Expand Down
3 changes: 2 additions & 1 deletion fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
'doc_host': 'https://AnswerDotAI.github.io',
'git_url': 'https://github.com/AnswerDotAI/fasthtml',
'lib_path': 'fasthtml'},
'syms': { 'fasthtml.components': { 'fasthtml.components.Checkbox': ('components.html#checkbox', 'fasthtml/components.py'),
'syms': { 'fasthtml.components': { 'fasthtml.components.A': ('components.html#a', 'fasthtml/components.py'),
'fasthtml.components.Checkbox': ('components.html#checkbox', 'fasthtml/components.py'),
'fasthtml.components.Hidden': ('components.html#hidden', 'fasthtml/components.py'),
'fasthtml.components.fill_dataclass': ('components.html#fill_dataclass', 'fasthtml/components.py'),
'fasthtml.components.fill_form': ('components.html#fill_form', 'fasthtml/components.py'),
Expand Down
13 changes: 9 additions & 4 deletions fasthtml/components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../01_components.ipynb.

# %% auto 0
__all__ = ['Checkbox', 'Hidden', 'set_val', 'find_inps', 'fill_form', 'fill_dataclass']
__all__ = ['Checkbox', 'Hidden', 'A', 'set_val', 'find_inps', 'fill_form', 'fill_dataclass']

# %% ../01_components.ipynb 2
from fastcore.xml import *
Expand All @@ -19,13 +19,18 @@ def Hidden(value:str="", **kw):
return Input(type="hidden", value=value, **kw)

# %% ../01_components.ipynb 8
def A(txt, hx_get, target_id=None, hx_swap=None, href='#', **kw):
if target_id: target_id = '#'+target_id
return xt('a', txt, href=href, hx_get=hx_get, hx_target=target_id, hx_swap=hx_swap, **kw)

# %% ../01_components.ipynb 10
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

# %% ../01_components.ipynb 9
# %% ../01_components.ipynb 11
def find_inps(html):
if not html: return []
tag,cs,attrs = html
Expand All @@ -35,7 +40,7 @@ def find_inps(html):
if isinstance(c, list): res.extend(find_inps(c))
return res

# %% ../01_components.ipynb 10
# %% ../01_components.ipynb 12
def fill_form(form, obj):
"Modifies form in-place and returns it"
inps = find_inps(form)
Expand All @@ -46,7 +51,7 @@ def fill_form(form, obj):
set_val(tag, attr, val)
return form

# %% ../01_components.ipynb 12
# %% ../01_components.ipynb 14
def fill_dataclass(src, dest):
"Modifies dataclass in-place and returns it"
for nm,val in asdict(src).items(): setattr(dest, nm, val)
Expand Down

0 comments on commit 304f4d4

Please sign in to comment.