Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DODS page cannot access CSS and JS #4307

Closed
9 tasks done
fz-gaojian opened this issue Dec 20, 2021 · 3 comments
Closed
9 tasks done

DODS page cannot access CSS and JS #4307

fz-gaojian opened this issue Dec 20, 2021 · 3 comments
Labels
question Question or problem question-migrate

Comments

@fz-gaojian
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

Description

Request URL: https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css
Request URL: https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js
Because the above two resources cannot be accessed, there is no display in the DOCS page.

Operating System

Linux

Operating System Details

No response

FastAPI Version

fastapi==0.54.1

Python Version

python 3.7.3

Additional Context

No response

@fz-gaojian fz-gaojian added the question Question or problem label Dec 20, 2021
@SangYuming
Copy link

SangYuming commented Dec 20, 2021

My solution is to download Swagger-ui, which must include favicon.png、swagger-ui.css、swagger-ui-bundle.js,Then create a new static folder

Provides static file services:

from fastapi import applications
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html

app.mount("/static", StaticFiles(directory="./static"), name="static")
def swagger_monkey_patch(*args, **kwargs):
    return get_swagger_ui_html(
        *args,
        **kwargs,
        swagger_js_url="/static/swagger-ui/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui/swagger-ui.css",
        swagger_favicon_url="/static/swagger-ui/favicon.png"
    )
applications.get_swagger_ui_html = swagger_monkey_patch

@liushuangdan
Copy link

liushuangdan commented Dec 20, 2021

Perhaps downloading Swagger-UI files will solve this problem.
I did like that。My API documentation worked。

if static_root:
    app.mount('/static', StaticFiles(directory=static_root), name='static')
else:
    app.mount('/static', StaticFiles(directory=f"{root}/src/static"), name='static')


def swagger_monkey_patch(*args, **kwargs):
    """
    Wrap the function which is generating the HTML for the /docs endpoint and
    overwrite the default values for the swagger js and css.
    """
    return get_swagger_ui_html(
        *args, **kwargs,
        swagger_js_url="/static/dist/swagger-ui-bundle.js",
        swagger_css_url="/static/dist/swagger-ui.css")


# Actual monkey patch
applications.get_swagger_ui_html = swagger_monkey_patch

@fz-gaojian
Copy link
Author

It is normal now, thank you

@tiangolo tiangolo reopened this Feb 27, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6475 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants