Skip to main content

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.

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:

import os
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.

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/{item_id}')
@chameleon_flask.template('catalog/item.pt')
async def item(item_id: int):
    item = service.get_item_by_id(item_id)
    if not item:
        chameleon_flask.not_found()
    
    return item.dict()

This will render a 404 response with using the template file templates/errors/404.pt. You can specify another template to use for the response, but it's not required.

An example

See the example/example_app.py file 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

chameleon_flask-0.6.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chameleon_flask-0.6.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file chameleon_flask-0.6.0.tar.gz.

File metadata

  • Download URL: chameleon_flask-0.6.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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

Hashes for chameleon_flask-0.6.0.tar.gz
Algorithm Hash digest
SHA256 252df3e36277c8d0398344af3f497163f05728de1e43aec6468d515d9ca32c95
MD5 f33930f5d527327603a04cbf89793643
BLAKE2b-256 8890f13c658d003dccc847bcacf548e293ad54047a36fd5be1685e9894bad6a7

See more details on using hashes here.

File details

Details for the file chameleon_flask-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: chameleon_flask-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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

Hashes for chameleon_flask-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6c86e5f130f616528c3320b0fccd78979af7ceabd5a7a74e4c224899fe4d2a
MD5 db92ec8bccf681a5b1c981cfed8babe0
BLAKE2b-256 384d107703d4438afc85a4a5bf2986ed25ca196980dc3085acaeda31b7d7fa69

See more details on using hashes here.

Supported by

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