Skip to content

About

Chameleon templates, modernized. A Rust engine that runs your Chameleon templates unchanged and several times faster, with async rendering, partials, Markdown and built-in Flask, Quart, FastAPI, Starlette, Robyn and Litestar integrations.

Topics

Resources

Stars

20 stars

Watchers

0 watching

Forks

Latest commit

 

History

228 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chameleon-rs: Chameleon templates, Rust speed

chameleon-rs

Chameleon templates, modernized. Several times faster.

chameleon-rs is Chameleon's template language on a new engine written in Rust. Your existing templates and code run unchanged, with byte-identical output, several times faster. It also adds what a modern Python web app expects: async rendering that never blocks the event loop on I/O, built-in integrations for Flask/Quart, FastAPI, Starlette, Robyn and Litestar, partials, Markdown, and support for free-threaded Python. No runtime dependencies.

Documentation: https://mkennedy.codes/docs/chameleon-rs/ · Source: https://github.com/mikeckennedy/chameleon-rs · Changelog · Compatibility · Performance

Install

pip install chameleon-rs        # or: uv pip install chameleon-rs

CPython 3.12 to 3.15, including the free-threaded builds (3.14t, 3.15t). Wheels for Linux (glibc, x86_64 and aarch64), macOS (Apple silicon) and Windows (x64) contain the compiled engine, so no Rust toolchain is needed there. Anywhere else (Alpine and other musl systems, Intel Macs, Windows on Arm) pip builds from the source distribution, which needs Rust. Stock Chameleon need not be installed.

Already on Chameleon?

Switching an application is one line at startup, before anything imports Chameleon:

import chameleon_rs.compat

chameleon_rs.compat.install()

From then on import chameleon gives chameleon-rs, and so do chameleon_partials, chameleon_flask, fastapi_chameleon and chameleon_robyn. Your templates and the code around them stay as they are.

  • Same output. Chameleon's own test suite, vendored unmodified, passes: 238 of the 238 tests that pass on Chameleon. All 122 of its fixture renders are byte-identical, and every page of two production Flask/Quart sites renders byte-identical HTML. Known differences.
  • Check your own app first. Shadow mode (chameleon_rs.shadow.install()) renders a sample of real traffic with both engines and reports any difference, while Chameleon keeps serving. Switching from Chameleon.

Starting fresh?

Templates are Chameleon's: HTML with tal: attributes and ${...} expressions, so a template is still valid HTML that an editor or browser can open.

from chameleon_rs import PageTemplate

page = PageTemplate('<p tal:repeat="name names">Hello, ${name}!</p>')
print(page(names=['World', 'Rust']))

In a web app, point chameleon-rs at a template folder and decorate your views. With FastAPI:

import fastapi
import chameleon_rs.fastapi as chameleon

app = fastapi.FastAPI()
chameleon.configure(template_folder='templates', app=app)

@app.get('/episodes/{number}')              # the route decorator first...
@chameleon.template('episodes/detail.pt')   # ...then @template
async def detail(number: int):
    episode = await db.episode(number)
    if episode is None:
        chameleon.not_found()
    return {'episode': episode}

The view keeps its signature, so path and query parameters, Depends, validation and the OpenAPI docs work as without @template. Getting started.

