# 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:
```python
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):
```python
@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](http://www.instantshift.com/2019/10/16/user-friendly-404-pages/).
This library has support for friendly 404 pages using the `chameleon_flask.not_found()` function.
Here's an example:
```python
@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.
Raw data
{
"_id": null,
"home_page": null,
"name": "chameleon_flask",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "Flask, Chameleon, template, integration",
"author": null,
"author_email": "Michael Kennedy <michael@talkpython.fm>",
"download_url": "https://files.pythonhosted.org/packages/ed/86/e3086f6e2d905aa2a4f6d756525819e7af87c3b307e777abbfd09ea4d836/chameleon_flask-0.0.5.tar.gz",
"platform": null,
"description": "# chameleon-flask\n\nAdds integration of the Chameleon template language to Flask and Quart. \n\n## Installation\n\nSimply `pip install chameleon_flask`.\n\n## Usage\n\nThis is easy to use. Just create a folder within your web app to hold the templates such as:\n\n```\n\u251c\u2500\u2500 app.py\n\u251c\u2500\u2500 views.py\n\u2502\n\u251c\u2500\u2500 templates\n\u2502 \u251c\u2500\u2500 home\n\u2502 \u2502 \u2514\u2500\u2500 index.pt\n\u2502 \u2514\u2500\u2500 shared\n\u2502 \u2514\u2500\u2500 layout.pt\n\n```\n\nIn the app startup, tell the library about the folder you wish to use:\n\n```python\nimport os\nfrom pathlib import Path\nimport chameleon_flask\n\ndev_mode = True\n\nBASE_DIR = Path(__file__).resolve().parent\ntemplate_folder = str(BASE_DIR / 'templates')\nchameleon_flask.global_init(template_folder, auto_reload=dev_mode)\n```\n\nThen just decorate the Flask or Quart view methods (works on sync and async methods):\n\n```python\n@app.get('/async')\n@chameleon_flask.template('async.pt')\nasync def async_world():\n await asyncio.sleep(.01)\n return {'message': \"Let's go async Chameleon!\"}\n```\n\nThe view method should return a `dict` to be passed as variables/values to the template.\n\nIf a `flask.Response` is returned, the template is skipped and the response along with status_code and\nother values is directly passed through. This is common for redirects and error responses not meant\nfor this page template. Otherwise the dictionary is used to render `async.pt` in this example.\n\n## Friendly 404s and errors\n\nA common technique for user-friendly sites is to use a [custom HTML page for 404 responses](http://www.instantshift.com/2019/10/16/user-friendly-404-pages/).\nThis library has support for friendly 404 pages using the `chameleon_flask.not_found()` function.\n\nHere's an example:\n\n```python\n@app.get('/catalog/item/{item_id}')\n@chameleon_flask.template('catalog/item.pt')\nasync def item(item_id: int):\n item = service.get_item_by_id(item_id)\n if not item:\n chameleon_flask.not_found()\n \n return item.dict()\n```\n\nThis will render a 404 response with using the template file `templates/errors/404.pt`.\nYou can specify another template to use for the response, but it's not required.\n\n## An example\n\nSee the `example/example_app.py` file for a working example to play with.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Adds integration of the Chameleon template language to Flask and Quart.",
"version": "0.0.5",
"project_urls": {
"Homepage": "https://github.com/mikeckennedy/chameleon-flask"
},
"split_keywords": [
"flask",
" chameleon",
" template",
" integration"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b4e874d8fa95a542036b4c14d4c921c6fd261a4e75ad0f4a86c06c4660d2ae4e",
"md5": "be0551abae43b77be3527885d2cc258a",
"sha256": "f4847d1d74c6e15e406bf8f5340928816939bd812d7e3f90dbeaf6e007aa47e8"
},
"downloads": -1,
"filename": "chameleon_flask-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "be0551abae43b77be3527885d2cc258a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 5520,
"upload_time": "2024-11-14T01:50:10",
"upload_time_iso_8601": "2024-11-14T01:50:10.322238Z",
"url": "https://files.pythonhosted.org/packages/b4/e8/74d8fa95a542036b4c14d4c921c6fd261a4e75ad0f4a86c06c4660d2ae4e/chameleon_flask-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "ed86e3086f6e2d905aa2a4f6d756525819e7af87c3b307e777abbfd09ea4d836",
"md5": "26ffc1d03feb73a7340f8dee7b9793dd",
"sha256": "a0382a7034a1364bba221a4af55899e3689c975e2d831fc65d6ba03053d17aa5"
},
"downloads": -1,
"filename": "chameleon_flask-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "26ffc1d03feb73a7340f8dee7b9793dd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 5782,
"upload_time": "2024-11-14T01:50:11",
"upload_time_iso_8601": "2024-11-14T01:50:11.235347Z",
"url": "https://files.pythonhosted.org/packages/ed/86/e3086f6e2d905aa2a4f6d756525819e7af87c3b307e777abbfd09ea4d836/chameleon_flask-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-14 01:50:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mikeckennedy",
"github_project": "chameleon-flask",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "flask",
"specs": []
},
{
"name": "Chameleon",
"specs": []
}
],
"tox": true,
"lcname": "chameleon_flask"
}