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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file django-amp-renderer-2.0.1.tar.gz.

File metadata

  • Download URL: django-amp-renderer-2.0.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for django-amp-renderer-2.0.1.tar.gz
Algorithm Hash digest
SHA256 53353026ec0aea477c0520d3552dedc188230251f2cb0f0657551b7a06972f9b
MD5 14ffa87a8321461c65b322abeae3f5f8
BLAKE2b-256 4008c2066051ad1749477000577e0f0d206ec08a96dbd7a08bbd6baa37b6f9cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_amp_renderer-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for django_amp_renderer-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 660e22cf6ae5f0a5b402a4d8cde6d4268bc980f8141aac12587a20a44f9f82a5
MD5 867b5b07e48aac93132c63ed6de5ffaa
BLAKE2b-256 36aaa29a141d7d1a3b17fc06dbf0faf73adbbc854f23b15a58d53f2b49518bad

See more details on using hashes here.

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