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

https://travis-ci.org/leforestier/yattag.svg

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

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


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.7.0.zip (27.1 kB view details)

Uploaded Source

yattag-1.7.0.tar.gz (22.8 kB view details)

Uploaded Source

File details

Details for the file yattag-1.7.0.zip.

File metadata

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

File hashes

Hashes for yattag-1.7.0.zip
Algorithm Hash digest
SHA256 c72cf834abe4a8440df39387fa0e1215b4a521d5f2e9707ad0b41924d34ac4db
MD5 56225c90997fb51cb2ff3f7e868fc49a
BLAKE2b-256 f076f565e3eacc99a6f3a30d14db0913a7c6d89b6a94c437a6d436cbe0599a16

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for yattag-1.7.0.tar.gz
Algorithm Hash digest
SHA256 c61c7a2eedcf7b81c9fa68f433a979351b91d1f20bf9f88d0d84f56c745787e8
MD5 3e31f5aa3221ba808846df6c3eaa7b39
BLAKE2b-256 9cae8479ece8faf5ca90c25457b89e67c9fe4537f00e97e0a21076ac7aef8901

See more details on using hashes here.

Supported by

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