Skip to main content

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.

.. code:: python

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`` dictionnary of default values, and an ``errors`` dictionnary of error messages to the ``Doc`` constructor.
Then, use the special ``input``, ``textarea``, ``select``, ``option`` methods when generating your documents.


Example with default values
~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

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

.. _yattag.org: http://www.yattag.org

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

yattag-1.0.0.zip (11.9 kB view details)

Uploaded Source

yattag-1.0.0.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file yattag-1.0.0.zip.

File metadata

  • Download URL: yattag-1.0.0.zip
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yattag-1.0.0.zip
Algorithm Hash digest
SHA256 b4bc3ddd5c8d3981c486ecb6e78f20272bba1e991127694da626105f29184dc1
MD5 7ed13f256ae61f64ca4ea6e7a07f0137
BLAKE2b-256 f80262414d628f58398f85ebd9e6bb6d3dcabdf0e5d004f486d7d9b6141c3732

See more details on using hashes here.

File details

Details for the file yattag-1.0.0.tar.gz.

File metadata

  • Download URL: yattag-1.0.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yattag-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b1f186a6795ff3565cb83a621e8e53250ab195b194c7c35b06e3c5fbec573ed4
MD5 7301ba7ab4c2418c2c94bde091e5dd4b
BLAKE2b-256 293d925f95ae223f8a856472f9948d0dc709efdd80173f9a9538ae8c0e7ecee5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page