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.
To install this module type the below command in the terminal.
pip install tinyhtml
- 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 2 : Using raw() and frag()
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.
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.
Example 5: Templating HTMLs using functions
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