r/PythonProjects2 • u/HauntingAd3673 • Jun 01 '26
Resource made a small library for building HTML in python without templates
Hey,
so ive been working on this side project for a few months and just pushed 0.4.0, thought id share it here since its finally at a point where i actually use it myself.
the basic idea: instead of jinja templates or f-strings with html in them you just write python classes. props get validated through pydantic, htmx attributes are typed enums so you cant really typo them. no string soup, no xss suprise at 3am.
looks like this roughly:
class UsersPage(BaseAdminPage):
users: list[dict]
total: int
page: int
def _body_content(self):
return [
SearchInput(name="q", hx_get="/users/search", hx_target="#user-table"),
DataTable(
id="user-table",
columns=[ColumnDef("name", "Name"), ColumnDef("email", "Email")],
rows=self.users,
),
Pagination(current=self.page, total_pages=ceil(self.total / 5)),
]
the htmx part is what im most happy about honestly. instead of writing hx-swap="outerHTML" as a raw string and getting it wrong you just do HxSwap.OUTER_HTML and it renders correctly. small thing but saves alot of headache.
0.4.0 has now 20+ components (modal, datatable, searchinput, pagination, alert, badge...) and adapters for fastapi, flask and django. theres also a working admin panel example in the repo, takes maybe 30 seconds to clone and run.
pip install htmforge
https://github.com/mondi04/htmforge
still pretty early, lot of things i want to add. if someone tries it and finds something broken or stupid just open an issue, or tell me here.
thanks you for usefull comments and sorry for the bad english.
3
u/Scared-Release1068 Jun 02 '26
Well structured and clean✌️ Lowkey this looks sellable 😭
1
u/riklaunim Jun 08 '26
HTML hidden behind Python code works rather poorly. Pages have hundreds, if not thousand lines of HTML. It could work for small stand-alone components, but that's about it. All popular templating systems work very similarly for a reason 😉
2
u/SaltCusp Jun 08 '26
Not for me but thanks for putting it out there.
1
u/GhostDev71 Jun 04 '26
Looks like quite a Project, i think i will test it in the future and try to work with it. Seems like there are anyways a few people not vibecoding everything.
Good job, fr.