Components

from lxml import html as lx
from pprint import pprint

source

show

 show (xt, *rest)

source

xt_html

 xt_html (tag:str, *c, id=None, cls=None, title=None, style=None,
          **kwargs)

source

xt_hx

 xt_hx (tag:str, *c, target_id=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_target=None, hx_swap=None, hx_include=None, hx_select=None,
        hx_indicator=None, hx_push_url=None, hx_confirm=None,
        hx_disable=None, hx_replace_url=None, hx_on=None, **kwargs)

For tags that have a name attribute, it will be set to the value of id if not provided explicitly:

Form(Button(target_id='foo', id='btn'),
     hx_post='/', target_id='tgt', id='frm')
<form hx-post="/" hx-target="#tgt" id="frm" name="frm">
  <button hx-target="#foo" id="btn" name="btn"></button>
</form>

source

fill_form

 fill_form (form:fastcore.xml.XT, obj)

Fills named items in form using attributes in obj

@dataclass
class TodoItem:
    title:str; id:int; done:bool; details:str
                
todo = TodoItem(id=2, title="Profit", done=True, details="Details")
check = Label(Input(type="checkbox", id="done", data_foo="bar"), "Done")
form = Form(Fieldset(Input(id="title"), check, Input(type="hidden", id="id"), Textarea(id='details'), Button("Save")))
form = fill_form(form, todo)
form
<form>
  <fieldset>
    <input id="title" name="title" value="Profit"></input>
    <label>
      <input type="checkbox" data-foo="bar" id="done" name="done" checked="1"></input>
Done
    </label>
    <input type="hidden" id="id" name="id" value="2"></input>
    <textarea id="details" name="details">Details</textarea>
    <button>Save</button>
  </fieldset>
</form>

source

fill_dataclass

 fill_dataclass (src, dest)

Modifies dataclass in-place and returns it

nt = TodoItem('', 0, False, '')
fill_dataclass(todo, nt)
nt
TodoItem(title='Profit', id=2, done=True, details='Details')

source

find_inputs

 find_inputs (e, tags='input', **kw)
Exported source
def find_inputs(e, tags='input', **kw):
    # Recursively find all elements in `e` with `tags` and attrs matching `kw`
    if not isinstance(e, (list,tuple)): return []
    inputs = []
    if isinstance(tags,str): tags = [tags]
    elif tags is None: tags = []
    cs = e
    if isinstance(e, list):
        tag,cs,attr = e
        if e[0] in tags and kw.items()<=e[2].items(): inputs.append(e)
    for o in cs: inputs += find_inputs(o, tags, **kw)
    return inputs
find_inputs(form, id='title')
[['input', (), {'id': 'title', 'name': 'title', 'value': 'Profit'}]]

You can also use lxml for more sophisticated searching:

elem = lx.fromstring(to_xml(form))
elem.xpath("//input[@id='title']/@value")
['Profit']

source

getattr

 __getattr__ (tag)

source

html2xt

 html2xt (html)
h = to_xml(form)
hl_md(html2xt(h), 'python')
Form(
  Fieldset(
    Input(id='title', name='title', value='Profit'),
    Label(
      Input(type='checkbox', data_foo='bar', id='done', name='done', checked='1'),
      'Done'
    ),
    Input(type='hidden', id='id', name='id', value='2'),
    Textarea('Details', id='details', name='details'),
    Button('Save')
  )
)