Python HTML Generator using Yattag Part 2
Python
HTML Generator
5. Shortcut for nodes that contain only text
If you're producing HTML
or XML, you've probably realized that most tag nodes contain only text. In
order to write these in a terser way, use the line method.
doc, tag, text, line=Doc().ttl()
with tag('ul', id='course'):
line('li', 'BCA', klass="degree")
line('li', 'BBA')
line('li', 'BSW')
You'll get:
<ul id="course">
<li class="degree">BCA</li>
<li>BBA</li>
<li>BSW</li>
</ul>
Oh, I forgot to
introduce the ttl method I just used in the example above.
It works just like
the tagtext method I've talked about in the beginning of the
tutorial, but instead of returning the triplet (doc, doc.tag, doc.text),
it returns the quadruplet (doc, doc.tag, doc.text, doc.line).
6. Indentation
We've added indentation
in the example above for readability purpose, but actually the getvalue method
doesn't add spaces, indentation or new lines between tags.
Consequently, for big
documents, the HTML or XML output is not as readable as hand-made HTML or XML.
In most cases, this is
not a problem at all. For example, when debugging a web application, you'll
look at the Python/Yattag code directly. Yattag code is in fact, more readable
than handmade HTML. The Python interpreter guarantees that all your HTML tags
are closed, and contrary to hand-made HTML, you'll get a syntax error if you don't
close a quoted string, so it's really not necessary to look at the HTML output.
Moreover, you can use
the "Inspect Element" function in Firefox (right click somewhere in
the page and click on "Inspect Element") to see the HTML document
tree in a well indented and browsable fashion.
However, there might be
a few situations where you want to produce well indented documents (for example
when generating XML configuration files intended to be editable by hand). For
that, you can use the indent function.
from yattag import indent
...
result=indent(doc.getvalue())
The indent function
takes a string representing a xml or html document, and returns a well indented
version of this document. The indent function is completely
decoupled from the rest of Yattag. You can indent documents that were not
generated by Yattag if you want.
You can tweak the end
result by supplying these keyword arguments:
·
indentation: the indentation unit. By default, it's two
spaces.
·
newline: the string to be used for new lines. By default,
it's '\n'. Maybe Windows users would like to set this to '\r\n'.
·
indent_text: by default, the content of nodes that directly
contain some text (for example <p>Hello <b>world!</b></p>) is left untouched. If you want the content of
these nodes to be indented as well, use indent_text=True.
result=indent(doc.getvalue(), indentation=' ', newline='\r\n',
indent_text=True)
Don't use the indent function
to output HTML in a web application. Although this function is relatively fast,
this would remain a waste of cpu time and bandwidth. Think of the planet!
Example:
from yattag import Doc
from yattag import indent
doc, tag, text, line=Doc().ttl()
doc.asis('<!DOCTYPE html>')
with tag('html'):
with tag('body'):
with tag('h1'):
text('Welcome to Python')
with tag('div', id='photo-container'):
doc.stag('img',src='/myphoto.jpg', klass="photo")
with tag('p'):
text('Piyush Patel')
with tag('ul', id='course'):
line('li', 'BCA', klass="degree")
line('li', 'BBA')
line('li', 'BSW')
print(indent(doc.getvalue()))
Output:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<div id="photo-container">
<img src="/myphoto.jpg" class="photo" />
</div>
<p>Piyush Patel</p>
<ul id="course">
<li class="degree">BCA</li>
<li>BBA</li>
<li>BSW</li>
</ul>
</body>
</html>
Comments
Post a Comment