FastAPI Blocks
fastapi-blocks is a Python package that allows you to structure your FastAPI applications using a modular "blocks" architecture. Each block is a self-contained component with its own routes, static files, and templates. This makes it easy to develop and maintain large FastAPI applications by breaking them down into smaller, reusable pieces.
Installation
pip install fastapi-blocks
Then run
python -m fastapi_blocks setup -f BLOCKS_FOLDER
*Replace BLOCKS_FOLDER with path to blocks folder from root
Updating blocks
python -m fastapi_blocks update
Additional flags:
- -A - Auto-install pip packages from blocks
- -S - Save block hashes
- -V - Do not verify block hashes (Default is to verify)
Examples
Example Block
- Example Block - An example block
Example Project
- Example Project - An example project
Reserved Words
Variable names
router- In router files, the variable router is used to searche for router to include into app.
Template names
How to
Register Jinja funcs for shared templates
In order to allow other blocks to access functions via jinja, you would need to add the functions via hook into BlockManager().templates_globals, the key being the name of the function to call from jinja, and the value being the function itself.
eg.
def register_jinja_funcs(**kwargs):
from fastapi_blocks import BlockManager
BlockManager().templates_globals['has_role'] = has_role
class Settings(BlockSettingsMixin):
additional_user_roles : Optional[List[str]] = Field(description="Additional user roles to add", default=None)
def _start_hooks(self) -> List:
return super()._start_hooks() + [register_jinja_funcs]
Then, make sure that the block_config.toml has an entry for extra_block_settings with the name of the python file as value. .ie for 'settings.py', it would be settings.
Once that is done, you will need to run setup so that the blockmanager toml is updated.
In some other block, you would then be able to call the function from jinja as long as it is using templates from BlockManager.
If you want to use jinja within a blocks jinja2env, you'd have to get the template_globals from blockmanager then add them to the jinja2env's globals.
Todo:
- Put more meaningful tests
- Add command to setup templates/default
- Add todos
- Add some sort of security check
Metadata
Release files for fastapi-blocks 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_blocks-0.1.5.tar.gz | 15.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_blocks-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.5 kB
Release files / fastapi_blocks-0.1.5.tar.gz
| Download URL | fastapi_blocks-0.1.5.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
80ff757acc7ccff76388cb1a9bbca0055ffa48c1181397bf54d52cdcdbddb040
|
|
BLAKE2b-256 checksum How to use checksums |
148cea49ac7c8813cc01fe3aeb770c223d4ee6f02cdedd138f12440936aa3b42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 27, 2025.
Transparency logRelease files / fastapi_blocks-0.1.5-py3-none-any.whl
| Download URL | fastapi_blocks-0.1.5-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
82cd69d8e8e2c5e833b1a8414f62e45fd426eb327b3b359894520a4f817bd7e4
|
|
BLAKE2b-256 checksum How to use checksums |
742feb3e8c7dd6d8901c78236a3367e01279ce0ead04e56812f9c46d45d7dd1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 27, 2025.
Transparency log