Skip to main content

A library for creating modular FastAPI applications.

Project description

FastAPI Blocks

PyPI version License: MIT Python 3.11+ Tests

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 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

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_blocks-0.1.5.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi_blocks-0.1.5-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_blocks-0.1.5.tar.gz.

File metadata

  • Download URL: fastapi_blocks-0.1.5.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_blocks-0.1.5.tar.gz
Algorithm Hash digest
SHA256 80ff757acc7ccff76388cb1a9bbca0055ffa48c1181397bf54d52cdcdbddb040
MD5 a079ca611fce25d76ee9ab2b1d0ba2d4
BLAKE2b-256 148cea49ac7c8813cc01fe3aeb770c223d4ee6f02cdedd138f12440936aa3b42

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_blocks-0.1.5.tar.gz:

Publisher: python-publish.yml on r3sbarra/fastapi-blocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_blocks-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: fastapi_blocks-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_blocks-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 82cd69d8e8e2c5e833b1a8414f62e45fd426eb327b3b359894520a4f817bd7e4
MD5 66f600e2a3a6189974bb9521dd57e3e8
BLAKE2b-256 742feb3e8c7dd6d8901c78236a3367e01279ce0ead04e56812f9c46d45d7dd1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_blocks-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on r3sbarra/fastapi-blocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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