Yattag is a Python library package that provides an easy way to create HTML or XML documents using Python code. The package is used for generating dynamic web pages or web-based documents without knowing their structure.
The yattag implementation of the document object model (DOM) is very simple with a few functions. It is easy to learn and use since there are very few API functions.
Here are some examples of how to use yattag in Python code to generate HTML or XML documents.
1. Example for HTML element creation -
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('h1'): text('Hello, world!') print(doc.getvalue())
This code creates an HTML document with an h1 tag and the text value of 'Hello, world!'.
2. Example for anchor link generation -
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('a', href='http://www.google.com'): text('Google') print(doc.getvalue())
This code generates an anchor tag element with Google as the text value with the URL=http://www.google.com.
3. Example for bulk div element creation -
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('div'): for i in range(5): with tag('p'): text('This is paragraph %d.' % (i + 1)) print(doc.getvalue())
This code creates five different paragraph elements inside a div element with varying text values.
4. Example for XML document creation -
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('greeting'): with tag('name'): text('John') with tag('message'): text('Hello, world!') print(doc.getvalue())
This code is generating a simple XML document which contains two tags greeting and name and message with their respective text.
These examples demonstrate how the yattag package is used to create HTML and XML documents. The package is flexible enough to be used for a variety of different HTML/XML document generating needs.
Python Doc.asis - 60 examples found. These are the top rated real world Python examples of yattag.Doc.asis extracted from open source projects. You can rate examples to help us improve the quality of examples.