1. Luke Plant
  2. pyquery


pyquery / pyquery /

Filename Size Date modified Message
6.2 KB
252 B
2.4 KB
14.9 KB
143 B
4.2 KB
pyquery: a jquery-like library for python

pyquery allows you to make jquery queries on xml documents.
The API is as much as possible the similar to jquery. pyquery uses lxml for fast
xml and html manipulation.

This is not (or at least not yet) a library to produce or interact with
javascript code. I just liked the jquery API and I missed it in python so I
told myself "Hey let's make jquery in python". This is the result.

It can be used for many purposes, one idea that I might try in the future is to
use it for templating with pure http templates that you modify using pyquery.

You can use the PyQuery class to load an xml document from a string, a lxml
document, from a file or from an url::

    >>> from pyquery import PyQuery
    >>> from lxml import etree
    >>> d = PyQuery("<html></html>")
    >>> d = PyQuery(etree.fromstring("<html></html>"))
    >>> d = PyQuery(url='http://google.com/')
    >>> d = PyQuery(filename=path_to_html_file)

Now d is like the $ in jquery::

    >>> d("#hello")
    >>> p = d("#hello")
    >>> p.html()
    'Hello world !'
    >>> p.html("you know <a href='http://python.org/'>Python</a> rocks")
    >>> p.html()
    'you know <a href="http://python.org/">Python</a> rocks'
    >>> p.text()
    'you know Python rocks'

You can play with the attributes with the jquery API::

    >>> p.attr("id")
    >>> p.attr("id", "plop")
    >>> p.attr("id", "hello")

Or in a more pythonic way::

    >>> p.attr.id = "plop"
    >>> p.attr.id
    >>> p.attr["id"] = "ola"
    >>> p.attr["id"]
    >>> p.attr(id='hello', class_='hello2')
    >>> p.attr.class_
    >>> p.attr.class_ = 'hello'

You can also play with css classes::

    >>> p.addClass("toto")
    >>> p.toggleClass("titi toto")
    >>> p.removeClass("titi")

Or the css style::

    >>> p.css("font-size", "15px")
    >>> p.attr("style")
    'font-size: 15px'
    >>> p.css({"font-size": "17px"})
    >>> p.attr("style")
    'font-size: 17px'

Same thing the pythonic way ('_' characters are translated to '-')::

    >>> p.css.font_size = "16px"
    >>> p.attr.style
    'font-size: 16px'
    >>> p.css['font-size'] = "15px"
    >>> p.attr.style
    'font-size: 15px'
    >>> p.css(font_size="16px")
    >>> p.attr.style
    'font-size: 16px'
    >>> p.css = {"font-size": "17px"}
    >>> p.attr.style
    'font-size: 17px'

You can also add content to the end of tags::

    >>> d('p').append('check out <a href="http://reddit.com/r/python"><span>reddit</span></a>')
    [<p#hello.hello>, <p#test>]
    >>> print d
    <p class="hello" id="hello" style="font-size: 17px">you know <a href="http://python.org/">Python</a> rockscheck out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><p id="test">
    hello <a href="http://python.org">python</a> !
    check out <a href="http://python.org/">Python</a> rockscheck out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>

Or to the beginning::

    >>> p.prepend('check out <a href="http://reddit.com/r/python">reddit</a>')
    >>> p.html()
    'check out <a href="http://reddit.com/r/python">reddit</a>you know ...'

Prepend or append an element into an other::

    >>> p.prependTo(d('#test'))
    >>> d('#test').html()
    '<p class="hello" ...</p>...hello...python...'

Insert an element after another::

    >>> p.insertAfter(d('#test'))
    >>> d('#test').html()
    '<a href="http://python.org">python</a> !...'

Or before::

    >>> p.insertBefore(d('#test'))
    >>> d('body').html()
    '\n<p class="hello" id="hello" style="font-size: 17px">...'

Doing something for each elements::

    >>> p.each(lambda e: e.addClass('hello2'))

Remove an element::

    >>> d.remove('p#id')
    >>> d('p#id')

Replace an element by another::

    >>> p.replaceWith('<p>testing</p>')
    >>> d('p')
    [<p>, <p#test>]

Or the other way around::

    >>> d('<h1>arya stark</h1>').replaceAll('p')
    >>> d('p')
    >>> d('h1')
    [<h1>, <h1>]

Remove what's inside the selection::

    >>> d('h1').empty()
    [<h1>, <h1>]

And you can get back the modified html::

    >>> print d

You can generate html stuff::

    >>> from pyquery import PyQuery as pq
    >>> print pq('<div>Yeah !</div>').addClass('myclass') + pq('<b>cool</b>')
    <div class="myclass">Yeah !</div><b>cool</b>

.. fake imports

    >>> from ajax import PyQuery as pq

You can query some wsgi app if WebOb is installed (it's not a pyquery
dependencie). IN this example the test app returns a simple input at `/` and a
submit button at `/submit`::

    >>> d = pq('<form></form>', app=input_app)
    >>> d.append(d.get('/'))
    >>> print d
    <form><input name="youyou" type="text" value=""/></form>

The app is also available in new nodes::

    >>> d.get('/').app is d.app is d('form').app

You can also request another path::

    >>> d.append(d.get('/submit'))
    >>> print d
    <form><input name="youyou" type="text" value=""/><input type="submit" value="OK"/></form>

If you want to run the tests that you can see above you should do::

    $ python bootstrap.py
    $ bin/buildout
    $ bin/test

For more documentation about the API use the jquery website http://docs.jquery.com/

The reference I'm now using for the API is ... the color cheat sheet


- SELECTORS: it works fine but missing all the :xxx (:first, :last, ...) can be
  done by patching lxml.cssselect
- CSS: done
- HTML: done
- MANIPULATING: did all but the "wrap" methods
- TRAVERSING: did a few
- EVENTS: nothing to do with server side might be used later for automatic ajax
- CORE UI EFFECTS: did hide and show the rest doesn't really makes sense on
  server side
- AJAX: some with wsgi app