Skip to main content

Unofficial Python port of server-side rendering from AMP Optimizer

Project description

AMP Renderer

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

Unofficial Python port of server-side rendering from AMP Optimizer. Tested in Python 3.10 through 3.14.

AMP Renderer performs the following optimizations:

  1. Inject the specific layout markup into each AMP element
  2. Insert the AMP runtime styles into the document
  3. Remove the AMP boilerplate styles, if possible
  4. Mark the document as “transformed” with the appropriate tags on the html element
  5. Insert img tags for images with the data-hero attribute

It also makes these formatting updates:

  1. Remove empty class and style tags for AMP HTML elements
  2. Convert tag names and attribute names to lowercase
  3. Convert numerical attribute values to strings
  4. Use double quotes ("") for attributes, and escape double quotes inside attribute values
  5. Remove whitespace between html attributes
  6. If desired, removes comments (disabled by default)
  7. 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 layout attribute
  • amp-audio is used
  • There is at least one amp-experiment active
  • Transformation fails for one or more elements due to an invalid attribute value for media, sizes, or heights
  • Any render-delaying extension is used. Currently this means:
    • amp-dynamic-css-classes
    • amp-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

  • Support sizes, media, and heights via CSS injection
  • Group CSS injections for media attributes by shared media queries to reduce necessary bytes
  • Support percent values in heights
  • Warn or fail if CSS injection puts the amp-custom element over the byte limit

Hero Images

  • Inject img tag for amp-imgs with the data-hero attribute
  • Enforce 2-image limit on data-hero
  • Autodetect hero images
  • Support hero image functionality for amp-iframe, amp-video, and amp-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.

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

amp_renderer-2.2.0.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

amp_renderer-2.2.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for amp_renderer-2.2.0.tar.gz
Algorithm Hash digest
SHA256 487c0bd23d737160ff18df01b4f6c8695a9be9abb3f5a3218692cd53461221cc
MD5 fab88066c810a1caa4b9af1b945e7e31
BLAKE2b-256 4a700e64b1e72dccfa7894cf01af2d16aece4507f722d0e6ef44304e5e3d4474

See more details on using hashes here.

File details

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

File metadata

  • Download URL: amp_renderer-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for amp_renderer-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20bbd9b9ec66895e691748c5ec6098f63a6e78d134158960388301786974ca47
MD5 29000baff11c74806108b7324502a596
BLAKE2b-256 6f84807c48d39ee2f7241aba9544ad63c7afce91ec1ec4d2bb2ff7a2bc133629

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