Skip to main content

Middleware for applying AMP Renderer to the output of a request in Django.

Project description

Django AMP Renderer

Python 3.6+ Django 2.2+ Build Status Coverage

Middleware for applying AMP Renderer to the output of a request in Django.

Usage

Install via PyPI:

pip install django-amp-renderer

To apply the middleware, add django_amp_renderer.middleware.AMPRenderingMiddleware to MIDDLEWARE in your Django settings file:

MIDDLEWARE = [
    'django_amp_renderer.middleware.AMPRenderingMiddleware',
    …
]

The middleware expects the variables AMP_RUNTIME_VERSION and AMP_RUNTIME_STYLES to be set in your Django settings file.

For AMP_RUNTIME_VERSION, provide the current AMP runtime version number as a string (to avoid losing leading zeroes). For AMP_RUNTIME_STYLES, provide the full contents of https://cdn.ampproject.org/v0.css.

AMP_RUNTIME_VERSION = '012007242032002'
AMP_RUNTIME_STYLES = """
    html{overflow-x:hidden!important}…
""".strip()

AMPRenderer has optional comment removal and attribute trimming. Those are disabled by default; To access them, use a subclass of the middleware and set the variables to True. They are passed along to the renderer.

class TransformingMiddleware(AMPRenderingMiddleware):
    should_strip_comments = True
    should_trim_attributes = True

You can apply the middleware to all requests, even non-AMP pages. The transformations will only be applied if the document contains the v0.js script (https://cdn.ampproject.org/v0.js).

If the transformation is applied, the Boilerplate-Status header of the response will either be set to "Removed" or "Ignored", based on whether the boilerplate was able to be removed by the renderer.

Testing, etc.

Install development requirements:

make install

Sort imports (Requires Python >= 3.6):

make format

Lint (Requires Python >= 3.6):

make lint

Test:

make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-amp-renderer-2.0.1.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

django_amp_renderer-2.0.1-py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page