Generate HTML using tinyhtml Module in Python
In this tutorial, we will learn how we can generate HTML using Python code. We will learn about the tinyhtml module and generate some HTML. Creating HTML can be very hectic and challenging, and sometimes it takes lots of time to debug and is error-prone. While creating an HTML page, we forget to close the div tag or many other silly mistakes, but we can avoid chances for errors using the tinyhtml module.
This module provides advanced methods and classes that help render html5 using Python code. It allows us to group several HTML tags. Using this module, we can also publish raw unescaped HTML, providing the functionality of looping or type conversion using builders.
To use this library, we need to install it using the below command.
We will use the following functions -
Let's understand the following example -
<!DOCTYPE html><html lang="en"><head><h1>hello Learners!!</h1></head></html>
In the above code, we have constructed the HTML using html() and h(). We can also use h() is also supported.
Let's understand another example -
Working of frag() function : <!DOCTYPE html><html lang="en"><h1>Welcome to Javatpoint</h1><p>You will found here best tutorials on the latest technology</p></html> The unescaped HTML raw content : <h1>Printing Raw HTML</h1><p> Don?t escape <<>>>> </p>
Using Classes and Labels as HTML
In this section, we use the "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.
Let's understand the following code.
Working with klass operator : <div class="jtp"></div> Working with label and escaping keyword : <label for="Javatpoint">JTP</label>
Working with Loop and Conditional
We can use Python loops and conditional statements to generate HTML content. Let's understand the following example.
Using loop elements : <ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul> Using conditional elements : <ul>JTP<li>java</li></ul>
In this tutorial, we have used tinyhtml to generate HTML tags. We have shown some simple tags, you can modify according to the need. You can also check its official documentation.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week