Skip to content

Instantly share code, notes, and snippets.

@chadwhitacre
Last active December 20, 2015 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadwhitacre/6059536 to your computer and use it in GitHub Desktop.
Save chadwhitacre/6059536 to your computer and use it in GitHub Desktop.
"""This is an Aspen hook for supporting PJAX.
Install it in your project's configure-aspen.py like so:
website.hooks.outbound += [aspen_pjax.outbound]
"""
from pyquery import PyQuery as pq # http://pythonhosted.org/pyquery/
def outbound(response):
"""Do PJAX by pulling an element out of the rendered page.
"""
if not response.headers.get('Content-Type', '').startswith('text/html'):
return
if response.request.headers.get('X-PJAX') is None:
return
if response.request.headers.get('X-PJAX-CONTAINER') is None:
return
selector = response.request.headers['X-PJAX-CONTAINER']
response.body = pq(response.body)(selector).html().encode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment