Skip to content
Related Articles

Related Articles

Improve Article

Generate HTML using tinyhtml module in Python

  • Last Updated : 04 Jul, 2021
Geek Week

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.

Installation

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:

Python3






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

Output: 

Example 2 : Using raw() and frag()

Python3




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\
    to.."))).render()
  
print(html_content)
  
print("\n")
  
# 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. 



Python3




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

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.

Python3




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

Output : 

Example 5: Templating HTMLs using functions

Python3




from tinyhtml import h, html, frag
  
  
# function to create layout.
# advantage is that this can be reused.
def create_layout(title, body):
    return html()(
        h("head")(
            h("title")(title),
        ),
        h("body")(body)
    )
  
  
# 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 : ")
print(layout)

Output : 

Templating html

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :