完全基於pythonic,基於開放式的可自定義文檔頁面,用於Swaggerui,redoc,rapidoc,元素,標量。
請記住,此軟件包不會生成OpenAPI規格,它只是用給定的配置呈現頁面。
gimme a Openapi規格,把其餘的留給我...
| 文件 | 頁 | config |
|---|---|---|
| Swaggerui | ✅ | ✔️ |
| 重做 | ✅ | ✔️ |
| Rapidoc | ✅ | ✔️ |
| 元素 | ✅ | ✔️ |
| 標量 | ✅ | ✔️ |
表情符號鍵:
| 表情符號 | 意義 |
|---|---|
| ✅ | 準備好 |
| ✔️ | 部分 |
| 失敗的 | |
| ? | 進行中 |
| ? | 待辦的 |
| 沒有把握 |
pip install openapipages 我知道它看起來有點樣板,但這都是直截了當的。 .render()方法將HTML返回為字符串。借助此設計,您可以根據需要擴展和配置頁面(例如,添加額外的邏輯以限制訪問頁面的訪問)。
include_in_schema參數在每個端點中設置為False,以避免在OpenAPI規範中包括這些端點。
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 ()
include_in_schema參數在每個端點中設置為False,以避免在OpenAPI規範中包括這些端點。
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 ])tl; dr-我不想複製並粘貼它...
幾種API文檔工具可以用標準接口在您的指尖使用。
不再將同一事物從一個項目複製和粘貼到另一個項目。導入包並使用它!
這是對FastApi回購的拉動請求。這是我了解配置有限的觀點,不會改變...
另外,作者對此公關的回答表明,將來我們將不會看到更多替代文檔工具。
這是對Fastapi倉庫提出的另一個拉力請求。它帶來了標量支持,但尚未獲得批准/合併,我認為由於以前的PR,它將保持這種狀態。
許多API文檔界面的標準接口具有配置功能。
最近,基於OpenAPI規格的文檔工具在Python社區中變得很流行。我們看到許多項目(Fastapi,Litestar,Apispec,Flasgger,Spectree,Connexion等)提供了支持OpenAPI規範的支持。
Litestar對Swaggerui,ReDoc,Rapidoc和Elements有支持,Fastapi對Swaggerui和Redoc有所支持,但是接下來是什麼?下一個足夠嗎?
它們都有一個共同點,其中一些HTML(如python字符串或文件)用給定的配置模板。
你看到我要去的地方嗎?
我希望openapipages成為基於OpenAPI規格的文檔工具的Sqlalchemy。
一個界面的一個接口!當然,框架不可知論...因此,您可以在Fastapi,Litestar項目或任何其他生成OpenAPI規格的項目中使用它。
Fastapi和Litestar項目以及上面提到的兩個拉請求激發了我創建此軟件包。
openapipages根據MIT許可條款分發。