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 Links
Meta
Author: Benjamin Le Forestier
Classifiers
Environment
- Web Environment
Intended Audience
- Developers
License
- OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Operating System
- OS Independent
Programming Language
- Python
- Python :: 2
- Python :: 2.6
- Python :: 2.7
- Python :: 3
- Python :: 3.2
- Python :: 3.3
- Python :: 3.4
- Python :: 3.5
- Python :: 3.6
- Python :: 3.7
Topic
- Internet :: WWW/HTTP
- Internet :: WWW/HTTP :: Dynamic Content
- Software Development :: Libraries :: Python Modules
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
1.16.1
Nov 02, 2024
1.16.0
Aug 06, 2024
1.15.2
Oct 27, 2023
1.15.1
Mar 03, 2023
1.15.0
Jan 02, 2023
1.14.0
Aug 06, 2020
1.13.2
Jan 13, 2020
1.13.1
Dec 25, 2019
1.13.0
Dec 24, 2019
1.12.2
Jul 23, 2019
1.12.1
Jul 23, 2019
1.12.0
Jul 20, 2019
1.11.2
Jun 07, 2019
1.11.1
Feb 08, 2019
1.11.0
Feb 07, 2019
1.10.1
Nov 11, 2018
1.10.0
Feb 04, 2018
1.9.2
Oct 26, 2017
1.9.1
Oct 26, 2017
1.9.0
Aug 22, 2017
1.8.0
Mar 19, 2017
1.7.2
Nov 11, 2016
1.7.1
Oct 12, 2016
1.7.0
Oct 08, 2016
1.6.0
May 08, 2016
1.5.3
Apr 05, 2016
1.5.2
Oct 11, 2015
1.5.1
Sep 19, 2015
1.5.0
May 12, 2015
1.1.0
Feb 04, 2015
1.0.7
Sep 13, 2014
1.0.6
Sep 12, 2014
1.0.5
Aug 21, 2014
1.0.4
Aug 03, 2014
1.0.3
Jul 23, 2014
1.0.2
Jul 18, 2014
1.0.1
Jun 30, 2014
1.0.0
Jun 30, 2014
0.9.6
May 03, 2014
0.9.5
May 01, 2014
0.9.4
Apr 23, 2014
0.9.3
Apr 17, 2014
0.9.2
Apr 14, 2014
0.9.1
Apr 13, 2014
0.9.0
Apr 09, 2014
0.8.4
Apr 07, 2014
0.8.3
Apr 06, 2014
0.8.2
Apr 04, 2014
0.8.1
Apr 03, 2014
0.8.0
Apr 02, 2014
Files in release
Extras:
None
Dependencies:
typing