Generate HTML using Tinyhtml Module using Python


Tinyhtml is a Python library used to generate HTML5 expressions or code. This is useful for generating HTML code when you are not as knowledgeable about the syntax of HTML. As the name says so, it is a ‘tiny’ library and can render HTML5 expressions.

There are many methods of rendering HTML code using tinyhtml few of which we’ll be seeing. To learn more, go through the documentation here.

Due to the lightweight nature of tinyhtml, it is easier to integrate it with other tools, for example, HTML code snippets can be rendered in Jupyter Notebooks by importing the module to the notebook you’re working in.

It is also easier to integrate it with Python apps like Flask or Django to generate HTML code snippets dynamically, that is, there’s no need to manually render the code all the time.

To use tinyhtml in our systems, we need to install it with the following command:

pip install tinyhtml

After installing the module in our system, we can start working with it in our systems and incorporate it into our projects.

import tinyhtml

After importing all the modules to be used from the tinyhtml module, we try out different methods to render HTML code. In general, it is important to keep these basic functions in mind.

html() - Depicts the <html> element in HTML code. It depicts the start of HTML code.

Syntax

html(lang="en")(
   #type content here
)

h() - The h() can be used to depict other elements present in HTML. In the above code, it’s used to define the <head> tag in HTML and also to depict the encoding of ‘utf-8’.

We need to properly define the tags, else the code may throw the error.

html(lang="en")(
   h("head")(
    h("p")(
      #type content here
     )
   )
)

Note: All of this should be declared inside the html() brackets.

Frag() Method

frag() - It groups different HTML elements together and is more user-friendly to apply than simply rendering the code with “html()”.

Syntax

html(lang="en")(
   frag(h("h1")("Example"),
     h("p")("Lorem Ipsum")
    )
)

Raw() Method

raw() - Prints the raw unedited HTML code provided by the user. This is best used if the user knows HTML syntax.

Syntax

#can print with any tags available in HTML.
raw("<h2>This is an example</h2>")

Example 1

In this example, we see various ways to generate HTML code using the tinyhtml module.

Algorithm

  • Import the necessary libraries.

  • Depict each way to render HTML code.

  • Use the HTML function in Python to render it first and print it.

  • Use the frag() method to render code and print it.

  • Use the raw() method to render HTML code and print it.

from tinyhtml import html, h, frag, raw

#to generate HTML code
htm=html(lang="en")(
   h("head")(
    h("meta", charset='utf-8')
   ),
).render()

print("HTML Code: \n", htm)

#how frag() works
htm_frag=html(lang="en")(
   frag(h("h1")("This is a sample header"),
     h("p")("This is a sample paragraph declared under the head in HTML"))
).render()
print("\n\nThis is an use case of frag()\n", htm_frag)

#how raw() works
htm_raw=raw('<h1>Printing the Raw HTML code</h1>')
print("\n\nThe raw way of printing HTML code: \n", htm_raw)

Output

Let us see the frag() HTML rendered code:

Example 2

This code is taken from the code rendered from the frag() function seen above.

<!DOCTYPE html><html lang="en"><h1>This is a sample header</h1>
<p>This is a sample paragraph declared under the head in HTML</p></html>

Run this code and open it in the browser:

Output

Thus we can infer that the HTML code generated can be used for web development too.

Conclusion

The tinyhtml module is a relatively new module released as a project. Since it is so new, it may be subject to more frequent updates, and maybe changes in syntax. It is optimal for generating HTML code without any whitespaces, which makes running the HTML code faster in case of web development. On the flip side, this significantly decreases code readability.

Another advantage is that declaring templates can be done in functions. This improves code reusability and the same template can be called in multiple places as a user-defined function due to this.

Due to it being so new, it has a smaller ecosystem and community support, due to which coding in tinyhtml as a beginner will be quite daunting due to the lack of solutions available online.

Updated on: 10-Aug-2023

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements