# FastAPI CDN host Selector for docs ui

[](https://pypi.python.org/pypi/fastapi-cdn-host)
[](https://github.com/waketzheng/fastapi-cdn-host/actions?query=workflow:ci)
[](https://coveralls.io/github/waketzheng/fastapi-cdn-host?branch=main)
[](https://github.com/astral-sh/ruff)
[](https://github.com/PyCQA/bandit)
Auto find swagger-ui in local files, if exist use them.
Otherwise make concurrent http requests by httpx to find out which third part cdn host(cdn.jsdelivr.net/unpkg.com/cdnjs.cloudflare.com/cdn.staticfile.org) is the fastest one.
**English** | [中文](./README.zh.md)
## Install
```bash
pip install fastapi-cdn-host
```
## Usage
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
# include_routes ...
fastapi_cdn_host.patch_docs(app)
```
See more at:
- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)
- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)
## Detail
1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
fastapi_cdn_host.patch_docs(app) # Will use `unpkg.com`(or other faster host) to replace the `cdn.jsdelivr.net/npm`
```
2. To support offline docs/, put swagger-ui asset files into local directory named `static`
```py
from pathlib import Path
fastapi_cdn_host.patch_docs(app, Path(__file__).parent.joinpath('static'))
```
This get the same result of the example in official document:
https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting-javascript-and-css-for-docs
3. If asset files are ready in private cdn
```py
from fastapi_cdn_host import AssetUrl
fastapi_cdn_host.patch_docs(
app,
cdn_host=AssetUrl(
js='http://my-cdn.com/swagger-ui.js',
css='http://my-cdn.com/swagger-ui.css',
redoc='http://my-cdn.com/redoc.standalone.js',
favicon='http://my-cdn.com/favicon.ico',
)
)
```
## License
[MIT](./LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/waketzheng/fastapi-cdn-host",
"name": "fastapi-cdn-host",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Waket Zheng",
"author_email": "waketzheng@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/72/31/5c1a486e2978319c4cd7a8f7f04c07884bbe90c269d15273c342fdce8f08/fastapi_cdn_host-0.8.4.tar.gz",
"platform": null,
"description": "# FastAPI CDN host Selector for docs ui\n\n[](https://pypi.python.org/pypi/fastapi-cdn-host)\n[](https://github.com/waketzheng/fastapi-cdn-host/actions?query=workflow:ci)\n[](https://coveralls.io/github/waketzheng/fastapi-cdn-host?branch=main)\n[](https://github.com/astral-sh/ruff)\n[](https://github.com/PyCQA/bandit)\n\nAuto find swagger-ui in local files, if exist use them.\nOtherwise make concurrent http requests by httpx to find out which third part cdn host(cdn.jsdelivr.net/unpkg.com/cdnjs.cloudflare.com/cdn.staticfile.org) is the fastest one.\n\n\n**English** | [\u4e2d\u6587](./README.zh.md)\n\n## Install\n\n```bash\npip install fastapi-cdn-host\n```\n\n## Usage\n```py\nimport fastapi_cdn_host\nfrom fastapi import FastAPI\n\napp = FastAPI()\n# include_routes ...\n\nfastapi_cdn_host.patch_docs(app)\n```\nSee more at:\n- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)\n- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)\n\n## Detail\n1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.\n```py\nimport fastapi_cdn_host\nfrom fastapi import FastAPI\n\napp = FastAPI()\nfastapi_cdn_host.patch_docs(app) # Will use `unpkg.com`(or other faster host) to replace the `cdn.jsdelivr.net/npm`\n```\n2. To support offline docs/, put swagger-ui asset files into local directory named `static`\n```py\nfrom pathlib import Path\n\nfastapi_cdn_host.patch_docs(app, Path(__file__).parent.joinpath('static'))\n```\nThis get the same result of the example in official document:\nhttps://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting-javascript-and-css-for-docs\n\n3. If asset files are ready in private cdn\n```py\nfrom fastapi_cdn_host import AssetUrl\n\nfastapi_cdn_host.patch_docs(\n app,\n cdn_host=AssetUrl(\n js='http://my-cdn.com/swagger-ui.js',\n css='http://my-cdn.com/swagger-ui.css',\n redoc='http://my-cdn.com/redoc.standalone.js',\n favicon='http://my-cdn.com/favicon.ico',\n )\n)\n```\n\n## License\n\n[MIT](./LICENSE)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Auto choose the fastest cdn host for fastapi project docs.",
"version": "0.8.4",
"project_urls": {
"Homepage": "https://github.com/waketzheng/fastapi-cdn-host",
"Repository": "https://github.com/waketzheng/fastapi-cdn-host.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c7c3dfd4756c7105bfeb4588795cf8b8e953a00413b385bb243b3b8bd897e25c",
"md5": "10c2ac16064c426fdbe2c78a551608d6",
"sha256": "4f2668a0726bfaa20feafa93bc174249265a44df51d3995b9bc147c943013010"
},
"downloads": -1,
"filename": "fastapi_cdn_host-0.8.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "10c2ac16064c426fdbe2c78a551608d6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 14014,
"upload_time": "2025-02-16T14:46:43",
"upload_time_iso_8601": "2025-02-16T14:46:43.290047Z",
"url": "https://files.pythonhosted.org/packages/c7/c3/dfd4756c7105bfeb4588795cf8b8e953a00413b385bb243b3b8bd897e25c/fastapi_cdn_host-0.8.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "72315c1a486e2978319c4cd7a8f7f04c07884bbe90c269d15273c342fdce8f08",
"md5": "f9fadbb5e5f99edec2fb163b150db629",
"sha256": "8e955d1974d861ba13586b2ab01af4b6c040e41593b29f14d07efa980453904b"
},
"downloads": -1,
"filename": "fastapi_cdn_host-0.8.4.tar.gz",
"has_sig": false,
"md5_digest": "f9fadbb5e5f99edec2fb163b150db629",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 12996,
"upload_time": "2025-02-16T14:46:51",
"upload_time_iso_8601": "2025-02-16T14:46:51.604309Z",
"url": "https://files.pythonhosted.org/packages/72/31/5c1a486e2978319c4cd7a8f7f04c07884bbe90c269d15273c342fdce8f08/fastapi_cdn_host-0.8.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-16 14:46:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "waketzheng",
"github_project": "fastapi-cdn-host",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-cdn-host"
}