Generate HTML using Tinyhtml Module using Python

Tinyhtml is a Python library for generating HTML5 code programmatically. This lightweight library is useful when you need to create HTML without manually writing markup syntax, making it ideal for dynamic web content generation.

The library integrates easily with Python frameworks like Flask and Django, and can render HTML snippets in Jupyter Notebooks. Its compact nature makes it perfect for generating HTML code dynamically without manual intervention.

Installation

Install tinyhtml using pip ?

pip install tinyhtml

Core Functions

Tinyhtml provides several key functions for HTML generation ?

  • html() Creates the root HTML element
  • h() Generates any HTML tag
  • frag() Groups multiple HTML elements together
  • raw() Inserts raw HTML code directly

Basic HTML Structure

Create a basic HTML document with proper structure ?

from tinyhtml import html, h

# Generate basic HTML structure
htm = html(lang="en")(
    h("head")(
        h("meta", charset='utf-8'),
        h("title")("My Page")
    ),
    h("body")(
        h("h1")("Welcome"),
        h("p")("This is generated by tinyhtml")
    )
).render()

print(htm)
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>My Page</title></head><body><h1>Welcome</h1><p>This is generated by tinyhtml</p></body></html>

Using frag() Method

The frag() method groups multiple elements for cleaner code ?

from tinyhtml import html, h, frag

# Using frag() to group elements
htm_frag = html(lang="en")(
    frag(
        h("h1")("Sample Header"),
        h("p")("This paragraph is grouped with the header"),
        h("ul")(
            h("li")("First item"),
            h("li")("Second item")
        )
    )
).render()

print(htm_frag)
<!DOCTYPE html><html lang="en"><h1>Sample Header</h1><p>This paragraph is grouped with the header</p><ul><li>First item</li><li>Second item</li></ul></html>

Using raw() Method

Insert raw HTML code directly when you know the exact markup ?

from tinyhtml import raw

# Insert raw HTML
htm_raw = raw('<h2 style="color: blue;">Raw HTML Header</h2><p>This is raw HTML content.</p>')
print(htm_raw)
<h2 style="color: blue;">Raw HTML Header</h2><p>This is raw HTML content.</p>

Complete Example

Here's a comprehensive example showing all methods together ?

from tinyhtml import html, h, frag, raw

# Complete HTML page with different methods
page = html(lang="en")(
    h("head")(
        h("meta", charset='utf-8'),
        h("title")("Tinyhtml Demo")
    ),
    h("body")(
        frag(
            h("h1")("Tinyhtml Examples"),
            h("p")("Generated using frag() method"),
            raw('<div style="background-color: #f0f0f0; padding: 10px;">Raw HTML div</div>'),
            h("footer")("© 2024 Generated by tinyhtml")
        )
    )
).render()

print(page)
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Tinyhtml Demo</title></head><body><h1>Tinyhtml Examples</h1><p>Generated using frag() method</p><div style="background-color: #f0f0f0; padding: 10px;">Raw HTML div</div><footer>© 2024 Generated by tinyhtml</footer></body></html>

Comparison of Methods

Method Use Case Advantage
html() Root HTML element Structured approach
h() Individual HTML tags Flexible tag creation
frag() Grouping elements Cleaner code organization
raw() Direct HTML insertion Quick HTML injection

Conclusion

Tinyhtml provides a programmatic way to generate HTML5 code in Python. Use html() and h() for structured generation, frag() for grouping elements, and raw() for direct HTML insertion when building dynamic web applications.

Updated on: 2026-03-27T12:02:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements