Skip to main content

Full-stack async framework for Python.

Project description


Full-stack, asynchronous Python3 framework.

Design goals

  • Fast, full-service framework
  • Modular approach that does not force any design decisions

Getting started

from fastapi import FastAPI, Request

from apitoolbox import crud, db_registry
from apitoolbox.middleware import SessionMiddleware
from apitoolbox.models import BASE, Session, User

DATABASE_URL = "sqlite:///sqlite.db?check_same_thread=False"

# Define our model
class MyUser(User):

# Instantiate the application
app = FastAPI()
app.add_middleware(SessionMiddleware, bind=DATABASE_URL)

# Create all tables
bind = db_registry.get_or_create(DATABASE_URL)

# Load some data
session = Session()
for name in ["alice", "bob", "charlie", "david"]:
    user = MyUser.get_by_username(session, name)
    if user is None:
        user = MyUser(username=name)

# Add an endpoint
async def list_users(
        request: Request
    return await crud.list_instances(MyUser, request.state.session)

Assuming the above code is stored in the file, then run it via:

uvicorn main:app --reload

Call the endpoint:

curl -s localhost:8000/users | jq .

The output should contain a list of 4 users, each with the attributes id, username, updated_at and created_at.

<aside class="warning"> NOTE: Sqlite3 really doesn't like multiple threads using the same connection (hence check_same_thread=False). In this case, it's safe but in production a different database should be used. </aside>

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for apitoolbox, version 0.12.0
Filename, size File type Python version Upload date Hashes
Filename, size apitoolbox-0.12.0.tar.gz (26.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page