Open In App

Generate HTML using tinyhtml module in Python

Last Updated : 04 Jul, 2021
Like Article

Creating HTML can sometimes be tedious tasks and difficult to debug and error-prone. A way to solve this is using some library which can take care of the opening and closing div, etc, which can reduce chances for mistakes. We will use the tinyhtml module for this purpose.

This module provides a set of classes helpful in rendering html5 using Python code and allows to group several HTML tags together. It also helps to publish raw unescaped HTML, providing the functionality of looping or type conversions using builders.


To install this module type the below command in the terminal.

pip install tinyhtml

Functions Used

  • html() : Marks beginning of html code.
  • h() : Most utility function, allows to render attributes, normal elements, and void/self-closing elements.
  • raw() : Used to print unescaped html strings.
  • frag() : Groups several HTML tags together.
  • render() : Processes and converts the input html.

Example 1:


from tinyhtml import html, h
# Constructing HTML using html() and h()
# nested h() is also supported
html_content = html(lang="en")(
        (h("h1")("hello Geeks!!")),
# printing html formed on console.


Example 2 : Using raw() and frag()


from tinyhtml import html, h, frag, raw
# using frag() to group to h fncs.
print("Working of frag() : ")
html_content = html(lang="en")(
    frag(h("h1")("Welcome to GFG"), h("p")("This\
    is one among best ever coding site you've been\
# prints raw unescaped HTML.
print("The unescaped HTML raw content : ")
print(raw('<h1>Printing Raw HTML</h1>
<p> Dont escape <<>>>> </p>

Output :

Example 3: Using Classes and labels as HTML

In this, we use “klass” operator to initialize a class. And for other labels which can coincide with the naming of Python reserved keywords, a trailing underscore is appended. 


from tinyhtml import h
# using klass to initialize class
print("Working with Class : ")
class_inp = h("div", klass="gfg")()
# using _ to escape for loop operator
print("Working with label and escaping keyword : ")
label_inp = h("label", for_="geeksforgeeks")("GFG")

Output : 

Example 4: Working with loop and conditionals

Rendering of HTML content that requires loops like list elements, and conditionals is also possible by basic python loops and conditionals.


from tinyhtml import h
# initializing loop elements
print("Using loop elements : ")
looped_element = h("ul")(h("li")(idx) for idx in range(5))
# using conditionals
print("Using conditional elements : ")
conditional_element = h("ul")(
    h("li")("Gfg") if False else "GFG", h("li")("Geeks"))

Output : 

Example 5: Templating HTMLs using functions


from tinyhtml import h, html, frag
# function to create layout.
# advantage is that this can be reused.
def create_layout(title, body):
    return html()(
# calling function to create layout.
layout = create_layout("Gfg Templating", frag(
    h("h1")("Demo Heading"),
    h("p")("Making fragment to demo templating layout"),
print("Created layout : ")

Output : 

Templating html

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads