Skip to main content

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

Project description

Django AMP Renderer

Python 3.10 | 3.11 | 3.12 | 3.13 | 3.14 Build Coverage

Middleware for applying AMP Renderer to the output of a request in Django. Tested in Python 3.8 and above, but works on Python 3.6+.

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):
    strip_comments = True
    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 (Requires Python >= 3.8):

make install

Sort imports:

make format

Lint:

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.2.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_amp_renderer-2.2.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file django_amp_renderer-2.2.0.tar.gz.

File metadata

  • Download URL: django_amp_renderer-2.2.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_amp_renderer-2.2.0.tar.gz
Algorithm Hash digest
SHA256 87c1316cec576abfc85ebde960ac39ce6b9e85e0189a4fa0d7adb05b31fe7d7b
MD5 f30f7437893e5ff73cd83630efffc003
BLAKE2b-256 c16585b9962a21cd492bf757e72ee89c5fad51f0460f6120858cd74fb94d29be

See more details on using hashes here.

File details

Details for the file django_amp_renderer-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_amp_renderer-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 041ece5818f74a1a10d0195c93026915c461eb0d965cd6e6230ca5df15b91e46
MD5 db8c804420045541825bc282ec6099bf
BLAKE2b-256 3ae2bfc283d69ffe7a8a02ed9c88d0693b1c9efc601465719afecdcbe726a37c

See more details on using hashes here.

Supported by

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