Generate HTML using tinyhtml module in Python
Last Updated :
04 Jul, 2021
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
Similar Reads
Rule-Based Data Extraction in HTML using Python textminer Module While working with HTML, there are various requirements to extract data from plain HTML tags in an ordered manner in form of python data containers such as lists, dict, integers, etc. This article deals with a library that helps to achieve this using a rule-based approach. Features of Python - textm
3 min read
Story Generator App Using Python In the realm of programming and natural language processing, there's an opportunity to create engaging and creative applications that spark the imagination. One such project is the development of a Story Generator App using Python. In this article, we'll embark on a journey to understand how to buil
4 min read
Create HTML User Interface using Eel in Python Eel is a Python library for making simple offline HTML/JS GUI apps, with full access to Python capabilities and libraries. Eel hosts a local webserver, then lets you annotate functions in Python so that they can be called from JavaScript, and vice versa.InstallationTo install this module, type the b
2 min read
Convert HTML source code to JSON Object using Python In this post, we will see how we can convert an HTML source code into a JSON object. JSON objects can be easily transferred, and they are supported by most of the modern programming languages. We can read JSON from Javascript and parse it as a Javascript object easily. Javascript can be used to make
3 min read
How to make HTML files open in Chrome using Python? Prerequisites: Webbrowser HTML files contain Hypertext Markup Language (HTML), which is used to design and format the structure of a webpage. It is stored in a text format and contains tags that define the layout and content of the webpage. HTML files are widely used online and displayed in web brow
2 min read
How to use HTML in Tkinter - Python? Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to
2 min read