Skip to main content

OpenAPI Spec-based pages (SwaggerUI, ReDoc, RapiDoc, Elements, Scalar) ready with configuration features.

Project description

Open API Pages

PyPI - Version PyPI - Python Version License Latest Commit

Downloads Downloads/Month Downloads/Week

Totally Pythonic, OpenAPI Based customizable documentation pages for SwaggerUI, ReDoc, RapiDoc, Elements, Scalar.

Keep in mind, that this package doesn't generate OpenAPI Spec, it just renders the pages with the given configuration.


Table of Contents

Features

Gimme an OpenAPI Spec, leave the rest to me...

  • Framework agnostic.
  • Zero dependencies, just Python standard library.
  • Fully typed.
  • Highly extensible.

Progress

Documentation Page Config
SwaggerUI :white_check_mark: :heavy_check_mark:
ReDoc :white_check_mark: :heavy_check_mark:
RapiDoc :white_check_mark: :heavy_check_mark:
Elements :white_check_mark: :heavy_check_mark:
Scalar :white_check_mark: :heavy_check_mark:

Emoji Key:

Emoji Meaning
:white_check_mark: Ready
:heavy_check_mark: Partially
:x: Failed
:construction: In Progress
:white_square_button: Pending
:warning: Not sure

Installation

pip install openapipages

Usage

I know it looks a bit boilerplate but it's all straight-forward. The .render() method returns the HTML as a string. Thanks to this design, you can extend and configure the pages as you wish (e.g. add extra logic to restrict access to the page).

FastAPI

The include_in_schema parameter is set to False in each endpoint to avoid including these endpoints in the OpenAPI Spec.

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from openapipages import Elements, RapiDoc, ReDoc, Scalar, SwaggerUI

# Disable the built-in /redoc page so we can make a custom one.
app = FastAPI(redoc_url=None)


@app.get("/")
def root() -> dict[str, str]:
    return {"Hello": "World"}


@app.get("/swaggerui", response_class=HTMLResponse, include_in_schema=False)
def get_swaggerui() -> str:
    return SwaggerUI(title="Swagger UI").render()


@app.get("/redoc", response_class=HTMLResponse, include_in_schema=False)
def get_redoc() -> str:
    return ReDoc(title="ReDoc").render()


@app.get("/scalar", response_class=HTMLResponse, include_in_schema=False)
def get_scalar() -> str:
    return Scalar(title="Scalar").render()


@app.get("/elements", response_class=HTMLResponse, include_in_schema=False)
def get_elements() -> str:
    return Elements(title="Elements").render()


@app.get("/rapidoc", response_class=HTMLResponse, include_in_schema=False)
def get_rapidoc() -> str:
    return RapiDoc(title="RapiDoc").render()

Litestar

The include_in_schema parameter is set to False in each endpoint to avoid including these endpoints in the OpenAPI Spec.

from litestar import Litestar, MediaType, get
from openapipages import Elements, RapiDoc, ReDoc, Scalar, SwaggerUI

openapi_url = "/schema/openapi.json"


@get("/")
def root() -> dict[str, str]:
    return {"Hello": "World"}


@get("/swaggerui", media_type=MediaType.HTML, include_in_schema=False)
def get_swaggerui() -> str:
    return SwaggerUI(title="Swagger UI", openapi_url=openapi_url).render()


@get("/redoc", media_type=MediaType.HTML, include_in_schema=False)
def get_redoc() -> str:
    return ReDoc(title="ReDoc", openapi_url=openapi_url).render()


@get("/scalar", media_type=MediaType.HTML, include_in_schema=False)
def get_scalar() -> str:
    return Scalar(title="Scalar", openapi_url=openapi_url).render()


@get("/elements", media_type=MediaType.HTML, include_in_schema=False)
def get_elements() -> str:
    return Elements(title="Elements", openapi_url=openapi_url).render()


@get("/rapidoc", media_type=MediaType.HTML, include_in_schema=False)
def get_rapidoc() -> str:
    return RapiDoc(title="RapiDoc", openapi_url=openapi_url).render()


app = Litestar([root, get_swaggerui, get_redoc, get_scalar, get_elements, get_rapidoc])

Motivation

TL;DR - I don't want to copy and paste it again...

Developer Experience

Several API Documentation tools are ready to use at your fingertips with a standard interface.

No more copying and pasting the same thing from one project to another. Import the package and use it!

Configuration

Here is a pull request made to the FastAPI repo. This was the point I understood the configuration was limited and it wouldn't change...

Also, the author's answer to this PR shows that we won't be seeing more alternative documentation tools in the future.

Alternatives

Here is another pull request made to the FastAPI repo. It brings Scalar support, but it's not approved/merged yet and I think it will stay that way thanks to the previous PR.

Standardisation

A standard interface for many API Documentation Interfaces with configuration features.

Lately, OpenAPI Spec-based Documentation tools have become popular in the Python community. We see a lot of projects (FastAPI, Litestar, APISpec, flasgger, SpecTree, Connexion, etc) offering support for OpenAPI Specification out of the box.

Litestar has support for SwaggerUI, ReDoc, RapiDoc, and Elements and FastAPI has support for SwaggerUI, and ReDoc but what is next? Will the next one be enough?

They all have one thing in common, some HTML (as Python string or a file) templated with the given configuration.

Do you see where I am going?

I want openapipages to be SQLAlchemy of OpenAPI Spec-based Documentation tools.

One interface for many! And of course Framework agnostic... So you can use it in your FastAPI, Litestar projects, or any other project that generates OpenAPI specifications.

See Also

Projects

Issues, PRs, and Discussions

Author

Disclaimer

FastAPI and Litestar projects and the two pull requests mentioned above inspired me to create this package.

License

openapipages is distributed under the terms of the MIT license.

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

openapipages-0.1.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

openapipages-0.1.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file openapipages-0.1.2.tar.gz.

File metadata

  • Download URL: openapipages-0.1.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for openapipages-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9adc7ec19997f0b0f27e5123606feaf63f09f8ef9910085174591e68cf699e20
MD5 847ea8f5f7d9f8433f85008943283135
BLAKE2b-256 0b0b65cd790394c3b9294bdbf0b4e8fcf8a51171c177dc908d1f7d586a3d6f5e

See more details on using hashes here.

File details

Details for the file openapipages-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for openapipages-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7495b69659c06ade718f3efa257a074c05813ec8cfe85b131935f6dcbd018c8b
MD5 a13c1098d9457c5b6293f2cefeaef18e
BLAKE2b-256 4c4b243292f8cfad1b0160d48f5988d5673ae34e6ee0f444027010099bc2d94c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page