|
| 1 | +from fasthtml.common import * |
| 2 | +from fasthtml.svg import * |
| 3 | + |
| 4 | +app,rt = fast_app(live=True) |
| 5 | + |
| 6 | +@rt |
| 7 | +def index(): |
| 8 | + return Titled("HTMX SVG swaps", |
| 9 | + Svg( |
| 10 | + get_circle(0), |
| 11 | + Rect(x=5, y=85, width=10, height=10, fill="purple"), |
| 12 | + width=100, height=100, viewBox="0 0 100 100" |
| 13 | + ), |
| 14 | + Hidden(id='ref', value='0'), |
| 15 | + Button('Multi', hx_get=change, hx_include='previous input'), |
| 16 | + Div(id='dest'), |
| 17 | + ) |
| 18 | + |
| 19 | +def get_circle(ref): |
| 20 | + opts = [ |
| 21 | + Circle(id='circle1', cx=50, cy=50, r=20, |
| 22 | + hx_swap_oob='true', hx_get=foo, hx_swap='outerHTML', hx_select='svg>*'), |
| 23 | + G(hx_swap_oob='true', id='circle1')( |
| 24 | + Circle(id='circle1', r='45', cx='50', cy='50', fill='red'), |
| 25 | + Circle(id='circle1', r='25', cx='70', cy='70', fill='green') |
| 26 | + ) |
| 27 | + ] |
| 28 | + return opts[ref] |
| 29 | + |
| 30 | +@rt |
| 31 | +def foo(): return Svg(Rect(x=50, y=50, width=30, height=30, fill='blue')) |
| 32 | + |
| 33 | +@rt |
| 34 | +def change(ref:int): |
| 35 | + return ( |
| 36 | + 'Multi' if ref else 'Blue', |
| 37 | + Template(Svg(get_circle(1-ref))), |
| 38 | + Hidden(id='ref', value=1-ref, hx_swap_oob='true') |
| 39 | + ) |
| 40 | + |
| 41 | +serve() |
| 42 | + |
0 commit comments