What's new compared with Chameleon

  • Errors where the mistake is. Forget the </div> after a tal:condition and Chameleon quietly renders what the condition should have hidden. Forget the </ul> after a tal:repeat and you get a NameError lines away from the real cause. With strict_tags=True (or CHAMELEON_STRICT_TAGS=1) an unclosed element carrying tal:, metal: or i18n: attributes is a ParseError that points at that tag and names the missing end tag. Opt-in, so the default stays Chameleon's. Catching unclosed tags.
  • Async rendering. await template.render_async(**model) awaits awaitable model values where the template uses them, without blocking the event loop. Macros, slots, partials and tal:on-error work across the wait, and with no awaitables the output is exactly render()'s. Opt-in concurrency: @chameleon_rs.concurrent helpers and concurrent=True partials run at the same time as the rest of the page. Async rendering.
  • Framework integrations. Built in, with nothing extra to install (table below). Pyramid's pyramid_chameleon and Zope/Plone's z3c.pt and Products.PageTemplates run unchanged through compat.install(). Async views render with render_async, and a @template written above its route decorator raises an error naming the fix instead of quietly returning JSON.
  • Partials. Any template can call ${render_partial('shared/card.pt', item=item)}; the partial renders natively inside the page, 10.9x faster than chameleon_partials. Partials.
  • HTMX. Because partials are built in, HTMX works natively: the page includes a fragment with render_partial, and the HTMX view returns the same template on its own. HTMX.
  • Markdown. ${markdown: episode.notes} converts Markdown to HTML while the page renders: CommonMark plus the GitHub extensions (tables, task lists, footnotes, ...), converted in Rust, with raw HTML escaped by default. It's about 26x faster than mistune, a pure-Python converter, on a 5 KB document, and repeat conversions come from a cache in well under a microsecond. Opt-in with chameleon_rs.configure(markdown=True); named profiles (md-comment:) give different settings to different content. Markdown.
  • One place for settings. chameleon_rs.configure(...) sets the template folder, auto_reload, partial lookup and Markdown for the whole process, thread-safely. Configuration.
  • Free-threaded Python. Wheels for 3.14t and 3.15t that keep the GIL off. render_async(..., offload=True) (experimental) renders in a worker thread, in parallel with the event loop on a free-threaded build.
  • Long-standing Chameleon issues fixed. pathlib.Path works wherever a path string does (#342); Zope's repeat.item.first('category') and last('category') group a sorted sequence (#144); <input disabled tal:attributes="disabled True"> renders disabled="disabled", not disableddisabled (#429); repeat.item stays right in a recursive macro (#177); a macro call no longer resets a local that shadows a global (#445); and errors inside metal:fill-slot point at the failing expression in the fill, not the page's metal:use-macro (#447, #291, #259).
  • Typed. PEP 561 type information for the whole API, including the native module.
  • No runtime dependencies. The engine, the Markdown converter and the integrations are in the wheel; each integration imports its framework only when you import it.

Frameworks

Framework Module Replaces Docs
Flask, Quart chameleon_rs.flask chameleon-flask Flask and Quart
FastAPI chameleon_rs.fastapi fastapi-chameleon FastAPI
Starlette chameleon_rs.starlette Starlette's Jinja2Templates (Templates is a drop-in) Starlette
Robyn chameleon_rs.robyn chameleon-robyn Robyn
Litestar chameleon_rs.litestar Litestar's TemplateConfig engine (ChameleonEngine); litestar-chameleon Litestar
Pyramid pyramid_chameleon, unchanged, through compat.install() — Pyramid

Web frameworks shows the same page in each one. Each module keeps the API of the library it replaces and adds the same decorator API (configure, @template, render, response, not_found, error, render_async, response_async, check_views).

Template names come from your code. The decorator modules and render_partial() look names up as the original libraries do (.. and absolute paths work), so never build one from request data. Starlette's Templates and Litestar's ChameleonEngine refuse names that reach outside their folder, as Jinja does. configure(strict_names=True) (or CHAMELEON_STRICT_NAMES=1) makes the decorator modules refuse them too, as "not found"; off by default, as in the original libraries. Either way, every spelling of one file (./a.pt, a//a.pt, an absolute path) shares one cached template.

Performance

chameleon-rs speedup over Chameleon: native partials 10.9x, macro calls 6.9x, listings 6.6x, app-shaped page 4.3x, template compilation about 70x; on a production Quart site, template rendering 2.2x, throughput 1.34x, whole requests 1.3x

In synthetic benchmarks (release build, same process, interleaved with Chameleon): listings 6.6x faster per item, macro calls 6.9x, an app-shaped page 4.3x, native partials 10.9x, and templates compile about 70x faster. On a production Quart site, template rendering was 2.2x faster in total and whole requests 1.3x faster; under load, throughput rose 34%. The released wheels are profile-guided, about 10% faster again. How it's measured.

Development

make dev     # set up venv with the reference Chameleon and build the extension
make test    # Rust tests, Python tests, Chameleon's own suite on both engines

Licensed under Chameleon's license (see LICENSE.txt and COPYRIGHT.txt); this project ports Chameleon's behavior and reuses its test suite.

About

Chameleon templates, modernized. A Rust engine that runs your Chameleon templates unchanged and several times faster, with async rendering, partials, Markdown and built-in Flask, Quart, FastAPI, Starlette, Robyn and Litestar integrations.

Topics

Resources

Stars

20 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages