P3 contains some core features of @mbostock's awesome d3 in python.
Using d3 on a daily basis I'm now most comfortable driving the DOM with data. When I came to do some server side work in python I missed d3's abstractions, and felt uncomfortable concatenating strings with templating languages.
P3 ports much of d3's core library for manipulating documents to python.
The document itself is provided by lxml
pip install p3
Import the P3 class from p3
from p3 import P3
Create a new P3 instance with no args.
p3 = P3()
print(p3.html())
calling .html outputs the document associated with this p3 instance. Here the the default empty document is displayed.
<!doctype html>
<html>
<head></head>
<body></body>
</html>
You might already have a document though, in which case just pass it into the constructor
from lxml.html import builder as E
from p3 import P3
doc = E.HTML(
E.HEAD(),
E.BODY(
E.DIV(E.OL())
)
)
p3 = P3(doc)
print(p3.html())
<!doctype html>
<html>
<head></head>
<body>
<div>
<ol></ol>
</div>
</body>
</html>
teas = [
'breakfast',
'darjeeling',
'earl grey',
'peppermint'
]
p3 = P3()
sel = p3.select('body').create('div').classed('container', True)
sel = sel.create('ul')
update = sel.select_all('li').data(teas)
update.enter().create('li')
update.text(lambda n, d, i: "lovely %s tea" % d)
print(p3.html())
<!doctype html>
<html>
<head></head>
<body>
<div class="container">
<ul>
<li>lovely breakfast tea</li>
<li>lovely darjeeling tea</li>
<li>lovely earl grey tea</li>
<li>lovely peppermint tea</li>
</ul>
</div>
</body>
</html>