0b01001001 / spectree

API spec validator and OpenAPI document generator for Python web frameworks.
https://0b01001001.github.io/spectree/
Apache License 2.0
316 stars 74 forks source link

[Question] Is it possible to load the Swagger UI offline? #261

Open Joseda8 opened 1 year ago

Joseda8 commented 1 year ago

The service I'm making runs on the localhost with not necessarily access to Internet. When I open the Swagger endpoint in the browser I can see the next messages in the console:

Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js”. swagger:36:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-standalone-preset.js”. swagger:37:1

So I want to know... How to load the Swagger interface offline?

kemingy commented 1 year ago

Thanks for asking. I think it's possible if we package the related frontend source code into the Python package.

Does that mean you can access internal PyPI but cannot access other websites?

jaykv commented 1 year ago

You can pair spectree with https://github.com/sveint/flask-swagger-ui for now. Not a perfect solution, but an easy workaround.

Here's a quick example: https://gist.github.com/jaykv/cd9afb91fa5a2fafc206345b7dd4b984

alexted commented 1 year ago

We also use SpecTree in our closed perimeter where there is no internet access! We take all the libraries for our apps from JFrog Artifactory or Nexus Repository (depending on the project/team), which have internet access, or rather only to global PyPi.

cardin commented 1 year ago

A very hackish way is to overwrite spectree/page.py before SpecTree is instantiated.

from spectree.page import DEFAULT_PAGE_TEMPLATES

app = Flask(...)

def _rewrite_cdn():
   DEFAULT_PAGE_TEMPLATES["redoc"] = DEFAULT_PAGE_TEMPLATES["redoc"].replace("https://cdn.jsdelivr.net/npm/redoc@next/bundles/", "localhost/assets")

_rewrite_cdn()
spec = SpecTree("flask")
spec.register(app)
alexted commented 7 months ago

Is there any news on this issue?

kemingy commented 7 months ago

Is there any news on this issue?

Similar to @cardin's comment. You can pack all the swagger related HTML/CSS/JS into one string.

Assume you already done it, you can use the code below:

from spectree import SpecTree

spec = SpecTree(page_templates={"swagger": YOUR_OFFLINE_SWAGGER_HTML_STRING})