from lxml import html as lx
from pprint import pprint
Components
ft_html
and ft_hx
functions to add some conveniences to ft
, along with a full set of basic HTML components, and functions to work with forms and FT
conversion
show
show (ft, *rest)
Renders FT Components into HTML within a Jupyter notebook.
= P(Strong("FastHTML is ", I("Fast")))
sentence
# When placed within the `show()` function, this will render
# the HTML in Jupyter notebooks.
show(sentence)
FastHTML is Fast
# Called without the `show()` function, the raw HTML is displayed
sentence
<p>
<strong>FastHTML is <i>Fast</i></strong></p>
attrmap_x
attrmap_x (o)
ft_html
ft_html (tag:str, *c, id=None, cls=None, title=None, style=None, attrmap=None, valmap=None, ft_cls=None, auto_id=None, **kwargs)
'a', **{'@click.away':1}) ft_html(
<a @click.away="1"></a>
'a', {'@click.away':1}) ft_html(
<a @click.away="1"></a>
= Div(id='someid') c
'a', id=c) ft_html(
<a id="someid" name="someid"></a>
ft_hx
ft_hx (tag:str, *c, target_id=None, hx_vals=None, hx_target=None, id=None, cls=None, title=None, style=None, accesskey=None, contenteditable=None, dir=None, draggable=None, enterkeyhint=None, hidden=None, inert=None, inputmode=None, lang=None, popover=None, spellcheck=None, tabindex=None, translate=None, hx_get=None, hx_post=None, hx_put=None, hx_delete=None, hx_patch=None, hx_trigger=None, hx_swap=None, hx_swap_oob=None, hx_include=None, hx_select=None, hx_select_oob=None, hx_indicator=None, hx_push_url=None, hx_confirm=None, hx_disable=None, hx_replace_url=None, hx_disabled_elt=None, hx_ext=None, hx_headers=None, hx_history=None, hx_history_elt=None, hx_inherit=None, hx_params=None, hx_preserve=None, hx_prompt=None, hx_request=None, hx_sync=None, hx_validate=None, **kwargs)
'a', hx_vals={'a':1}) ft_hx(
<a hx-vals='{"a": 1}'></a>
'a', hx_target=c) ft_hx(
<a hx-target="#someid"></a>
File
File (fname)
Use the unescaped text in file fname
directly
For tags that have a name
attribute, it will be set to the value of id
if not provided explicitly:
='foo', id='btn'),
Form(Button(target_id='/', target_id='tgt', id='frm') hx_post
<form hx-post="/" hx-target="#tgt" id="frm" name="frm"><button hx-target="#foo" id="btn" name="btn"></button></form>
fill_form
fill_form (form:fastcore.xml.FT, obj)
Fills named items in form
using attributes in obj
@dataclass
class TodoItem:
str; id:int; done:bool; details:str; opt:str='a'
title:
= TodoItem(id=2, title="Profit", done=True, details="Details", opt='b')
todo = Label(Input(type="checkbox", cls="checkboxer", name="done", data_foo="bar"), "Done", cls='px-2')
check = Form(Fieldset(Input(cls="char", id="title", value="a"), check, Input(type="hidden", id="id"),
form ='a'), Option(value='b'), name='opt'),
Select(Option(valueid='details'), Button("Save"),
Textarea(="stuff"))
name= fill_form(form, todo)
form assert '<textarea id="details" name="details">Details</textarea>' in to_xml(form)
form
<form><fieldset name="stuff"> <input value="Profit" id="title" class="char" name="title">
<label class="px-2"> <input type="checkbox" name="done" data-foo="bar" class="checkboxer" checked="1">
</label> <input type="hidden" id="id" name="id" value="2">
Done<select name="opt"><option value="a"></option><option value="b" selected="1"></option></select><textarea id="details" name="details">Details</textarea><button>Save</button></fieldset></form>
@dataclass
class MultiSelect:
list[str]
items:
= MultiSelect(items=['a', 'c'])
multiselect = Form(Select(Option('a', value='a'), Option('b', value='b'), Option('c', value='c'), multiple='1', name='items'))
multiform = fill_form(multiform, multiselect)
multiform assert '<option value="a" selected="1">a</option>' in to_xml(multiform)
assert '<option value="b">b</option>' in to_xml(multiform)
assert '<option value="c" selected="1">c</option>' in to_xml(multiform)
multiform
<form><select multiple="1" name="items"><option value="a" selected="1">a</option><option value="b">b</option><option value="c" selected="1">c</option></select></form>
@dataclass
class MultiCheck:
list[str]
items:
= MultiCheck(items=['a', 'c'])
multicheck = Form(Fieldset(Label(Input(type='checkbox', name='items', value='a'), 'a'),
multiform type='checkbox', name='items', value='b'), 'b'),
Label(Input(type='checkbox', name='items', value='c'), 'c')))
Label(Input(= fill_form(multiform, multicheck)
multiform assert '<input type="checkbox" name="items" value="a" checked="1">' in to_xml(multiform)
assert '<input type="checkbox" name="items" value="b">' in to_xml(multiform)
assert '<input type="checkbox" name="items" value="c" checked="1">' in to_xml(multiform)
multiform
<form><fieldset><label> <input type="checkbox" name="items" value="a" checked="1">
</label><label> <input type="checkbox" name="items" value="b">
a</label><label> <input type="checkbox" name="items" value="c" checked="1">
b</label></fieldset></form> c
fill_dataclass
fill_dataclass (src, dest)
Modifies dataclass in-place and returns it
= TodoItem('', 0, False, '')
nt
fill_dataclass(todo, nt) nt
TodoItem(title='Profit', id=2, done=True, details='Details', opt='b')
find_inputs
find_inputs (e, tags='input', **kw)
Recursively find all elements in e
with tags
and attrs matching kw
= find_inputs(form, id='title')
inps len(inps), 1)
test_eq( inps
[input((),{'value': 'Profit', 'id': 'title', 'class': 'char', 'name': 'title'})]
You can also use lxml for more sophisticated searching:
= lx.fromstring(to_xml(form))
elem "//input[@id='title']/@value"), ['Profit']) test_eq(elem.xpath(
getattr
__getattr__ (tag)
html2ft
html2ft (html, attr1st=False)
Convert HTML to an ft
expression
= to_xml(form)
h 'python') hl_md(html2ft(h),
Form(
Fieldset(='Profit', id='title', name='title', cls='char'),
Input(value
Label(type='checkbox', name='done', data_foo='bar', checked='1', cls='checkboxer'),
Input('Done',
='px-2'
cls
),type='hidden', id='id', name='id', value='2'),
Input(
Select(='a'),
Option(value='b', selected='1'),
Option(value='opt'
name
),'Details', id='details', name='details'),
Textarea('Save'),
Button(='stuff'
name
) )
=True), 'python') hl_md(html2ft(h, attr1st
Form(='stuff')(
Fieldset(name='Profit', id='title', name='title', cls='char'),
Input(value='px-2')(
Label(clstype='checkbox', name='done', data_foo='bar', checked='1', cls='checkboxer'),
Input('Done'
),type='hidden', id='id', name='id', value='2'),
Input(='opt')(
Select(name='a'),
Option(value='b', selected='1')
Option(value
),'Details', id='details', name='details'),
Textarea('Save')
Button(
) )
sse_message
sse_message (elm, event='message')
Convert element elm
into a format suitable for SSE streaming
print(sse_message(Div(P('hi'), P('there'))))
event: message
data: <div>
data: <p>hi</p>
data: <p>there</p>
data: </div>