Generate HTML or XML in a pythonic way. Pure python alternative to web template engines.Can fill HTML forms with default values and error messages.
Project description
Why use a template engine when you can generate HTML or XML documents with Python in a very readable way?
( full tutorial on yattag.org )
Basic example
Nested html tags, no need to close tags.
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('html'):
with tag('body', id = 'hello'):
with tag('h1'):
text('Hello world!')
print(doc.getvalue())
Html form rendering
Yattag can fill your HTML forms with default values and error messages. Pass a defaults dictionary of default values, and an errors dictionary of error messages to the Doc constructor. Then, use the special input, textarea, select, option methods when generating your documents.
Example with default values
from yattag import Doc
doc, tag, text = Doc(
defaults = {'ingredient': ['chocolate', 'coffee']}
).tagtext()
with tag('form', action = ""):
with tag('label'):
text("Select one or more ingredients")
with doc.select(name = 'ingredient', multiple = "multiple"):
for value, description in (
("chocolate", "Dark chocolate"),
("almonds", "Roasted almonds"),
("honey", "Acacia honey"),
("coffee", "Ethiopian coffee")
):
with doc.option(value = value):
text(description)
doc.stag('input', type = "submit", value = "Validate")
print(doc.getvalue())
Example with default values and errors
from yattag import Doc
doc, tag, text = Doc(
defaults = {
'title': 'Untitled',
'contact_message': 'You just won the lottery!'
},
errors = {
'contact_message': 'Your message looks like spam.'
}
).tagtext()
with tag('h1'):
text('Contact form')
with tag('form', action = ""):
doc.input(name = 'title', type = 'text')
with doc.textarea(name = 'contact_message'):
pass
doc.stag('input', type = 'submit', value = 'Send my message')
print(doc.getvalue())
Full tutorial on yattag.org
GitHub repo: https://github.com/leforestier/yattag
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file yattag-1.16.1.tar.gz
.
File metadata
- Download URL: yattag-1.16.1.tar.gz
- Upload date:
- Size: 29.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baa8f254e7ea5d3e0618281ad2ff5610e0e5360b3608e695c29bfb3b29d051f4 |
|
MD5 | 2a48bb970f2f023159ba26cf2276cb09 |
|
BLAKE2b-256 | 1c1ad3b2a2b8f843f5e7138471c4a5c9172ef62bb41239aa4371784b7448110c |