Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

sammyt/p3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P3

P3 contains some core features of @mbostock's awesome d3 in python.

Rational

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

Installation

pip install p3

Getting Started

Creating a P3 instance

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>

Driving the document with data

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>

About

data driven documents in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages