HTML5Tagger

1.3.0 · active · verified Sat Apr 11

HTML5Tagger is a Python library designed for Pythonic HTML generation and templating without the need for external template files. It provides a simplified HTML5 syntax, allowing developers to create entire HTML documents using only Python code. Maintained under the Sanic organization, it aims for speed and simplicity, offering a pure Python implementation with no external dependencies. The library sees infrequent but consistent updates, with the latest major release in March 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic HTML5 document using `html5tagger`. It shows how to initialize a `Document` object, add standard HTML elements using attribute access (e.g., `doc.h1`), pass attributes as keyword arguments, nest elements using `with` statements, and utilize template variables for dynamic content. Finally, it shows how to render the complete HTML string.

from html5tagger import Document, E

# Create a full HTML document
doc = Document(
    E.TitleText_,
    lang="en", # HTML tag attributes as keyword arguments
    _urls=["style.css", "script.js"], # Special argument for linking resources
)

# Add elements directly or with context managers
with doc.body:
    doc.h1("Welcome to html5tagger!")
    with doc.p("This is an example of creating HTML content in Python. It's ") as p:
        p.strong("easy")
        p(" and ")
        p.em("intuitive")
        p(".")
    
    # Nesting elements
    with doc.ul:
        doc.li("No manual closing tags for most elements.")
        doc.li("Attributes are passed as keyword arguments.")
        doc.li.a(href="https://github.com/sanic-org/html5tagger")("Check out the GitHub repo!")
    
    # Add a paragraph with a dynamic title using template variables
    doc.p(doc.IntroText_, id="intro-paragraph")

# Fill in template variables (optional)
doc.TitleText = "My Dynamic Page"
doc.IntroText = "Here's some dynamic content."

# Render the document to an HTML string
html_output = doc.render()
print(html_output)

view raw JSON →