Skip to content

Commit

Permalink
feat: support spectree[offline] (#399)
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy authored Jan 6, 2025
1 parent 2a3f8af commit 8838871
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ If all you need is a framework-agnostic library that can generate OpenAPI docume

## Quick Start

Install with pip: `pip install spectree`. If you'd like for email fields to be validated, use `pip install spectree[email]`.
Install with pip:

```bash
pip install spectree
```

If you want to install with offline OpenAPI web pages support:

> Offline mode doesn't support SwaggerUI OAuth2 redirection.
```bash
pip install spectree[offline]
```

### Examples

Expand Down
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[project]
name = "spectree"
version = "1.4.3"
version = "1.4.4"
dynamic = []
description = "generate OpenAPI document and validate request&response with Python annotations."
readme = "README.md"
license = {text = "Apache-2.0"}
requires-python = ">=3.8"
requires-python = ">=3.9"
authors = [
{ name = "Keming Yang", email = "[email protected]" },
]
Expand All @@ -14,7 +14,6 @@ classifiers = [
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand All @@ -28,6 +27,9 @@ dependencies = [
]

[project.optional-dependencies]
offline = [
"offapi>=0.1.0",
]
dev = [
"ruff>=0.1.3",
"mypy>=0.971",
Expand Down
4 changes: 2 additions & 2 deletions spectree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ._pydantic import AnyUrl, InternalBaseModel, root_validator
from .models import SecurityScheme, Server
from .page import DEFAULT_PAGE_TEMPLATES
from .page import PAGE_TEMPLATES


class ModeEnum(str, Enum):
Expand Down Expand Up @@ -69,7 +69,7 @@ class Configuration(InternalBaseModel):
#: to render the documentation page content. (Each page template should contain a
#: `{spec_url}` placeholder, that'll be replaced by the actual OpenAPI spec URL in
#: the rendered documentation page
page_templates: Dict[str, str] = DEFAULT_PAGE_TEMPLATES
page_templates: Dict[str, str] = PAGE_TEMPLATES
#: opt-in type annotation feature, see the README examples
annotations: bool = True
#: servers section of OAS :py:class:`spectree.models.Server`
Expand Down
13 changes: 12 additions & 1 deletion spectree/page.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

DEFAULT_PAGE_TEMPLATES: Dict[str, str] = {
ONLINE_PAGE_TEMPLATES: Dict[str, str] = {
# https://github.com/Redocly/redoc
"redoc": """
<!DOCTYPE html>
Expand Down Expand Up @@ -189,3 +189,14 @@
</body>
</html>""",
}

try:
from offapi import OpenAPITemplate

PAGE_TEMPLATES = {
"redoc": OpenAPITemplate.REDOC.value,
"swagger": OpenAPITemplate.SWAGGER.value,
"scalar": OpenAPITemplate.SCALAR.value,
}
except ImportError:
PAGE_TEMPLATES = ONLINE_PAGE_TEMPLATES

0 comments on commit 8838871

Please sign in to comment.