Skip to main content

Generate REST API and OpenAPI documentation for your Flask project.

Project description

Generate REST API and OpenAPI documentation for your Flask project.

test pypi pypistats pypi versions

Flask OpenAPI3 is a web API framework based on Flask. It uses Pydantic to verify data and automatic generation of interaction documentation: Swagger, ReDoc and RapiDoc.

The key features are:

  • Easy to code: Easy to use and easy to learn

  • Standard document specification: Based on OpenAPI Specification

  • Interactive OpenAPI documentation: Swagger, Redoc and RapiDoc

  • Data validation: Fast data verification based on Pydantic

  • Authorization: Support to reload authorizations in Swagger UI

Requirements

Python 3.7+

flask-openapi3 is dependent on the following libraries:

Installation

pip install -U flask-openapi3

or

conda install -c conda-forge flask-openapi3
Optional dependencies

To install these dependencies with flask-openapi3:

pip install flask-openapi3[yaml]
# or
pip install flask-openapi3[async]
# or
pip install flask-openapi3[dotenv]
# or
pip install flask-openapi3[email]
# or all
pip install flask-openapi3[yaml,async,dotenv,email]
# or manually
pip install pyyaml asgiref python-dotenv email-validator

A Simple Example

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

from pydantic import BaseModel

from flask_openapi3 import Info, Tag
from flask_openapi3 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 typing import Optional

from pydantic import BaseModel, Field

from flask_openapi3 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: Optional[int] = Field(None, description='Age')


class BookBody(BaseModel):
    age: Optional[int] = 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.json()

    @api_view.doc(summary="create book")
    def post(self, body: BookBody):
        """description for a created book"""
        return body.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.

You will see the documentation: Swagger, Redoc and RapiDoc.

openapi openapi-swagger openapi-redoc openapi-RapiDoc

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-openapi3-2.5.5.tar.gz (999.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_openapi3-2.5.5-py3-none-any.whl (998.0 kB view details)

Uploaded Python 3

File details

Details for the file flask-openapi3-2.5.5.tar.gz.

File metadata

  • Download URL: flask-openapi3-2.5.5.tar.gz
  • Upload date:
  • Size: 999.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for flask-openapi3-2.5.5.tar.gz
Algorithm Hash digest
SHA256 6b776eaf0814594ab8b0605cd84f0b7d0cb894a24340d6ee49176dd36bebdee8
MD5 e2ab150e581379d75aaf08260c139000
BLAKE2b-256 da95ce3d1c621b2e1698ba2f441bf340a9f83425d9360ea596d2ce2b4a1c02e0

See more details on using hashes here.

File details

Details for the file flask_openapi3-2.5.5-py3-none-any.whl.

File metadata

  • Download URL: flask_openapi3-2.5.5-py3-none-any.whl
  • Upload date:
  • Size: 998.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for flask_openapi3-2.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fe8d79a003fcb481547573c32cd260caac079b3b013d27731462b0a8f95bc08d
MD5 baa0d4e1abf0b165f4dacfc77cb39bb7
BLAKE2b-256 7bf146411342a753efb022fd5a0a888001f1069c43a0b85c3926de1d603aedea

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