Middleware for applying AMP Renderer to the output of a request in Django.
Project description
Django AMP Renderer
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53353026ec0aea477c0520d3552dedc188230251f2cb0f0657551b7a06972f9b |
|
MD5 | 14ffa87a8321461c65b322abeae3f5f8 |
|
BLAKE2b-256 | 4008c2066051ad1749477000577e0f0d206ec08a96dbd7a08bbd6baa37b6f9cb |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 660e22cf6ae5f0a5b402a4d8cde6d4268bc980f8141aac12587a20a44f9f82a5 |
|
MD5 | 867b5b07e48aac93132c63ed6de5ffaa |
|
BLAKE2b-256 | 36aaa29a141d7d1a3b17fc06dbf0faf73adbbc854f23b15a58d53f2b49518bad |