![]() |
|
[Tut] Python Generate HTML – 3 Easy Ways - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Generate HTML – 3 Easy Ways (/thread-100512.html) |
[Tut] Python Generate HTML – 3 Easy Ways - xSicKxBot - 12-31-2022 Python Generate HTML – 3 Easy Ways 5/5 – (4 votes) Problem Statement: How to generate HTML documents in Python?One of the advantages of opting for Python as your programming language is that it is one of the most versatile languages, as it emphasizes code readability with extensive use of white space. It supports a large collection of libraries that serves various purposes, which include generating HTML documents in Python. Before we dive into the libraries, let us learn how we can actually write to an HTML file in Python. How to Write to an HTML File in Python? You can create and save HTML files with the help of a few simple steps:
Example: Code: # Creating the HTML file
file_html = open("demo.html", "w") # Adding the input data to the HTML file
file_html.write('''<html>
<head>
<title>HTML File</title>
</head> <body>
<h1>Welcome Finxters</h1> <p>Example demonstrating How to generate HTML Files in Python</p> </body>
</html>''') # Saving the data into the HTML file
file_html.close()Output: Here’s how the Code: demo.html![]() Code: <html>
<head>
<title>HTML File</title>
</head> <body>
<h1>Welcome Finxters</h1> <p>Example demonstrating How to generate HTML Files in Python</p> </body>
</html>When you open it in the browser, it looks like this: ![]() Method 1: Using the Airium Library Airium is a bidirectional HTML-Python translator that uses the DOM structure and is represented by the Python indentation with context managers. We need to install the Code: airiumCode: pip install airium == 0.2.5The biggest advantage of using the Airium library in Python is that it also has a reverse translator. This translator helps to build the Python code out of the HTML string. Example: The following example demonstrates how we can generate HTML docs using Airium. Code: # Importing the airium library
from airium import Airium a = Airium() # Generating HTML file
a('<!DOCTYPE html>') with a.html(lang="pl"): with a.head(): a.meta(charset="utf-8") a.title(_t="Example: How to use Airium library") with a.body(): with a.h1(id="id23345225", kclass='main_header'): a("Hello Finxters") # Casting the file to a string to extract the value
html = str(a) # Casting the file to UTF-8 encoded bytes:
html_bytes = bytes(a) print(html)Output: Code: <!DOCTYPE html>
<html lang="pl"> <head> <meta charset="utf-8" /> <title>Example: How to use Airium library</title> </head> <body> <h1 id="id23345225" kclass="main_header"> Hello Finxters </h1> </body>
</html>You can also store this document as a file using the following code: Code: with open('file.html', 'wb') as f: f.write(bytes(html, encoding='utf8'))Yattag is a Python library used to generate HTML or XML documents in a Pythonic way. If we are using the Yattag library, we don’t have to use the closing tag in HTML. It considers all the templates as the piece of code in Python. We can even render the HTML forms easily with default values and error messages. Before we dive into the solution, let us have a quick look at a few basics. How does the Code: yattag.DocCode: Yattag.DocCode: join()Code: text()Code: tag()Code: getvalue()What is the Code: tag()In Python, a Code: tag()Code: withCode: __enter__()Code: __exit__()Code: __enter__Code: __exit__The line Code: tag('h1')Code: <h1>Example: Code: # Importing the Yattag library
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('html'): with tag('body'): with tag('p', id = 'main'): text('We can write any text here') with tag('a', href = '/my-link'): text('We can insert any link here') result = doc.getvalue()
print(result)Output: Code: <html><body><p id="main">We can write any text here</p><a href="/my-link">We can insert any link here</a></body></html>It is easier and more readable to generate dynamic HTML documents with the Yattag library than to write static HTML docs. However, most of the time, when you are generating HTML documents, most of the tag nodes will contain only text. Hence, we can use the following line method to write these more concisely. Example: Code: doc, tag, text, line = Doc().ttl()
with tag('ul', id = 'To-dos'): line('li', 'Clean up the dishes', kclass = "priority") line('li', 'Call for appointment') line('li', 'Complete the paper')Output: Code: <ul id = 'To-dos'> <li class = "priority"> Clean up the dishes </li> <li> Call for appointment </li> <li> Complete the paper </li>
</ul>We can use the Code: XML.etreeCode: XML.etreeXML follows the hierarchical data format and is usually represented in the form of an element tree. The element tree also has two classes for this purpose.
Example: Code: # Importing the XML package and the sys module
import sys
from xml.etree import ElementTree as ET html = ET.Element('html')
body = ET.Element('body')
html.append(body)
div = ET.Element('div', attrib={'class': 'foo'})
body.append(div)
span = ET.Element('span', attrib={'class': 'bar'})
div.append(span)
span.text = "Hello Finxters. This article explains how to generate HTML documents in Python." # Here, the code checks the Python version.
if sys.version_info < (3, 0, 0): # If the Python version is less than 2.0 ET.ElementTree(html).write(sys.stdout, encoding='utf-8', method='html')
else: # For versions Python 3 and above ET.ElementTree(html).write(sys.stdout, encoding='unicode', method='html')Output: Code: <html><body><div class="foo"><span class="bar">Hello Finxters. This article explains how to generate HTML documents in Python.</span></div></body></html>That’s all about generating HTML documents in Python. I hope you found this article helpful. Please stay tuned and subscribe for more such interesting articles. Happy learning! Authors: Rashi Agarwal and Shubham Sayon Recommended Read: How to Get an HTML Page from a URL in Python? Web Scraping with BeautifulSoup ![]() One of the most sought-after skills on Fiverr and Upwork is web scraping . Make no mistake: extracting data programmatically from web sites is a critical life-skill in today’s world that’s shaped by the web and remote work. This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping. |