Adds integration of the Chameleon template language to Flask and Quart.
Project description
chameleon-flask
Adds integration of the Chameleon template language to Flask and Quart.
📖 Documentation: mkennedy.codes/docs/chameleon-flask
Installation
Simply pip install chameleon_flask.
Usage
This is easy to use. Just create a folder within your web app to hold the templates such as:
├── app.py
├── views.py
│
├── templates
│ ├── home
│ │ └── index.pt
│ └── shared
│ └── layout.pt
In the app startup, tell the library about the folder you wish to use:
from pathlib import Path
import chameleon_flask
dev_mode = True
BASE_DIR = Path(__file__).resolve().parent
template_folder = str(BASE_DIR / 'templates')
chameleon_flask.global_init(template_folder, auto_reload=dev_mode)
Then just decorate the Flask or Quart view methods (works on sync and async methods):
@app.get('/async')
@chameleon_flask.template('async.pt')
async def async_world():
await asyncio.sleep(.01)
return {'message': "Let's go async Chameleon!"}
The view method should return a dict to be passed as variables/values to the template.
If a flask.Response is returned, the template is skipped and the response along with status_code and
other values is directly passed through. This is common for redirects and error responses not meant
for this page template. Otherwise the dictionary is used to render async.pt in this example.
Everything works the same on a Quart app — decorate your async Quart views and return a
dict or a quart.Response.
You can also control the response's content type and default status code right on the decorator:
@app.get('/xml')
@chameleon_flask.template('sample.xml', content_type='application/xml', status_code=201)
def xml_response():
return {'items': ['pyramid', 'flask', 'fastapi']}
Default template names
Use the decorator bare and the template file is derived from the view: a function index
in views.py maps to {template_folder}/views/index.html, falling back to
views/index.pt if no .html file exists. The name is resolved when the view is
decorated, so call global_init() before defining views when you rely on this form.
@app.get('/')
@chameleon_flask.template
def index():
return {'message': 'Hello world'} # renders templates/views/index.pt
Friendly 404s and errors
A common technique for user-friendly sites is to use a custom HTML page for 404 responses.
This library has support for friendly 404 pages using the chameleon_flask.not_found() function.
Here's an example:
@app.get('/catalog/item/<int:item_id>')
@chameleon_flask.template('catalog/item.pt')
def item(item_id: int):
item = service.get_item_by_id(item_id)
if not item:
chameleon_flask.not_found()
return {'item': item}
This will render a 404 response using the template file templates/errors/404.pt.
You can specify another template to use for the response, but it's not required.
Alpine.js, Vue, and friends
By default, Chameleon rejects attributes outside its own TAL/METAL/i18n namespaces, which
breaks the shorthand syntax used by attribute-based JavaScript frameworks (@click, :class,
x-data, and so on). Pass restricted_namespace=False to allow them through unchanged:
chameleon_flask.global_init(
template_folder,
auto_reload=dev_mode,
restricted_namespace=False, # Enable Alpine.js / Vue-style attributes
)
Rendering without the decorator
For error handlers and other spots where the decorator doesn't fit, render directly:
import chameleon_flask
@app.errorhandler(500)
def server_error(e):
return chameleon_flask.response('errors/500.pt', status_code=500)
# Or get the raw HTML as a string:
html = chameleon_flask.engine.render('home/index.pt', message='Hi')
See the full API reference for every function, parameter, and exception.
An example
See example/example_app.py for a working example to play with.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chameleon_flask-0.6.1.tar.gz.
File metadata
- Download URL: chameleon_flask-0.6.1.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.20 {"installer":{"name":"uv","version":"0.11.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
262f4aae13e133f40d9d83452219442255c28847647959be1f849f70d70909bf
|
|
| MD5 |
496762b2c8944f81bea24b4bf1f59d6d
|
|
| BLAKE2b-256 |
db6236b72f45ae7453ddc43a4910d7cce4c0b89635fe403689149327cbd7c8c1
|
File details
Details for the file chameleon_flask-0.6.1-py3-none-any.whl.
File metadata
- Download URL: chameleon_flask-0.6.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.20 {"installer":{"name":"uv","version":"0.11.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f414bb4d7c763840509984eef2962eb1bbbc21d75a3da500139715900e96fa48
|
|
| MD5 |
27c256ccea905f743567b920640e7a35
|
|
| BLAKE2b-256 |
e59ebf7267c47a75dc7d76273c20155de782e8797341c0cbd5694cb3a6e06484
|