yattag


Nameyattag JSON
Version 1.15.2 PyPI version JSON
download
home_pagehttps://www.yattag.org
SummaryGenerate HTML or XML in a pythonic way. Pure python alternative to web template engines.Can fill HTML forms with default values and error messages.
upload_time2023-10-27 12:17:29
maintainer
docs_urlNone
authorBenjamin Le Forestier
requires_python
license
keywords html template templating xml document form rendering
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. image:: https://travis-ci.org/leforestier/yattag.svg
    :target: https://travis-ci.org/leforestier/yattag

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

.. 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: https://www.yattag.org

            

Raw data

            {
    "_id": null,
    "home_page": "https://www.yattag.org",
    "name": "yattag",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "html,template,templating,xml,document,form,rendering",
    "author": "Benjamin Le Forestier",
    "author_email": "benjamin@leforestier.org",
    "download_url": "https://files.pythonhosted.org/packages/75/49/51045efa8c4e42831c80e0031ee4139450347e587e5cda0a731198ed6e36/yattag-1.15.2.tar.gz",
    "platform": null,
    "description": ".. image:: https://travis-ci.org/leforestier/yattag.svg\n    :target: https://travis-ci.org/leforestier/yattag\n\nWhy use a template engine when you can generate HTML or XML documents with Python in a very readable way?\n\n( full tutorial on yattag.org_ )\n\nBasic example\n-------------\n\nNested html tags, no need to close tags.\n\n.. code:: python\n\n    from yattag import Doc\n\n    doc, tag, text = Doc().tagtext()\n\n    with tag('html'):\n        with tag('body', id = 'hello'):\n            with tag('h1'):\n                text('Hello world!')\n\n    print(doc.getvalue())\n\n\nHtml form rendering\n-------------------\n\nYattag can fill your HTML forms with default values and error messages.\nPass a ``defaults`` dictionary of default values, and an ``errors`` dictionary of error messages to the ``Doc`` constructor.\nThen, use the special ``input``, ``textarea``, ``select``, ``option`` methods when generating your documents.\n\n\nExample with default values\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n    from yattag import Doc\n\n    doc, tag, text = Doc(\n        defaults = {'ingredient': ['chocolate', 'coffee']}\n    ).tagtext()\n\n    with tag('form', action = \"\"):\n        with tag('label'):\n            text(\"Select one or more ingredients\")\n        with doc.select(name = 'ingredient', multiple = \"multiple\"):\n            for value, description in (\n                (\"chocolate\", \"Dark chocolate\"),\n                (\"almonds\", \"Roasted almonds\"),\n                (\"honey\", \"Acacia honey\"),\n                (\"coffee\", \"Ethiopian coffee\")\n            ):\n                with doc.option(value = value):\n                    text(description)\n        doc.stag('input', type = \"submit\", value = \"Validate\")\n\n    print(doc.getvalue())\n\nExample with default values and errors\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n    from yattag import Doc\n\n    doc, tag, text = Doc(\n        defaults = {\n            'title': 'Untitled',\n            'contact_message': 'You just won the lottery!'\n        },\n        errors = {\n            'contact_message': 'Your message looks like spam.'\n        }\n    ).tagtext()\n\n    with tag('h1'):\n        text('Contact form')\n    with tag('form', action = \"\"):\n        doc.input(name = 'title', type = 'text')\n        with doc.textarea(name = 'contact_message'):\n            pass\n        doc.stag('input', type = 'submit', value = 'Send my message')\n\n    print(doc.getvalue())\n\nFull tutorial on yattag.org_\n\nGitHub repo: https://github.com/leforestier/yattag\n\n.. _yattag.org: https://www.yattag.org\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "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.",
    "version": "1.15.2",
    "project_urls": {
        "Homepage": "https://www.yattag.org"
    },
    "split_keywords": [
        "html",
        "template",
        "templating",
        "xml",
        "document",
        "form",
        "rendering"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "754951045efa8c4e42831c80e0031ee4139450347e587e5cda0a731198ed6e36",
                "md5": "d997d0f2484f03eeee8119a431e90a6c",
                "sha256": "aad9f540bd22dc503e5b5506cc47856facf081aa71fd35f727371b63e1e402bf"
            },
            "downloads": -1,
            "filename": "yattag-1.15.2.tar.gz",
            "has_sig": false,
            "md5_digest": "d997d0f2484f03eeee8119a431e90a6c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 28653,
            "upload_time": "2023-10-27T12:17:29",
            "upload_time_iso_8601": "2023-10-27T12:17:29.462596Z",
            "url": "https://files.pythonhosted.org/packages/75/49/51045efa8c4e42831c80e0031ee4139450347e587e5cda0a731198ed6e36/yattag-1.15.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-27 12:17:29",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "yattag"
}
        
Elapsed time: 0.13416s