2008-08-16 23:26:59 +08:00
|
|
|
====================================================
|
2010-07-27 03:15:15 +08:00
|
|
|
py.xml: Lightweight and flexible xml/html generation
|
2008-08-16 23:26:59 +08:00
|
|
|
====================================================
|
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
There are a plethora of frameworks and libraries to generate
|
|
|
|
xml and html trees. However, many of them are large, have a
|
|
|
|
steep learning curve and are often hard to debug. Not to
|
|
|
|
speak of the fact that they are frameworks to begin with.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
The py lib strives to offer enough functionality to represent
|
2010-07-27 03:15:15 +08:00
|
|
|
itself and especially its API in html or xml.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
.. _xist: http://www.livinglogic.de/Python/xist/index.html
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
a pythonic object model , please
|
2008-08-16 23:26:59 +08:00
|
|
|
================================
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
The py lib offers a pythonic way to generate xml/html, based on
|
|
|
|
ideas from xist_ which `uses python class objects`_ to build
|
|
|
|
xml trees. However, xist_'s implementation is somewhat heavy
|
|
|
|
because it has additional goals like transformations and
|
|
|
|
supporting many namespaces. But its basic idea is very easy.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
.. _`uses python class objects`: http://www.livinglogic.de/Python/xist/Howto.html
|
|
|
|
|
|
|
|
generating arbitrary xml structures
|
2010-07-27 03:15:15 +08:00
|
|
|
-----------------------------------
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
With ``py.xml.Namespace`` you have the basis
|
2010-07-27 03:15:15 +08:00
|
|
|
to generate custom xml-fragments on the fly::
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class ns(py.xml.Namespace):
|
|
|
|
"my custom xml namespace"
|
2008-08-16 23:26:59 +08:00
|
|
|
doc = ns.books(
|
|
|
|
ns.book(
|
2010-07-27 03:15:15 +08:00
|
|
|
ns.author("May Day"),
|
|
|
|
ns.title("python for java programmers"),),
|
|
|
|
ns.book(
|
|
|
|
ns.author("why"),
|
2008-08-16 23:26:59 +08:00
|
|
|
ns.title("Java for Python programmers"),),
|
2010-07-27 03:15:15 +08:00
|
|
|
publisher="N.N",
|
2008-08-16 23:26:59 +08:00
|
|
|
)
|
|
|
|
print doc.unicode(indent=2).encode('utf8')
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
will give you this representation::
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
<books publisher="N.N">
|
|
|
|
<book>
|
|
|
|
<author>May Day</author>
|
|
|
|
<title>python for java programmers</title></book>
|
|
|
|
<book>
|
|
|
|
<author>why</author>
|
|
|
|
<title>Java for Python programmers</title></book></books>
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
In a sentence: positional arguments are child-tags and
|
|
|
|
keyword-arguments are attributes.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
On a side note, you'll see that the unicode-serializer
|
|
|
|
supports a nice indentation style which keeps your generated
|
|
|
|
html readable, basically through emulating python's white
|
|
|
|
space significance by putting closing-tags rightmost and
|
2010-07-27 03:15:15 +08:00
|
|
|
almost invisible at first glance :-)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
basic example for generating html
|
2008-08-16 23:26:59 +08:00
|
|
|
---------------------------------
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Consider this example::
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
from py.xml import html # html namespace
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
paras = "First Para", "Second para"
|
|
|
|
|
|
|
|
doc = html.html(
|
|
|
|
html.head(
|
2010-07-27 03:15:15 +08:00
|
|
|
html.meta(name="Content-Type", value="text/html; charset=latin1")),
|
2008-08-16 23:26:59 +08:00
|
|
|
html.body(
|
|
|
|
[html.p(p) for p in paras]))
|
|
|
|
|
|
|
|
print unicode(doc).encode('latin1')
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Again, tags are objects which contain tags and have attributes.
|
|
|
|
More exactly, Tags inherit from the list type and thus can be
|
|
|
|
manipulated as list objects. They additionally support a default
|
|
|
|
way to represent themselves as a serialized unicode object.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
If you happen to look at the py.xml implementation you'll
|
|
|
|
note that the tag/namespace implementation consumes some 50 lines
|
|
|
|
with another 50 lines for the unicode serialization code.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
CSS-styling your html Tags
|
2008-08-16 23:26:59 +08:00
|
|
|
--------------------------
|
|
|
|
|
|
|
|
One aspect where many of the huge python xml/html generation
|
|
|
|
frameworks utterly fail is a clean and convenient integration
|
|
|
|
of CSS styling. Often, developers are left alone with keeping
|
|
|
|
CSS style definitions in sync with some style files
|
|
|
|
represented as strings (often in a separate .css file). Not
|
|
|
|
only is this hard to debug but the missing abstractions make
|
2010-07-27 03:15:15 +08:00
|
|
|
it hard to modify the styling of your tags or to choose custom
|
|
|
|
style representations (inline, html.head or external). Add the
|
|
|
|
Browers usual tolerance of messyness and errors in Style
|
|
|
|
references and welcome to hell, known as the domain of
|
|
|
|
developing web applications :-)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
By contrast, consider this CSS styling example::
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class my(html):
|
|
|
|
"my initial custom style"
|
|
|
|
class body(html.body):
|
|
|
|
style = html.Style(font_size = "120%")
|
|
|
|
|
|
|
|
class h2(html.h2):
|
|
|
|
style = html.Style(background = "grey")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class p(html.p):
|
|
|
|
style = html.Style(font_weight="bold")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
doc = my.html(
|
2010-07-27 03:15:15 +08:00
|
|
|
my.head(),
|
2008-08-16 23:26:59 +08:00
|
|
|
my.body(
|
2010-07-27 03:15:15 +08:00
|
|
|
my.h2("hello world"),
|
|
|
|
my.p("bold as bold can")
|
2008-08-16 23:26:59 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
print doc.unicode(indent=2)
|
|
|
|
|
|
|
|
This will give you a small'n mean self contained
|
|
|
|
represenation by default::
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
<html>
|
|
|
|
<head/>
|
|
|
|
<body style="font-size: 120%">
|
|
|
|
<h2 style="background: grey">hello world</h2>
|
|
|
|
<p style="font-weight: bold">bold as bold can</p></body></html>
|
|
|
|
|
|
|
|
Most importantly, note that the inline-styling is just an
|
2010-07-27 03:15:15 +08:00
|
|
|
implementation detail of the unicode serialization code.
|
2008-08-16 23:26:59 +08:00
|
|
|
You can easily modify the serialization to put your styling into the
|
|
|
|
``html.head`` or in a separate file and autogenerate CSS-class
|
2010-07-27 03:15:15 +08:00
|
|
|
names or ids.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
Hey, you could even write tests that you are using correct
|
|
|
|
styles suitable for specific browser requirements. Did i mention
|
2010-07-27 03:15:15 +08:00
|
|
|
that the ability to easily write tests for your generated
|
|
|
|
html and its serialization could help to develop _stable_ user
|
|
|
|
interfaces?
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
More to come ...
|
|
|
|
----------------
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
For now, i don't think we should strive to offer much more
|
|
|
|
than the above. However, it is probably not hard to offer
|
|
|
|
*partial serialization* to allow generating maybe hundreds of
|
|
|
|
complex html documents per second. Basically we would allow
|
|
|
|
putting callables both as Tag content and as values of
|
|
|
|
attributes. A slightly more advanced Serialization would then
|
|
|
|
produce a list of unicode objects intermingled with callables.
|
|
|
|
At HTTP-Request time the callables would get called to
|
|
|
|
complete the probably request-specific serialization of
|
|
|
|
your Tags. Hum, it's probably harder to explain this than to
|
2010-07-27 03:15:15 +08:00
|
|
|
actually code it :-)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
.. _`py.test`: test/index.html
|