Which python html generator module should be used in a non-web application?

I am hacking a quick and dirty python script to generate some reports as static html files.

What would be a good module to easily build static html files outside the context of a web application?

My goals are simplicity (HTML won't be very complicated) and ease of use (I don't want to write a lot of code just to output some html tags).

I found two alternatives in my first goolge search:

In addition, I feel that using the template engine will be overkill, but if you are different, say it and why.

Any other recommendations?

+3
source share
6 answers

Perhaps you could try Markdown and convert it to HTML on the fly?

+5
source

You don’t necessarily need something complicated - for example, here is a 150-line library for generating HTML in functional mode:

http://github.com/Yelp/PushmasterApp/blob/master/pushmaster/taglib.py

(Full disclosure, I work with the person who originally wrote this version, and I also use it myself.)

+3
source

? -, ( , ). Mako, , , html ( db ..),

+3

HTML. , .

import string

TEMPLATE_FORMAT = """
<html>
<head><title>Trial</title></head>
<body>
    <div class="myclass">$my_div_data</div>
</body>
"""
my_div_data = "some_data_to_display_in_HTML"
TEMPLATE    = string.Template(TEMPLATE_FORMAT)
html_data   = TEMPLATE.safe_substitute(my_div_data)
open("out.html", "w").write(html_data)

, HTML . , , .

+2

ElementTree html . :

from xml.etree.ElementTree import ElementTree, Element, SubElement
import sys 

html = Element('html')

head = SubElement(html, 'head')
style = SubElement(head, 'link')
style.attrib = {'rel': 'stylesheet', 'href': 'style.css', 'type': 'text/css'}
body = SubElement(html, 'body')

para = SubElement(body, 'p')
para.text = 'Lorem ipsum sit amet'

doc = ElementTree(html)
doc.write(sys.stdout)

html : Jinja2, Mako, Cheetah, .

+2
+1

Source: https://habr.com/ru/post/1768546/