Skip to main content

Adds integration of the Chameleon template language to FastAPI.

Project description

fastapi-chameleon

Adds integration of the Chameleon template language to FastAPI. If you are interested in Jinja instead, see the sister project: github.com/AGeekInside/fastapi-jinja.

Installation

Simply pip install fastapi_chameleon.

Usage

This is easy to use. Just create a folder within your web app to hold the templates such as:

├── main.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 fastapi_chameleon

dev_mode = True

BASE_DIR = Path(__file__).resolve().parent
template_folder = str(BASE_DIR / 'templates')
fastapi_chameleon.global_init(template_folder, auto_reload=dev_mode)

Then just decorate the FastAPI view methods (works on sync and async methods):

@router.post('/')
@fastapi_chameleon.template('home/index.pt')
async def home_post(request: Request):
    form = await request.form()
    vm = PersonViewModel(**form) 

    return vm.dict() # {'first':'Michael', 'last':'Kennedy', ...}

The view method should return a dict to be passed as variables/values to the template.

If a fastapi.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.

Friendly 404s and errors

A common technique for user-friendly sites is to use a custom HTML page for 404 responses. This is especially important in FastAPI because FastAPI returns a 404 response + JSON by default. This library has support for friendly 404 pages using the fastapi_chameleon.not_found() function.

Here's an example:

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

If you need to return errors other than Not Found (status code 404), you can use a more generic function: fastapi_chameleon.generic_error(template_file: str, status_code: int). This function will allow you to return different status codes. It's generic, thus you'll have to pass a path to your error template file as well as a status code you want the user to get in response. For example:

@router.get('/catalog/item/{item_id}')
@fastapi_chameleon.template('catalog/item.pt')
async def item(item_id: int):
    item = service.get_item_by_id(item_id)
    if not item:
        fastapi_chameleon.generic_error('errors/unauthorized.pt',
                                        fastapi.status.HTTP_401_UNAUTHORIZED)

    return item.dict()

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

fastapi_chameleon-0.1.14.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

fastapi_chameleon-0.1.14-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_chameleon-0.1.14.tar.gz.

File metadata

  • Download URL: fastapi_chameleon-0.1.14.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for fastapi_chameleon-0.1.14.tar.gz
Algorithm Hash digest
SHA256 154a8f60c496aab0d38dbabbe69c425dbe5c1de73cd49ac6989397ad1ecfce7f
MD5 22a53eee65b4ad0a111c5365fa41076f
BLAKE2b-256 f4d399e3c64d3b0b8d0449fca084edff6ea1813f7d79726f0caffe17d64f0816

See more details on using hashes here.

File details

Details for the file fastapi_chameleon-0.1.14-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_chameleon-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 ba09607d9c08b0956c6bc1b529e61e8a9abef1be8ed00a73b0ba061e63e0dd52
MD5 46a9b7ed44c869dd9cb0ebfead23fa75
BLAKE2b-256 6628d1622d1383c912ab1ca1d394140e6f77d8778496318c4059d24a88ffbd48

See more details on using hashes here.

Supported by

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