Giter Club home page Giter Club logo

flask-ninja's Introduction

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

flask-ninja's People

Contributors

kabell avatar kenaco avatar samarcan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.