from lxml import html as lx
from pprint import pprint
Components
show
show (xt, *rest)
xt_html
xt_html (tag:str, *c, id=None, cls=None, title=None, style=None, **kwargs)
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:
='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.XT, obj)
Fills named items in form
using attributes in obj
@dataclass
class TodoItem:
str; id:int; done:bool; details:str
title:
= TodoItem(id=2, title="Profit", done=True, details="Details")
todo = Label(Input(type="checkbox", id="done", data_foo="bar"), "Done")
check = Form(Fieldset(Input(id="title"), check, Input(type="hidden", id="id"), Textarea(id='details'), Button("Save")))
form = fill_form(form, todo)
form 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>
<
Donelabel>
</input type="hidden" id="id" name="id" value="2"></input>
<textarea id="details" name="details">Details</textarea>
<button>Save</button>
<fieldset>
</form> </
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')
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 = []
= e
cs if isinstance(e, list):
= e
tag,cs,attr 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
id='title') find_inputs(form,
[['input', (), {'id': 'title', 'name': 'title', 'value': 'Profit'}]]
You can also use lxml for more sophisticated searching:
= lx.fromstring(to_xml(form))
elem "//input[@id='title']/@value") elem.xpath(
['Profit']
getattr
__getattr__ (tag)
html2xt
html2xt (html)
= to_xml(form)
h 'python') hl_md(html2xt(h),
Form(
Fieldset(id='title', name='title', value='Profit'),
Input(
Label(type='checkbox', data_foo='bar', id='done', name='done', checked='1'),
Input('Done'
),type='hidden', id='id', name='id', value='2'),
Input('Details', id='details', name='details'),
Textarea('Save')
Button(
) )