一个将 FastAPI 与 Stoplight Elements 结合的库,用于快速生成美观、交互式的 API 文档
Project description
FastAPI Stoplight Elements
一个将 FastAPI 与 Stoplight Elements 结合的库,用于快速生成美观、交互式的 API 文档。
项目简介
该项目提供了一个简单的接口,可以轻松地将 Stoplight Elements 集成到 FastAPI 应用中,为你的 API 自动生成专业的文档界面。Stoplight Elements 是一个功能强大的开源 API 文档工具,支持 OpenAPI 规范,并提供交互式测试功能。
功能特点
- 自动从 FastAPI 应用生成 OpenAPI 规范文档
- 提供美观、现代化的文档界面
- 支持交互式 API 测试(Try It 功能)
- 支持明暗主题切换
- 响应式设计,适配各种屏幕尺寸
- 高度可配置,满足不同需求
- 支持多种布局模式(侧边栏、响应式、堆叠式)
- 支持多种路由模式
界面预览
以下是 API 文档界面的预览截图:
文档首页
API 详情页
交互式测试
暗主题模式
快速开始
以下是一个简单的使用示例:
from fastapi import FastAPI, Request
from pydantic import BaseModel, Field
from fastapi_stoplight import get_stoplight_elements_html
app = FastAPI()
# 定义数据模型
class User(BaseModel):
name: str = Field(..., description="用户名")
age: int = Field(..., description="年龄")
# 定义路由
@app.get("/index/{pk}", summary="首页")
def read_root(pk: str):
return {"Hello": pk}
@app.get("/list", summary="用户列表", response_model=list[User])
def list_users():
return [
{"id": 1, "name": "张三", "age": 18},
{"id": 2, "name": "李四", "age": 19},
{"id": 3, "name": "王五", "age": 20},
]
# 添加文档路由
@app.get("/stoplight", include_in_schema=False)
def stoplight(request: Request):
return get_stoplight_elements_html(
openapi_url=request.app.openapi_url,
title="用户服务API"
)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
运行应用后,访问 http://localhost:8000/stoplight 即可查看生成的 API 文档。
配置选项
get_stoplight_elements_html 函数支持以下参数:
openapi_url: OpenAPI 规范文件的 URL 路径title: API 文档页面的标题stoplight_elements_js_url: Stoplight Elements JavaScript 库的 CDN URLstoplight_elements_css_url: Stoplight Elements CSS 样式的 CDN URLstoplight_elements_favicon_url: 页面 favicon 图标的 URLapi_description_document: 直接提供的 OpenAPI 文档内容base_path: API 的基础路径hide_internal: 是否隐藏标记为内部的 APIhide_try_it: 是否完全隐藏 "Try It" 功能hide_try_it_panel: 是否隐藏 "Try It" 面板,但保留请求示例hide_schemas: 是否在侧边栏中隐藏模式定义hide_export: 是否隐藏导出按钮try_it_cors_proxy: CORS 代理 URLtry_it_credential_policy: "Try It" 功能的凭证策略layout: 文档的布局模式logo: 自定义 Logo 的 URLrouter: 路由模式
许可证
本项目采用 MIT 许可证。详情请见 LICENSE 文件。
贡献
欢迎贡献代码!请先阅读贡献指南。
- Fork 项目
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开 Pull Request=
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_stoplight-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_stoplight-0.1.0.tar.gz
- Upload date:
- Size: 915.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fcee97fe85cb7ca5d5a2639eff67423fabeec0b6dfaecb5be279768bfd9ec02
|
|
| MD5 |
0e7b451de29eb8f0c7b92815d457abe0
|
|
| BLAKE2b-256 |
c9662054f92778eb47f03be0664c3c461b304d7a5c56662bb589321ad9553c3a
|
File details
Details for the file fastapi_stoplight-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_stoplight-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2a5e25378ca5bf100984fa601b74e624199731945e3e867a3b11075686a9c9f
|
|
| MD5 |
811c845d260cc5bd5b020361b9cd5da6
|
|
| BLAKE2b-256 |
fec26f285372e8c6f97954cc75636e4825d7d8a91ad0675082b9888631dae648
|
File details
Details for the file fastapi_stoplight-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: fastapi_stoplight-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4728a9ef87298d28164128cded7e1b45848abd1944a568de2afb6aed2d9dd94
|
|
| MD5 |
e0a3fd17678e535e62656e2dbe9a5a26
|
|
| BLAKE2b-256 |
b73b1a1af20493161496d978c2c6b1b6863e16aa570efd603d5d39d6461f18af
|