# FastAPI Swagger Plugin
This plugin updates the FastAPI app to include a latest Swagger UI distribution. Also, it works locally and does not
depend on the @tiangolo domains and cdns.
## Why?
The FastAPI already includes a Swagger UI. However, author updates swagger not so often. Moreover he uses his own
hosts for the resources, and they are not always available (especially in Russia). This plugin allows to use the latest
Swagger UI distribution and host it inside your app.
## Usage
### Installation
```bash
pip install fastapi-swagger
```
### Basic Usage
```python
from fastapi import FastAPI
from fastapi_swagger import patch_fastapi
app = FastAPI(docs_url=None, swagger_ui_oauth2_redirect_url=None) # docs url will be at /docs
patch_fastapi(app)
```
## How it works?
How it was before:
FastAPI uses the `get_swagger_ui_html` function to render the Swagger UI under the hood.
```python
def get_swagger_ui_html(
*,
openapi_url: str,
title: str,
swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
oauth2_redirect_url: Optional[str] = None,
init_oauth: Optional[Dict[str, Any]] = None,
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
) -> HTMLResponse:
...
```
How it is now:
Actually, we just copy the Swagger UI distribution from
the [GitHub releases](https://github.com/swagger-api/swagger-ui/releases) to the `fastapi_swagger` package, and serve it
from your app.
Patch creates several additional routes with the Swagger UI resources and one route for docs page (btw, using same
`get_swagger_ui_html` function).
```python
def patch_fastapi(
app: FastAPI,
docs_url: str = "/docs",
*,
title: Optional[str],
swagger_js_url: str = "/swagger/swagger-ui-bundle.js", # relative path from app root
swagger_css_url: str = "/swagger/swagger-ui.css", # relative path from app root
swagger_favicon_url: str = "/swagger/favicon-32x32.png", # relative path from app root
oauth2_redirect_url: Optional[str] = None,
init_oauth: Optional[Dict[str, Any]] = None,
swagger_ui_parameters: Optional[Dict[str, Any]] = None,
):
...
patch_fastapi(app)
# Now there are additional routes /swagger/swagger-ui-bundle.js, /swagger/swagger-ui.css, /swagger/favicon-32x32.png and /docs
# They all are not dependent on the external resources.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/dantetemplar/fastapi-swagger",
"name": "fastapi-swagger",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Ruslan Bel'kov",
"author_email": "ruslan.belckov@yandex.ru",
"download_url": "https://files.pythonhosted.org/packages/0b/00/1ff6089b3802b370c985463f418f29aab777b6c68eb9dfb09e8206c998b2/fastapi_swagger-0.2.7.tar.gz",
"platform": null,
"description": "# FastAPI Swagger Plugin\n\nThis plugin updates the FastAPI app to include a latest Swagger UI distribution. Also, it works locally and does not\ndepend on the @tiangolo domains and cdns.\n\n## Why?\n\nThe FastAPI already includes a Swagger UI. However, author updates swagger not so often. Moreover he uses his own\nhosts for the resources, and they are not always available (especially in Russia). This plugin allows to use the latest\nSwagger UI distribution and host it inside your app.\n\n## Usage\n\n### Installation\n\n```bash\npip install fastapi-swagger\n```\n\n### Basic Usage\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_swagger import patch_fastapi\n\napp = FastAPI(docs_url=None, swagger_ui_oauth2_redirect_url=None) # docs url will be at /docs\n\npatch_fastapi(app)\n```\n\n## How it works?\n\nHow it was before:\nFastAPI uses the `get_swagger_ui_html` function to render the Swagger UI under the hood.\n\n```python\ndef get_swagger_ui_html(\n *,\n openapi_url: str,\n title: str,\n swagger_js_url: str = \"https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js\",\n swagger_css_url: str = \"https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css\",\n swagger_favicon_url: str = \"https://fastapi.tiangolo.com/img/favicon.png\",\n oauth2_redirect_url: Optional[str] = None,\n init_oauth: Optional[Dict[str, Any]] = None,\n swagger_ui_parameters: Optional[Dict[str, Any]] = None,\n) -> HTMLResponse:\n ...\n```\n\nHow it is now:\n\nActually, we just copy the Swagger UI distribution from\nthe [GitHub releases](https://github.com/swagger-api/swagger-ui/releases) to the `fastapi_swagger` package, and serve it\nfrom your app.\nPatch creates several additional routes with the Swagger UI resources and one route for docs page (btw, using same\n`get_swagger_ui_html` function).\n\n```python\ndef patch_fastapi(\n app: FastAPI,\n docs_url: str = \"/docs\",\n *,\n title: Optional[str],\n swagger_js_url: str = \"/swagger/swagger-ui-bundle.js\", # relative path from app root\n swagger_css_url: str = \"/swagger/swagger-ui.css\", # relative path from app root\n swagger_favicon_url: str = \"/swagger/favicon-32x32.png\", # relative path from app root\n oauth2_redirect_url: Optional[str] = None,\n init_oauth: Optional[Dict[str, Any]] = None,\n swagger_ui_parameters: Optional[Dict[str, Any]] = None,\n):\n ...\n\n\npatch_fastapi(app)\n# Now there are additional routes /swagger/swagger-ui-bundle.js, /swagger/swagger-ui.css, /swagger/favicon-32x32.png and /docs\n# They all are not dependent on the external resources.\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "This plugin updates the FastAPI app to host latest Swagger UI distribution.",
"version": "0.2.7",
"project_urls": {
"Homepage": "https://github.com/dantetemplar/fastapi-swagger",
"Repository": "https://github.com/dantetemplar/fastapi-swagger"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b2e91c8f5bdbb64d30db8d1e49c288f7b65a9e851c9442274fb852ff22a46e6d",
"md5": "9153d3e8b40dca7c226d8a6b6e288740",
"sha256": "2e6782ecc0e9f02db5d6e84edf9c13c675687fad499649fa3a05fbdb68b9ffca"
},
"downloads": -1,
"filename": "fastapi_swagger-0.2.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9153d3e8b40dca7c226d8a6b6e288740",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 434697,
"upload_time": "2025-02-18T01:07:20",
"upload_time_iso_8601": "2025-02-18T01:07:20.620685Z",
"url": "https://files.pythonhosted.org/packages/b2/e9/1c8f5bdbb64d30db8d1e49c288f7b65a9e851c9442274fb852ff22a46e6d/fastapi_swagger-0.2.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "0b001ff6089b3802b370c985463f418f29aab777b6c68eb9dfb09e8206c998b2",
"md5": "07f81d3bfe903d668f51adf96e421a6f",
"sha256": "3ce55882fcfcd89e4bf6077c68ea4957974729517bdc2d868409f3603431a8c6"
},
"downloads": -1,
"filename": "fastapi_swagger-0.2.7.tar.gz",
"has_sig": false,
"md5_digest": "07f81d3bfe903d668f51adf96e421a6f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 433000,
"upload_time": "2025-02-18T01:07:24",
"upload_time_iso_8601": "2025-02-18T01:07:24.039891Z",
"url": "https://files.pythonhosted.org/packages/0b/00/1ff6089b3802b370c985463f418f29aab777b6c68eb9dfb09e8206c998b2/fastapi_swagger-0.2.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-18 01:07:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dantetemplar",
"github_project": "fastapi-swagger",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-swagger"
}