Package: haitch
A lazy HTML element builder.haitchhaitch, an alternative pronunciation for the letter "H".Thehaitchpackage provides a builder for composing HTML elements in a Document Object Model (DOM) tree. The element nodes will only be rendered when the__str__method is invoked.Why write your HTML in templates when you can do it in Python?FeaturesCompose HTML elements.Lazily evaluate DOM tree.100% test coverage.100% type annotated codebase.Zero dependencies.RequirementsPython 3.8+QuickstartInstallhaitchusing pip:$pipinstallhaitchImport the library like so:importhaitchasHBy importing the root module, you now have access to any element you want:# Render known `h1` tag.h1=H.h1("Hello, world")print(h1)# <h1>Hello, world</h1># Render custom `foo` tag (useful for web components).foo=H.foo("Hello, world")print(foo)# <foo>Hello, world!</foo>Here is a simple, real-world example that showcases the advantage of writing HTML in Python. Let's say we want a list of customer emails based on some business logic:# Fetch customers from a data store.customers=[("jane@aol.com",False,True),("bob@example.com",True,False),("mark@mail.org",True,False),("karen@hr.org",False,False),]# Build the DOM tree with attributes and children.dom=H.div(class_="container")(H.h1("Customers to contact:"),H.ul()([H.li(H.a(href=f"mailto:{email}")(email),)foremail,is_premium,is_newincustomersifis_premiumoris_new],),)print(dom)# <div class="container">#   <h1>Customers to contact:</h1>#   <ul>#     <li><a href="mailto:jane@aol.com">jane@aol.com</a></li>#     <li><a href="mailto:bob@example.com">bob@example.com</a></li>#     <li><a href="mailto:mark@mail.org">mark@mail.org</a></li>#   </ul># </div>FYI: The example output is prettified for better readability. The actual output is a minified string with no formatting.MotivationI was inspired by the fantastichtbuilderlibrary as an alternative to template engines. The library met most of my needs, but I really wanted to expand its functionality by leveraging modern Python features like type annotations. This coincided with my excitement forHypermedia-Driven Applications (HDAs)with tools likehtmxandalpine.While writing HDAs has simplified my application logic, the lack of autocompletion and diagnostics in templates are quite annoying. Therefore, my goal with this package is to make writing HTML as simple and fun as writing normal Python codeby writing normal Python code.RoadmapExplicit attribute annotations for known elements likea,img, etc.Document elements based onMDN Web Docs.Extra Annotations for common third-party plugins likehtmxandalpine.Non-goalsThe following features willnotbe supported:Deprecated elements and attributes: if you want to use them, that is fine by me ashaitchsupports generic elements and attributes. However, I am not going to write the annotations and documentation for them.100% input validation: this library is meant to assist the developer to write valid HTML with the help of type annotations and documentation. In the end, it is up to the developer and browser to verify that the input is of the correct type. So for example, I will not add any validation to make sure that a<col>element is directly nested inside of a<colgroup>element or that thespanattribute for the<col>element is a positive integer.Licensehaitchis distributed under the terms of theBSD-3-Clauselicense.