Skip to main content

StarMallow framework

Project description

StarMallow

StarMallow is a starlette based web framework for building robust APIs with Python and Marshmallow. It's been inspired by FastAPI, but uses Marshmallow instead of Pydantic for it's schema definitions. It's reason for existing is simply because marshmallow is a much more powerful serialization engine, and in most cases it's fast enough. An example of Pydantic's limitations can be found in Issue 2277

Example

Create it

Create a file main.py with:

from typing import Annotated
from marshmallow_dataclass import dataclass
from starmallow import Body, Path, StarMallow

app = StarMallow()

# Minimum example
@app.get("/path/{item_id}")
def get_id(item_id):
    return item_id


# Example with explicit location option
@dataclass
class MyBody:
    item_id: int
    sub_item_id: int


@app.get("/body")
async def get_body(body: MyBody = Body()) -> int:
    return body.item_id


# Example with explicit marshmallow schema
class MyBodySchema(ma.Schema):
    item_id = mf.Integer()

@app.get("/path/body_schema")
def get_body_from_schema(body: Dict[str, int] = Body(model=MyBodySchema)) -> int:
    return body['item_id']


# Example with Annotated

@app.get("/body_annotated")
async def get_body_annotated(body: Annotated[MyBody, Body()]) -> int:
    return body.item_id

Run it

Run the server with:

 uvicorn sample_server:app --reload

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [84092]
INFO:     Started server process [87944]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

View the docs

HTTP Endpoints

You can also use class-based views. This can make it easier to organize your code and gives you an easy migration path if you use flask-smorest

from marshmallow_dataclass import dataclass
from starmallow import StarMallow
from starmallow.decorators import route
from starmallow.endpoints import APIHTTPEndpoint


app = StarMallow()


@dataclass
class Pet:
    name: str


@app.api_route('/')
class Pets(APIHTTPEndpoint):
    def get(self) -> Pet:
        return Pet.get()

    def post(self, pet: Pet) -> Pet:
        pet.create()
        return pet


@app.api_route('/id/{pet_id}')
class PetById(ApiHttpEndpoint):
    @route(deprecated=True)  # Specify @route if you need to override parameters
    def get(self, pet_id: int) -> Pet:
        return Pet.get(pet_id)


    @route(status_code=204)  # Specify @route if you need to override parameters
    def get(self, pet_id: int):
        Pet.get(pet_id).delete()

Optional Dependencies

  • uvicorn - for the server that loads and serves your application.
  • orjson - Required if you want to use ORJSONResponse.
  • ujson - Required if you want to use UJSONResponse.

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

starmallow-0.6.4.tar.gz (80.9 kB view details)

Uploaded Source

Built Distribution

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

starmallow-0.6.4-py3-none-any.whl (56.8 kB view details)

Uploaded Python 3

File details

Details for the file starmallow-0.6.4.tar.gz.

File metadata

  • Download URL: starmallow-0.6.4.tar.gz
  • Upload date:
  • Size: 80.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.0

File hashes

Hashes for starmallow-0.6.4.tar.gz
Algorithm Hash digest
SHA256 a8c9ad488a7a4eea44b739d990f5333893c7bbe465b6ac109f48bdeef25edeaf
MD5 44c9599e075be91716540a7f5d15807a
BLAKE2b-256 87432ee37ad064551b43f8e27cb99bfaaf3b5af8c7bf0395c3014ad08a6b5b0c

See more details on using hashes here.

File details

Details for the file starmallow-0.6.4-py3-none-any.whl.

File metadata

  • Download URL: starmallow-0.6.4-py3-none-any.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.0

File hashes

Hashes for starmallow-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ec074011984a8bd958fcbeac6daba1a86d5b24b9545dc8eaec7fd98ce4c79e6a
MD5 5049dbaf70024b6b45d3522422934d26
BLAKE2b-256 02b3c457fe32facef2ddf9244e4ad8654e5154ec85c3bf0d98bf0f25c4d1933a

See more details on using hashes here.

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