From 304f4d43a1d9ebe772660919dd0476cef2dfe0cb Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Sun, 19 May 2024 03:01:18 +1000 Subject: [PATCH] A hx tag --- 00_core.ipynb | 37 +++++++++++++++++++++++++ 01_components.ipynb | 62 ++++++++++++++++++++++++++++++++++++------ fasthtml/_modidx.py | 3 +- fasthtml/components.py | 13 ++++++--- 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/00_core.ipynb b/00_core.ipynb index 5f9315cd..b0b5c4a7 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -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", diff --git a/01_components.ipynb b/01_components.ipynb index a48c0b00..c893ecfc 100644 --- a/01_components.ipynb +++ b/01_components.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 71, "id": "56db45b6", "metadata": {}, "outputs": [], @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 72, "id": "8e2d405b", "metadata": {}, "outputs": [], @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 73, "id": "6c3dcdbc", "metadata": {}, "outputs": [], @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 74, "id": "430aa9d9", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 75, "id": "2d089687", "metadata": {}, "outputs": [], @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 76, "id": "3d9e3af6", "metadata": {}, "outputs": [ @@ -84,7 +84,7 @@ "" ] }, - "execution_count": 60, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -95,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 77, "id": "461bcf58", "metadata": {}, "outputs": [], @@ -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", + "\n", + "text\n", + "\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, @@ -262,7 +306,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 84, "id": "d211e8e2", "metadata": {}, "outputs": [], diff --git a/fasthtml/_modidx.py b/fasthtml/_modidx.py index 516c57d9..e9c2eab2 100644 --- a/fasthtml/_modidx.py +++ b/fasthtml/_modidx.py @@ -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'), diff --git a/fasthtml/components.py b/fasthtml/components.py index b0daf67b..2ac6e506 100644 --- a/fasthtml/components.py +++ b/fasthtml/components.py @@ -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 * @@ -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 @@ -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) @@ -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)