fastapi-standalone-docs 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 IOXIO®
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.1
2
+ Name: fastapi-standalone-docs
3
+ Version: 0.0.1
4
+ Summary: Host FastAPI Swagger UI and ReDoc without third party CDNs
5
+ License: MIT
6
+ Author: IOXIO Ltd
7
+ Requires-Python: >=3.8.1,<4.0
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Provides-Extra: scripts
14
+ Requires-Dist: fastapi (>=0.95.2,<0.98.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # FastAPI Standalone Docs
18
+
19
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ioxiocom/fastapi-standalone-docs/publish.yaml)](https://github.com/ioxiocom/fastapi-standalone-docs/actions/workflows/publish.yaml)
20
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
21
+ [![PyPI](https://img.shields.io/pypi/v/fastapi-standalone-docs)](https://pypi.org/project/fastapi-standalone-docs/)
22
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-standalone-docs)](https://pypi.org/project/fastapi-standalone-docs/)
23
+ [![License: BSD 3-Clause](https://img.shields.io/pypi/l/fastapi-standalone-docs)](https://opensource.org/license/mit/)
24
+
25
+ Host FastAPI Swagger UI and ReDoc without any third party CDNs. Handy if you want to be
26
+ able to use the docs offline or want to avoid loading content from 3rd party CDNs due to
27
+ privacy reasons.
28
+
29
+ This library was heavily inspired by
30
+ [fastapi-offline-swagger-ui](https://github.com/ahmetoner/fastapi-offline-swagger-ui),
31
+ but we wanted a way to take it into use with less boilerplate code and also have it
32
+ hosted on PyPI.
33
+
34
+ ## Installation
35
+
36
+ The package is available on PyPI:
37
+
38
+ ```bash
39
+ pip install fastapi-standalone-docs
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ Import `StandaloneDocs` and pass in the FastAPI application to it:
45
+
46
+ ```python
47
+ from fastapi import FastAPI
48
+ from fastapi_standalone_docs import StandaloneDocs
49
+
50
+ app = FastAPI()
51
+ StandaloneDocs(app=app)
52
+ ```
53
+
54
+ The static files will by default be served from subpaths of the `docs_url` and
55
+ `redoc_url` as you've specified in your FastAPI app. The library doesn't make any
56
+ assumption on any particular path being reserved for the library. And if you disable
57
+ either of the two UIs, the corresponding files won't either be served unnecessarily.
58
+
59
+ This example would move the Swagger UI and the static files needed for it to the path
60
+ `/swagger` and disable the ReDoc interface and the static files needed by it:
61
+
62
+ ```python
63
+ from fastapi import FastAPI
64
+ from fastapi_standalone_docs import StandaloneDocs
65
+
66
+ app = FastAPI(docs_url="/swagger", redoc_url=None)
67
+ StandaloneDocs(app=app)
68
+ ```
69
+
70
+ By default, the library will use the FastAPI favicons, and they're served from
71
+ `{docs_url}/fastapi/favicon.png` and `{redoc_url}/fastapi/favicon.png`. You can use your
72
+ own favicons like this, which will also disable the FastAPI icons:
73
+
74
+ ```python
75
+ from fastapi import FastAPI
76
+ from fastapi_standalone_docs import StandaloneDocs
77
+
78
+ app = FastAPI()
79
+ StandaloneDocs(
80
+ app=app,
81
+ redoc_favicon_url="/favicon.png",
82
+ swagger_favicon_url="/favicon.png",
83
+ )
84
+ ```
85
+
86
+ To make the docs truly stand alone the Google Fonts are by default disabled (they are
87
+ used in the ReDoc interface). If you want to enable them just set
88
+ `with_google_fonts=True`, like this:
89
+
90
+ ```python
91
+ from fastapi import FastAPI
92
+ from fastapi_standalone_docs import StandaloneDocs
93
+
94
+ app = FastAPI()
95
+ StandaloneDocs(
96
+ app=app,
97
+ with_google_fonts=True,
98
+ )
99
+ ```
100
+
101
+ ## Development
102
+
103
+ PRs are welcome!
104
+
105
+ This project is using [Poetry](https://python-poetry.org/) and
106
+ [pre-commit](https://pre-commit.com/), so please ensure you've installed both. To set up
107
+ the project with them run:
108
+
109
+ ```bash
110
+ pre-commit install
111
+ poetry install
112
+ ```
113
+
114
+ Running tests locally:
115
+
116
+ ```bash
117
+ poetry run invoke test
118
+ ```
119
+
120
+ Updating the static files from the CDNs:
121
+
122
+ ```bash
123
+ poetry run update-fastapi-standalone-docs
124
+ ```
125
+
126
+ If you want to test using it locally in some other project, in that project run this
127
+ (make sure to adjust the path to the local repo):
128
+
129
+ ```bash
130
+ poetry add ../fastapi-standalone-docs --editable
131
+ ```
132
+
133
+ ## License
134
+
135
+ The code of this library itself is released under the [MIT license](./LICENSE). Please
136
+ however note that this library also contains parts of
137
+ [ReDoc](./fastapi_standalone_docs/static/redoc/), which is licensed under the
138
+ [MIT license](./fastapi_standalone_docs/static/redoc/redoc.standalone.js.LICENSE.txt),
139
+ [Swagger UI](./fastapi_standalone_docs/static/swagger/), which is licensed under the
140
+ [Apache 2.0 License](./fastapi_standalone_docs/static/swagger/LICENSE), as well as parts
141
+ of [FastAPI](./fastapi_standalone_docs/static/fastapi) (the favicon), which is licensed
142
+ under the [MIT license](./fastapi_standalone_docs/static/fastapi/LICENSE).
143
+
@@ -0,0 +1,126 @@
1
+ # FastAPI Standalone Docs
2
+
3
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ioxiocom/fastapi-standalone-docs/publish.yaml)](https://github.com/ioxiocom/fastapi-standalone-docs/actions/workflows/publish.yaml)
4
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
5
+ [![PyPI](https://img.shields.io/pypi/v/fastapi-standalone-docs)](https://pypi.org/project/fastapi-standalone-docs/)
6
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-standalone-docs)](https://pypi.org/project/fastapi-standalone-docs/)
7
+ [![License: BSD 3-Clause](https://img.shields.io/pypi/l/fastapi-standalone-docs)](https://opensource.org/license/mit/)
8
+
9
+ Host FastAPI Swagger UI and ReDoc without any third party CDNs. Handy if you want to be
10
+ able to use the docs offline or want to avoid loading content from 3rd party CDNs due to
11
+ privacy reasons.
12
+
13
+ This library was heavily inspired by
14
+ [fastapi-offline-swagger-ui](https://github.com/ahmetoner/fastapi-offline-swagger-ui),
15
+ but we wanted a way to take it into use with less boilerplate code and also have it
16
+ hosted on PyPI.
17
+
18
+ ## Installation
19
+
20
+ The package is available on PyPI:
21
+
22
+ ```bash
23
+ pip install fastapi-standalone-docs
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ Import `StandaloneDocs` and pass in the FastAPI application to it:
29
+
30
+ ```python
31
+ from fastapi import FastAPI
32
+ from fastapi_standalone_docs import StandaloneDocs
33
+
34
+ app = FastAPI()
35
+ StandaloneDocs(app=app)
36
+ ```
37
+
38
+ The static files will by default be served from subpaths of the `docs_url` and
39
+ `redoc_url` as you've specified in your FastAPI app. The library doesn't make any
40
+ assumption on any particular path being reserved for the library. And if you disable
41
+ either of the two UIs, the corresponding files won't either be served unnecessarily.
42
+
43
+ This example would move the Swagger UI and the static files needed for it to the path
44
+ `/swagger` and disable the ReDoc interface and the static files needed by it:
45
+
46
+ ```python
47
+ from fastapi import FastAPI
48
+ from fastapi_standalone_docs import StandaloneDocs
49
+
50
+ app = FastAPI(docs_url="/swagger", redoc_url=None)
51
+ StandaloneDocs(app=app)
52
+ ```
53
+
54
+ By default, the library will use the FastAPI favicons, and they're served from
55
+ `{docs_url}/fastapi/favicon.png` and `{redoc_url}/fastapi/favicon.png`. You can use your
56
+ own favicons like this, which will also disable the FastAPI icons:
57
+
58
+ ```python
59
+ from fastapi import FastAPI
60
+ from fastapi_standalone_docs import StandaloneDocs
61
+
62
+ app = FastAPI()
63
+ StandaloneDocs(
64
+ app=app,
65
+ redoc_favicon_url="/favicon.png",
66
+ swagger_favicon_url="/favicon.png",
67
+ )
68
+ ```
69
+
70
+ To make the docs truly stand alone the Google Fonts are by default disabled (they are
71
+ used in the ReDoc interface). If you want to enable them just set
72
+ `with_google_fonts=True`, like this:
73
+
74
+ ```python
75
+ from fastapi import FastAPI
76
+ from fastapi_standalone_docs import StandaloneDocs
77
+
78
+ app = FastAPI()
79
+ StandaloneDocs(
80
+ app=app,
81
+ with_google_fonts=True,
82
+ )
83
+ ```
84
+
85
+ ## Development
86
+
87
+ PRs are welcome!
88
+
89
+ This project is using [Poetry](https://python-poetry.org/) and
90
+ [pre-commit](https://pre-commit.com/), so please ensure you've installed both. To set up
91
+ the project with them run:
92
+
93
+ ```bash
94
+ pre-commit install
95
+ poetry install
96
+ ```
97
+
98
+ Running tests locally:
99
+
100
+ ```bash
101
+ poetry run invoke test
102
+ ```
103
+
104
+ Updating the static files from the CDNs:
105
+
106
+ ```bash
107
+ poetry run update-fastapi-standalone-docs
108
+ ```
109
+
110
+ If you want to test using it locally in some other project, in that project run this
111
+ (make sure to adjust the path to the local repo):
112
+
113
+ ```bash
114
+ poetry add ../fastapi-standalone-docs --editable
115
+ ```
116
+
117
+ ## License
118
+
119
+ The code of this library itself is released under the [MIT license](./LICENSE). Please
120
+ however note that this library also contains parts of
121
+ [ReDoc](./fastapi_standalone_docs/static/redoc/), which is licensed under the
122
+ [MIT license](./fastapi_standalone_docs/static/redoc/redoc.standalone.js.LICENSE.txt),
123
+ [Swagger UI](./fastapi_standalone_docs/static/swagger/), which is licensed under the
124
+ [Apache 2.0 License](./fastapi_standalone_docs/static/swagger/LICENSE), as well as parts
125
+ of [FastAPI](./fastapi_standalone_docs/static/fastapi) (the favicon), which is licensed
126
+ under the [MIT license](./fastapi_standalone_docs/static/fastapi/LICENSE).
@@ -0,0 +1,2 @@
1
+ # flake8: noqa
2
+ from fastapi_standalone_docs.standalone_docs import StandaloneDocs
@@ -0,0 +1,59 @@
1
+ from pathlib import Path
2
+ from typing import List
3
+ from urllib.request import urlretrieve
4
+
5
+ import fastapi_standalone_docs
6
+
7
+ FASTAPI_FILES = [
8
+ "https://fastapi.tiangolo.com/img/favicon.png",
9
+ "https://raw.githubusercontent.com/tiangolo/fastapi/master/LICENSE",
10
+ ]
11
+
12
+ SWAGGER_FILES = [
13
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/LICENSE",
14
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css",
15
+ "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js",
16
+ ]
17
+
18
+ REDOC_FILES = [
19
+ "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js.LICENSE.txt",
20
+ "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
21
+ "https://cdn.redoc.ly/redoc/logo-mini.svg",
22
+ ]
23
+
24
+
25
+ def update_docs():
26
+ static_path = Path(fastapi_standalone_docs.__path__[0]) / "static"
27
+ fastapi_path = static_path / "fastapi"
28
+ swagger_path = static_path / "swagger"
29
+ redoc_path = static_path / "redoc"
30
+
31
+ fastapi_path.mkdir(parents=True, exist_ok=True)
32
+ swagger_path.mkdir(parents=True, exist_ok=True)
33
+ redoc_path.mkdir(parents=True, exist_ok=True)
34
+
35
+ download_files(fastapi_path, FASTAPI_FILES)
36
+ download_files(swagger_path, SWAGGER_FILES)
37
+ download_files(redoc_path, REDOC_FILES)
38
+
39
+ patch_redoc_logo(target=static_path / "redoc")
40
+
41
+
42
+ def download_files(destination: Path, urls: List[str]) -> None:
43
+ for url in urls:
44
+ filename = url.split("/")[-1]
45
+ urlretrieve(url, destination / filename)
46
+
47
+
48
+ def patch_redoc_logo(target: Path):
49
+ file = target / "redoc.standalone.js"
50
+ content = file.read_text(encoding="utf-8")
51
+ content = content.replace(
52
+ '"https://cdn.redoc.ly/redoc/logo-mini.svg"',
53
+ 'window.location.pathname+"/logo-mini.svg"',
54
+ )
55
+ file.write_text(content, encoding="utf-8")
56
+
57
+
58
+ if __name__ == "__main__":
59
+ update_docs()
@@ -0,0 +1,74 @@
1
+ from pathlib import Path
2
+ from typing import Optional
3
+
4
+ from fastapi import FastAPI, applications
5
+ from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
6
+ from fastapi.staticfiles import StaticFiles
7
+
8
+ import fastapi_standalone_docs
9
+
10
+
11
+ class StandaloneDocs:
12
+ def __init__(
13
+ self,
14
+ app: FastAPI,
15
+ with_google_fonts: bool = False,
16
+ swagger_favicon_url: Optional[str] = None,
17
+ redoc_favicon_url: Optional[str] = None,
18
+ ):
19
+ self.app = app
20
+ self.with_google_fonts = with_google_fonts
21
+ self.static_path = Path(fastapi_standalone_docs.__path__[0]) / "static"
22
+
23
+ self.patch_swagger(swagger_favicon_url)
24
+ self.patch_redoc(redoc_favicon_url)
25
+
26
+ def patch_swagger(self, swagger_favicon_url: Optional[str]):
27
+ if self.app.docs_url:
28
+ if not swagger_favicon_url:
29
+ swagger_favicon_url = self.app.docs_url + "/fastapi/favicon.png"
30
+ self.app.mount(
31
+ self.app.docs_url + "/fastapi/",
32
+ StaticFiles(directory=self.static_path / "fastapi"),
33
+ )
34
+
35
+ self.app.mount(
36
+ self.app.docs_url + "/",
37
+ StaticFiles(directory=self.static_path / "swagger"),
38
+ )
39
+
40
+ def patched_get_swagger_ui_html(*args, **kwargs):
41
+ return get_swagger_ui_html(
42
+ *args,
43
+ **kwargs,
44
+ swagger_favicon_url=swagger_favicon_url,
45
+ swagger_css_url=self.app.docs_url + "/swagger-ui.css",
46
+ swagger_js_url=self.app.docs_url + "/swagger-ui-bundle.js",
47
+ )
48
+
49
+ applications.get_swagger_ui_html = patched_get_swagger_ui_html
50
+
51
+ def patch_redoc(self, redoc_favicon_url: Optional[str]):
52
+ if self.app.redoc_url:
53
+ if not redoc_favicon_url:
54
+ redoc_favicon_url = self.app.redoc_url + "/fastapi/favicon.png"
55
+ self.app.mount(
56
+ self.app.redoc_url + "/fastapi/",
57
+ StaticFiles(directory=self.static_path / "fastapi"),
58
+ )
59
+
60
+ self.app.mount(
61
+ self.app.redoc_url + "/",
62
+ StaticFiles(directory=self.static_path / "redoc"),
63
+ )
64
+
65
+ def patched_get_redoc_html(*args, **kwargs):
66
+ return get_redoc_html(
67
+ *args,
68
+ **kwargs,
69
+ redoc_js_url=self.app.redoc_url + "/redoc.standalone.js",
70
+ redoc_favicon_url=redoc_favicon_url,
71
+ with_google_fonts=self.with_google_fonts,
72
+ )
73
+
74
+ applications.get_redoc_html = patched_get_redoc_html
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sebastián Ramírez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ <svg width="300" height="300" xmlns="http://www.w3.org/2000/svg"><path fill="#0044d4" d="M249.488 231.49a123.68 123.68 0 0 0-22.86-12.66 111.55 111.55 0 0 0-42.12-214.75h-171.8a8.18 8.18 0 0 0-5.78 14 7.48 7.48 0 0 0 5.78 2.48 95 95 0 1 1 0 190c-.83 0-1.38.27-2.21.27a8 8 0 0 0-6.33 7.71v1.11a7.69 7.69 0 0 0 7.71 7.7h5.52a92.93 92.93 0 0 1 50.66 17.62 95.33 95.33 0 0 1 34.14 45.43 8.27 8.27 0 0 0 7.7 5.52h171.8a7.76 7.76 0 0 0 6.61-3.58 8 8 0 0 0 1.09-7.42 109.06 109.06 0 0 0-39.91-53.43zm-158-37.16a111.62 111.62 0 0 0 32.76-78.75 110 110 0 0 0-32.76-78.74 105.91 105.91 0 0 0-20.37-16.24h113.39a95 95 0 1 1 0 190 3.55 3.55 0 0 0-1.65.27H70.798a109.06 109.06 0 0 0 20.65-16.54h.04zm-13.77 37.16c-1.92-1.37-4.13-2.75-6.33-4.13h117.8a94.79 94.79 0 0 1 80.12 52h-153.63a112 112 0 0 0-38-47.87h.04z" class="cls-1"/><path fill="#0044d4" d="M158.398 115.58a8.11 8.11 0 0 0 8.26 8.26h82.6a8.26 8.26 0 0 0 0-16.52h-82.6a8.29 8.29 0 0 0-8.26 8.26zM152.298 156.92h82.59a8.26 8.26 0 0 0 0-16.52h-82.59a8.11 8.11 0 0 0-8.26 8.26 8.28 8.28 0 0 0 8.26 8.26zM152.298 90.8h82.59a8.26 8.26 0 0 0 0-16.52h-82.59a8.11 8.11 0 0 0-8.26 8.26 8.46 8.46 0 0 0 8.26 8.26z" class="cls-1"/></svg>