AMP Renderer
Unofficial Python port of server-side rendering from AMP Optimizer. Tested in Python 3.10 through 3.14.
AMP Renderer performs the following optimizations:
- Inject the specific layout markup into each AMP element
- Insert the AMP runtime styles into the document
- Remove the AMP boilerplate styles, if possible
- Mark the document as “transformed” with the appropriate tags on the
htmlelement - Insert
imgtags for images with the data-hero attribute
It also makes these formatting updates:
- Remove empty
classandstyletags for AMP HTML elements - Convert tag names and attribute names to lowercase
- Convert numerical attribute values to strings
- Use double quotes ("") for attributes, and escape double quotes inside attribute values
- Remove whitespace between html attributes
- If desired, removes comments (disabled by default)
- If desired, trims whitespace around HTML attribute values (disabled by default, and not always a good idea)
AMPRenderer can be used on a block of arbitrary HTML, but when used on a full document, it will insert the AMP runtime styles and, if possible, remove the AMP boilerplate styles.
Boilerplate styles can be removed except in these cases:
- An AMP element uses an unsupported value for the
layoutattribute amp-audiois used- There is at least one
amp-experimentactive - Transformation fails for one or more elements due to an invalid attribute value for
media,sizes, orheights - Any render-delaying extension is used. Currently this means:
amp-dynamic-css-classesamp-story
If boilerplate styles can’t be removed, the attribute no_boilerplate will be set to True after calling render; otherwise it will be False. Until render runs, the no_boilerplate attribute isn’t set at all.
Usage
If using Django, you can use the Django AMP Renderer middleware.
Otherwise, install via PyPI:
pip install amp-renderer
Minimal usage:
from amp_renderer import AMPRenderer
...
RUNTIME_VERSION = "012345678" /* Current AMP runtime version number */
RUNTIME_STYLES = "..." /* Current contents of https://cdn.ampproject.org/v0.css */
renderer = AMPRenderer(
runtime_version=RUNTIME_VERSION,
runtime_styles=RUNTIME_STYLES)
original_html = """
<!doctype html>
<html ⚡>
...
</html>
"""
result = renderer.render(original_html)
print(result)
Remove comments and/or trim attributes:
renderer.strip_comments = True
renderer.trim_attributes = True
result = renderer.render(original_html)
print(result)
The AMPRenderer class inherits from HTMLParser, and can be similarly extended.
Testing, etc.
Install development requirements (Requires Python >= 3.10):
make install
Format:
make format
Lint:
make lint
Test:
make test
Discussion
There are still some aspects of the official AMP Optimizer implementation that haven’t been addressed yet. PRs welcome.
General
-
Tested against AMP Optimizer’s ServerSideRendering spec - Automatic runtime version management
Dynamic attributes
-
Supportsizes,media, andheightsvia CSS injection -
Group CSS injections formediaattributes by shared media queries to reduce necessary bytes - Support percent values in
heights - Warn or fail if CSS injection puts the
amp-customelement over the byte limit
Hero Images
-
Injectimgtag foramp-imgs with thedata-heroattribute - Enforce 2-image limit on
data-hero - Autodetect hero images
- Support hero image functionality for
amp-iframe,amp-video, andamp-video-iframe
Performance
The Python AMP Renderer does not insert preload links into the head of the DOM object for hero images; This can be done by hand for more control over the critical path.
Metadata
Release files for amp-renderer 2.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| amp_renderer-2.2.0.tar.gz | 23.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| amp_renderer-2.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 46.7 kB
Release files / amp_renderer-2.2.0.tar.gz
| Download URL | amp_renderer-2.2.0.tar.gz |
|---|---|
| Size | 23.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
487c0bd23d737160ff18df01b4f6c8695a9be9abb3f5a3218692cd53461221cc
|
|
BLAKE2b-256 checksum How to use checksums |
4a700e64b1e72dccfa7894cf01af2d16aece4507f722d0e6ef44304e5e3d4474
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / amp_renderer-2.2.0-py3-none-any.whl
| Download URL | amp_renderer-2.2.0-py3-none-any.whl |
|---|---|
| Size | 23.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20bbd9b9ec66895e691748c5ec6098f63a6e78d134158960388301786974ca47
|
|
BLAKE2b-256 checksum How to use checksums |
6f84807c48d39ee2f7241aba9544ad63c7afce91ec1ec4d2bb2ff7a2bc133629
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for amp-renderer 2.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.