Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/AGeekInside/fastapi-jinja
Adds integration of the Jinja template language to FastAPI.
https://github.com/AGeekInside/fastapi-jinja
hacktoberfest hacktoberfest2020
Last synced: 13 days ago
JSON representation
Adds integration of the Jinja template language to FastAPI.
- Host: GitHub
- URL: https://github.com/AGeekInside/fastapi-jinja
- Owner: AGeekInside
- License: mit
- Fork: true (mikeckennedy/fastapi-chameleon)
- Created: 2020-10-26T21:44:13.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-21T21:42:46.000Z (10 months ago)
- Last Synced: 2024-08-01T00:42:26.746Z (3 months ago)
- Topics: hacktoberfest, hacktoberfest2020
- Language: Python
- Homepage:
- Size: 28.3 KB
- Stars: 67
- Watchers: 2
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fastapi - FastAPI Jinja - Adds integration of the Jinja template language to FastAPI. (Third-Party Extensions / Utils)
- awesome-fastapi - FastAPI Jinja - Adds integration of the Jinja template language to FastAPI. (Third-Party Extensions / Utils)
README
# fastapi-jinja
Adds integration of the Jinja template language to FastAPI. This is inspired and based off fastapi-chamelon by Mike Kennedy. Check that out, if you are using chamelon.
## Installation
For the moment, this is not yet on pypi and is fairly unstable, but if you wish to used it directly from here, just do the following:
```bash
pip install git+https://github.com/ageekinside/fastapi-jinja
```You may want to fork this repo and then use your URL until this is more final.
## 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.j2
│ └── shared
│ └── layout.j2```
In the app startup, tell the library about the folder you wish to use:
```python
import os
import fastapi_jinjadev_mode = True
folder = os.path.dirname(__file__)
template_folder = os.path.join(folder, 'templates')
template_folder = os.path.abspath(template_folder)fastapi_jinja.global_init(template_folder, auto_reload=dev_mode)
```Then just decorate the FastAPI view methods (works on sync and async methods):
```python
@router.post('/')
@fastapi_jinja.template('home/index.j2')
async def home_post(request: Request):
form = await request.form()
vm = PersonViewModel(**form)return vm.dict() # {'first':'John', 'last':'Doe', ...}
```
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.