Fastapi: [FEATURE] RapiDoc Integration

Created on 31 Mar 2020  路  6Comments  路  Source: tiangolo/fastapi

There's another OpenAPI web viewer: RapiDoc. It has a nice interface (like redoc) and support for using the API (like swagger).

Apparently, it supports uploading an array of files. Swagger has a bug that prevents this (https://github.com/swagger-api/swagger-ui/issues/4600).

It would be nice to also have RapiDoc integration, maybe at /rapidoc?

answered enhancement

Most helpful comment

It might be nice at some point to make doc renderers configurable in a less kitchen sink fashion, since bundling RapiDoc, SwaggerUI, ReDoc, Stoplight and all the others alongside FastAPI (when most people tend to pick and use just one anyway) could end up turning into a source of avoidable bloat eventually.

All 6 comments

It might be nice at some point to make doc renderers configurable in a less kitchen sink fashion, since bundling RapiDoc, SwaggerUI, ReDoc, Stoplight and all the others alongside FastAPI (when most people tend to pick and use just one anyway) could end up turning into a source of avoidable bloat eventually.

I agree with @sm-Fifteen that a more configurable/pluggable system might be good for OpenAPI interfaces.

In the meantime though, you can of course add any interface you'd like on your own, and it's quite easy! Generally you just need to create an HTMLResponse with the necessary payload and specify the location of your schema JSON file, which you can get from the FastAPI() object (e.g. app.openapi_url). This is how you'd do it for RapiDoc:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse


app = FastAPI()


@app.get("/rapidoc", response_class=HTMLResponse, include_in_schema=False)
async def rapidoc():
    return f"""
        <!doctype html>
        <html>
            <head>
                <meta charset="utf-8">
                <script 
                    type="module" 
                    src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"
                ></script>
            </head>
            <body>
                <rapi-doc spec-url="{app.openapi_url}"></rapi-doc>
            </body> 
        </html>
    """

And if you need the openapi_url from within a routing file (let's say you want a router for all your custom docs or something), you can always get it from the Request object, e.g.

from fastapi import Request

async def get_openapi_url_in_route(req: Request):
    return req.app.openapi_url

Thanks for the help here everyone! :clap:

Yep, what @sm-Fifteen and @acnebs said.

You could create an issue in that repo requesting an example of how to integrate it with FastAPI. For example with @acnebs example.

Also, if they add the fastapi topic, it will automatically show up in the "External Links" section in the docs.

I'd like to work on this issue :)

I guess I need to:

  • add RapiDoc integration
  • update docs:

    • add it to the features page, together with Swagger/ReDoc

    • add screenshots where there are Swagger/ReDoc ones.

  • request fastapi topic in RapiDoc repo
  • request rapidoc topic in fastapi repo

Should the question of the configurability of the renderers be in another issue?

Thanks @heitorPB for your interest!

But actually, I would prefer not to have it by default in FastAPI among the others.


About configurability, I'm not really sure it would be worth it, as it would mean taking configurations for:

  • Extra docs URL
  • HTML for that config, or a function that provides that HTML
  • I can imagine others wanting to pass configurations for that HTML, like custom JS or CSS files

And then that would have to allow multiple of those things, so it would have to be a dict of dicts, or a list of some additional custom object, etc.

What I see is that it would probably end up being an involved extra configuration setup that would be more or less complex to understand and use, and wouldn't provide a huge benefit compared to just adding a new docs interface by hand, explicitly.

And as @acnebs shows above, it's quite simple to do it explicitly and doesn't impose any restrictions that a new set of configs would have to impose.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings