Skip to main content

A Python engine for the Liquid template language.

Project description

A Python implementation of Liquid, the safe, customer-facing template language for flexible web apps.

Version Tests License Python versions PyPy versions
from liquid import Template

template = Template("Hello, {{ you }}!")
print(template.render(you="World"))  # "Hello, World!"
print(template.render(you="Liquid"))  # "Hello, Liquid!"

Installing

Install Python Liquid using Pipenv:

$ pipenv install python-liquid

Or pip:

$ python -m pip install -U python-liquid

Compatibility

We strive to be 100% compatible with the reference implementation of Liquid, written in Ruby. That is, given an equivalent render context, a template rendered with Python Liquid should produce the same output as when rendered with Ruby Liquid.

See the known issues page for details of known incompatibilities between Python Liquid and Ruby Liquid, and please help by raising an issue if you notice an incompatibility.

Benchmark

You can run the benchmark using make benchmark (or python -O performance.py if you don’t have make) from the root of the source tree. On my ropey desktop computer with a Ryzen 5 1500X, we get the following results.

Best of 5 rounds with 100 iterations per round and 60 ops per iteration (6000 ops per round).

lex template (not expressions): 1.3s (4727.35 ops/s, 78.79 i/s)
                 lex and parse: 6.4s (942.15 ops/s, 15.70 i/s)
                        render: 1.7s (3443.62 ops/s, 57.39 i/s)
         lex, parse and render: 8.2s (733.30 ops/s, 12.22 i/s)

And PyPy3.7 gives us a decent increase in performance.

Best of 5 rounds with 100 iterations per round and 60 ops per iteration (6000 ops per round).

lex template (not expressions): 0.58s (10421.14 ops/s, 173.69 i/s)
                 lex and parse: 2.9s (2036.33 ops/s, 33.94 i/s)
                        render: 1.1s (5644.80 ops/s, 94.08 i/s)
         lex, parse and render: 4.2s (1439.43 ops/s, 23.99 i/s)

On the same machine, running rake benchmark:run from the root of the reference implementation source tree gives us these results.

/usr/bin/ruby ./performance/benchmark.rb lax

Running benchmark for 10 seconds (with 5 seconds warmup).

Warming up --------------------------------------
             parse:     3.000  i/100ms
            render:     8.000  i/100ms
    parse & render:     2.000  i/100ms
Calculating -------------------------------------
             parse:     39.072  (± 0.0%) i/s -    393.000  in  10.058789s
            render:     86.995  (± 1.1%) i/s -    872.000  in  10.024951s
    parse & render:     26.139  (± 0.0%) i/s -    262.000  in  10.023365s

I’ve tried to match the benchmark workload to that of the reference implementation, so that we might compare results directly. The workload is meant to be representative of Shopify’s use case, although I wouldn’t be surprised if their usage has changed subtly since the benchmark fixture was designed.

Contributing

  • Install development dependencies with Pipenv

  • Python Liquid uses type hints and static type checking. Run mypy or tox -e typing to check for typing issues.

  • Format code using black.

  • Write tests using unittest.TestCase.

  • Run tests with make test or python -m unittest or pytest.

  • Check test coverage with make coverage and open htmlcov/index.html in your browser.

  • Check your changes have not adversely affected performance with make benchmark.

Docs

Documentation is built using Docusaurus. Find the source in the docs branch of this repository.

The docs folder in the root of this repository contains older, sphinx-based docs, still hosted on Read the Docs.

The plan is to generate API Documentation for Docusaurus from Python doc comments. The older docs will be kept until such time that better Docusaurus/sphinx integration is available or a workaround is found.

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

python-liquid-1.4.0.tar.gz (217.6 kB view details)

Uploaded Source

Built Distribution

python_liquid-1.4.0-py3-none-any.whl (158.5 kB view details)

Uploaded Python 3

File details

Details for the file python-liquid-1.4.0.tar.gz.

File metadata

  • Download URL: python-liquid-1.4.0.tar.gz
  • Upload date:
  • Size: 217.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for python-liquid-1.4.0.tar.gz
Algorithm Hash digest
SHA256 6dde35f43bd6346f6559d17ffca133af2a6a5b65d8a046c1833ae72ad992bc4b
MD5 4d01cdb03688333de717c07b023023c0
BLAKE2b-256 c5bf718ec11893411fde0ec7968d83cd920aad3b84c134ddbf68378af15449ca

See more details on using hashes here.

File details

Details for the file python_liquid-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_liquid-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10d7eed6c255c858858676c5d66c8da64d536ff87780aaf048bd727e5950b4a0
MD5 1d02bb5fe35ed389296f7adcbb6ea67c
BLAKE2b-256 960f6fb6a22a07b0f8817393fd6a8c489c4049f7cf6fb32de99205bc5ec01b9a

See more details on using hashes here.

Supported by

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