I’m a big fan of the Chameleon templating language for Python web apps. For a long time it was the default HTML flavor for Pyramid. But I’m kicking offer new project based on Flask. Flask has really seen a resurgence lately so it seems like the perfect choice for what I’m working on.
One drawback, Flask only supports Jinja2. Jinja is fine, like most templating languages, it is full of Python syntax and begin/end blocks. It’s less pure web compared to Chameleon.
So I fixed it
I created a new open source package for making Chameleon feel very native in Flask:
@app.get('/async')
@chameleon_flask.template('async.pt')
async def async_world():
await asyncio.sleep(.01)
return {'message': "Let's go async Chameleon!"}
Notice the second decorator: @chameleon_flask.template('async.pt')
. That’s the magic. Return a dictionary and it’ll render the Chameleon template at /templates/async.pt
.
If you want to use Chameleon in Flask, check out chameleon-flask on GitHub. And if you’re using FastAPI, I created a sister project called fastapi-chameleon.
Two caveats
First, why didn’t I just use a few of the existing Flask + Chameleon libraries? They seem like they’d work. But they were either very minimal or didn’t support both sync and async view methods.
Secondly, many Flask extensions assume they are working with Jinja as their template system. So this may or may not work with certain extensions. I generally don’t use extensions so it’s fine for me. Just figure out if this is fine for you.