Skip to main content

A simple API framework based on Flask.

Project description

A simple API framework based on Flask.

test pypi pypistats pypi versions

Flask OpenAPI is a simple web API framework based on Flask. It uses Pydantic to verify data and automatic generation of interaction documentation.

The key features are:

Requirements

Python 3.11+

flask-openapi is dependent on the following libraries:

Installation

pip install -U flask-openapi[swagger]
Optional dependencies

To install these dependencies with flask-openapi:

pip install flask-openapi[yaml]
# or
pip install flask-openapi[async]
# or
pip install flask-openapi[dotenv]
# or
pip install flask-openapi[email]
# or all
pip install flask-openapi[yaml,async,dotenv,email]
# or manually
pip install pyyaml asgiref python-dotenv email-validator
# OpenAPI UI plugins
pip install -U flask-openapi[swagger,redoc,rapidoc,rapipdf,scalar,elements]

A Simple Example

Here's a simple example, further go to the Example.

from pydantic import BaseModel

from flask_openapi import Info, Tag
from flask_openapi import OpenAPI

info = Info(title="book API", version="1.0.0")
app = OpenAPI(__name__, info=info)

book_tag = Tag(name="book", description="Some Book")


class BookQuery(BaseModel):
    age: int
    author: str


@app.get("/book", summary="get books", tags=[book_tag])
def get_book(query: BookQuery):
    """
    to get all books
    """
    return {
        "code": 0,
        "message": "ok",
        "data": [
            {"bid": 1, "age": query.age, "author": query.author},
            {"bid": 2, "age": query.age, "author": query.author}
        ]
    }


if __name__ == "__main__":
    app.run(debug=True)
Class-based API View Example
from pydantic import BaseModel, Field

from flask_openapi import OpenAPI, Tag, Info, APIView


info = Info(title="book API", version="1.0.0")
app = OpenAPI(__name__, info=info)

api_view = APIView(url_prefix="/api/v1", view_tags=[Tag(name="book")])


class BookPath(BaseModel):
    id: int = Field(..., description="book ID")


class BookQuery(BaseModel):
    age: int | None = Field(None, description="Age")


class BookBody(BaseModel):
    age: int | None = Field(..., ge=2, le=4, description="Age")
    author: str = Field(None, min_length=2, max_length=4, description="Author")


@api_view.route("/book")
class BookListAPIView:
    a = 1

    @api_view.doc(summary="get book list")
    def get(self, query: BookQuery):
        print(self.a)
        return query.model_dump_json()

    @api_view.doc(summary="create book")
    def post(self, body: BookBody):
        """description for a created book"""
        return body.model_dump_json()


@api_view.route("/book/<id>")
class BookAPIView:
    @api_view.doc(summary="get book")
    def get(self, path: BookPath):
        print(path)
        return "get"

    @api_view.doc(summary="update book")
    def put(self, path: BookPath):
        print(path)
        return "put"

    @api_view.doc(summary="delete book", deprecated=True)
    def delete(self, path: BookPath):
        print(path)
        return "delete"


app.register_api_view(api_view)

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

API Document

Run the simple example, and go to http://127.0.0.1:5000/openapi.

OpenAPI UI plugins are optional dependencies that require manual installation.

pip install -U flask-openapi[swagger,redoc,rapidoc,rapipdf,scalar,elements]

More optional ui templates goto the document about UI_Templates.

openapi

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_openapi-5.0.0rc1.tar.gz (574.4 kB view details)

Uploaded Source

Built Distribution

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

flask_openapi-5.0.0rc1-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file flask_openapi-5.0.0rc1.tar.gz.

File metadata

  • Download URL: flask_openapi-5.0.0rc1.tar.gz
  • Upload date:
  • Size: 574.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for flask_openapi-5.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 003a8df8232d56a718f47cba1687f02f658e0835aa64a17d2773adbf385de173
MD5 91a8f4e97bcef2b8b0dcafd81f12fa6f
BLAKE2b-256 16e4035bd95dc5652391e61a2a04d9e34ba022479c4c9919c946e3312580319a

See more details on using hashes here.

File details

Details for the file flask_openapi-5.0.0rc1-py3-none-any.whl.

File metadata

  • Download URL: flask_openapi-5.0.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for flask_openapi-5.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cd00c6c26435a9e981c67b9580d5e30e1ee7557386752340289b11774691c65
MD5 befa84262e192de4211033055e88ee45
BLAKE2b-256 ca1166f0d176f578c512a917b925eab152d518b9981f7face129773463af780a

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