Web frameworks
chameleon-rs works with the Python web frameworks you’re likely to use, with nothing extra to install. Each integration is a module of chameleon-rs that imports its framework only when you import it, and uses the version your app already has.
| Framework | Use | It replaces | Guide |
|---|---|---|---|
| Flask, Quart | chameleon_rs.flask |
chameleon-flask | Flask and Quart |
| FastAPI | chameleon_rs.fastapi |
fastapi-chameleon | FastAPI |
| Starlette | chameleon_rs.starlette |
Starlette’s Jinja2Templates |
Starlette |
| Robyn | chameleon_rs.robyn |
chameleon-robyn | Robyn |
| Litestar | chameleon_rs.litestar |
Litestar’s Jinja engine, litestar-chameleon | Litestar |
| Pyramid | pyramid_chameleon, unchanged | (runs through compat.install()) | Pyramid |
Already using one of the libraries in the third column? chameleon_rs.compat.install() makes its import give the built-in, so the app runs unchanged. See Switching from Chameleon.
The same page in each framework
Each example renders templates/home/index.pt:
<h1>${title}</h1>
<ul><li tal:repeat="v videos">${v}</li></ul>import flask
import chameleon_rs.flask as chameleon_flask
app = flask.Flask(__name__)
chameleon_flask.configure(template_folder='templates', app=app)
@app.get('/')
@chameleon_flask.template('home/index.pt')
def index():
return {'title': 'Latest videos', 'videos': ['intro', 'partials']}import quart
import chameleon_rs.flask as chameleon_flask
app = quart.Quart(__name__)
chameleon_flask.configure(template_folder='templates', app=app)
@app.get('/')
@chameleon_flask.template('home/index.pt')
async def index():
return {'title': 'Latest videos', 'videos': await load_videos()}import fastapi
import chameleon_rs.fastapi as fastapi_chameleon
app = fastapi.FastAPI()
fastapi_chameleon.configure(template_folder='templates', app=app)
@app.get('/')
@fastapi_chameleon.template('home/index.pt')
async def index():
return {'title': 'Latest videos', 'videos': await load_videos()}from starlette.applications import Starlette
from starlette.routing import Route
from chameleon_rs.starlette import Templates
templates = Templates(directory='templates')
async def index(request):
model = {'title': 'Latest videos', 'videos': await load_videos()}
return await templates.TemplateResponse_async(request, 'home/index.pt', model)
app = Starlette(routes=[Route('/', index)])from robyn import Robyn
import chameleon_rs.robyn as chameleon_robyn
app = Robyn(__file__)
chameleon_robyn.configure(template_folder='templates', app=app)
@app.get('/')
@chameleon_robyn.template('home/index.pt')
async def index():
return {'title': 'Latest videos', 'videos': await load_videos()}from litestar import Litestar, get
import chameleon_rs.litestar as chameleon_litestar
@get('/')
@chameleon_litestar.template('home/index.pt')
async def index() -> dict:
return {'title': 'Latest videos', 'videos': await load_videos()}
app = Litestar([index])
chameleon_litestar.configure(template_folder='templates', app=app)import chameleon_rs.compat
chameleon_rs.compat.install() # first, before pyramid_chameleon is imported
from pyramid.config import Configurator
from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/home/index.pt')
def index(request):
return {'title': 'Latest videos', 'videos': ['intro', 'partials']}
def main(global_config, **settings):
with Configurator(settings=settings) as config:
config.include('pyramid_chameleon')
config.add_route('home', '/')
config.scan()
return config.make_wsgi_app()