Components Guide
Components in htmforge are regular Python classes based on Component.
You declare typed fields, then return one root Element from render().
Core Component API
- Implement
render() -> Elementand return the root element. validate_assignment=Trueensures runtime prop changes are validated.- Use
to_html()to render a full HTML string. - Use
to_fragment()when you want an HTMX-friendly fragment. - Use
clone(**overrides)to create a modified copy.
htmx_attrs() returns only HTMX fields that are set. Dict values are serialized as compact JSON.
If a subclass does not implement render(), instantiation fails with TypeError.
Minimal Example
from htmforge import Component
from htmforge.core.element import Element
from htmforge.elements import div, h2, p
class UserCard(Component):
name: str
email: str
def render(self) -> Element:
return div(
h2(self.name),
p(self.email),
cls="card",
)
html = UserCard(name="Ada", email="ada@example.com").to_html()
Framework Adapters
to_fastapi()returns a FastAPIHTMLResponseto_flask()returns a FlaskResponseto_django()returns a DjangoHttpResponse
HTMX Integration
You can set typed HTMX values either on elements or components.
from htmforge.elements import button
from htmforge.htmx import HxSwap, HxTarget
delete_btn = button(
"Delete",
hx_delete="/users/1",
hx_swap=HxSwap.OUTER_HTML,
hx_target=HxTarget.CLOSEST_TR,
hx_confirm="Delete this user?",
)
Pre-built Components
Data and Feedback
DataTablefor tabular data with legacy and dict-row modesAlertfor status messagesBadgeandBreadcrumbfor small metadata and navigation hintsPaginationfor page controlsToastfor timed notifications
Interaction
Modalfor dialog flowsSearchInputfor debounced searchTabsfor HTMX tab panelsAccordionfor expandable sectionsDropdownfor action menusSpinnerfor loading states
Forms
FormFieldfor single field renderingSelectField,CheckboxField,RadioGroupfor typed controlsFormGroupfor grouped layoutsFormfor full forms with automatic error injection
from htmforge.components import Form, FormField, InputType
form = Form(
action="/register",
fields=[
FormField(name="username", label_text="Username", input_type=InputType.TEXT),
FormField(name="email", label_text="Email", input_type=InputType.EMAIL),
],
errors={"email": "This email is already registered."},
submit_label="Create account",
)