Skip to main content

Flask Ninja is a web framework for building APIs with Flask and Python 3.9+ type hints.

Project description

Flask Ninja

build python

Flask Ninja is a web framework for building APIs with Flask and Python 3.9+ type hints.

Key features:

  • Easy: Designed to be easy to use and intuitive.
  • Fast to code: Type hints and automatic docs lets you focus only on business logic.
  • Standards-based: Based on the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
  • Models based: Pydantic models support and automatic (de)serialization of requests/responses.
  • Secure: Natively supports various authentication methods for the requests.

For mode details, see the Documentation

Installation

pip install flask-ninja

Usage

In your flask project where you create flask app:

from flask import Flask
from flask_ninja import NinjaAPI
from pydantic import BaseModel

app = Flask(__name__)
api = NinjaAPI(app)

class Response(BaseModel):
    """Response model containing results of various number operations."""
    sum: int
    difference: int
    product: int
    power: int

@api.get("/compute")
def compute(a: int, b: int) -> Response:
    """Computes results of various number operations.

    This endpoint returns a result of the following operations:
    - sum
    - difference
    - product
    - power

    :param int a: First number
    :param int b: Second number number
    """
    return Response(
        sum=a + b,
        difference=a - b,
        product=a * b,
        power=a ** b
    )

if __name__ == "__main__":
    app.run()

That's it !

Now you've just created an API that:

  • receives an HTTP GET request at /compute
  • takes, validates and type-casts GET parameters a and b
  • validates the returned Response object and serializes it into JSON
  • generates an OpenAPI schema for defined operation

Interactive API docs

Now go to http://127.0.0.1:5000/docs

You will see the automatic interactive API documentation (provided by Swagger UI):

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

flask_ninja-1.3.0.tar.gz (497.0 kB view details)

Uploaded Source

Built Distribution

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

flask_ninja-1.3.0-py3-none-any.whl (502.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_ninja-1.3.0.tar.gz.

File metadata

  • Download URL: flask_ninja-1.3.0.tar.gz
  • Upload date:
  • Size: 497.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for flask_ninja-1.3.0.tar.gz
Algorithm Hash digest
SHA256 3acd63a2af5bdd7ab58f4f30341aacb21830718f12b94910959af592fcdd4599
MD5 b1c3fa0c4841ec7db5c2675d8a91faec
BLAKE2b-256 0d636c72fc186b31c641cae3698aaa2a655aa4e7b2252d738cc9ffa15ba97939

See more details on using hashes here.

File details

Details for the file flask_ninja-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: flask_ninja-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 502.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for flask_ninja-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2175ad83cda5b501c2fba1f9b5812ee7c3817c49722c1ba0895dcdf232490db4
MD5 33df58a1e765154cbc9cbb2c838f4ccd
BLAKE2b-256 b1ad11ab62aa5756e0719aaacb564c94e4ed98f423c0faa09f2a4cca82646d24

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