Giter VIP home page Giter VIP logo

p3's Introduction

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>

p3's People

Contributors

sammyt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

p3's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.