Generate HTML using tinyhtml module in Python
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
html_content = html(lang = "en" )(
h( "head" )(
(h( "h1" )( "hello Geeks!!" )),
),
).render()
print (html_content)
|
Output:
Example 2 : Using raw() and frag()
Python3
from tinyhtml import html, h, frag, raw
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" )
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
print ( "Working with Class : " )
class_inp = h( "div" , klass = "gfg" )()
print (class_inp)
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
print ( "Using loop elements : " )
looped_element = h( "ul" )(h( "li" )(idx) for idx in range ( 5 ))
print (looped_element)
print ( "\n" )
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
def create_layout(title, body):
return html()(
h( "head" )(
h( "title" )(title),
),
h( "body" )(body)
)
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
Last Updated :
04 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...