9

I'm building an API, using Flask and flask-restful and flask-swagger-ui. I have now modified the project structure and now I can no longer access the project's swagger.json file.

Based on the package documentation flask-swagger-ui, you would only need to change the parameter API_URL to the correct path. But even when entering relative path or full path, I can no longer access the file.

enter image description here

My Code:

from flask import Flask, jsonify from flask_migrate import Migrate from flask_restful import Api from flask_swagger_ui import get_swaggerui_blueprint def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) api = Api(app, prefix="/api/v1") '''swagger specific''' SWAGGER_URL = '/api/v1/docs' # API_URL = 'templates/swagger.json' API_URL = 'app/templates/docs/swagger.json' SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint( SWAGGER_URL, API_URL, config={ 'app_name': "My Rest App" } ) app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL) db.init_app(app) Migrate(app, db) return app 

My Tree Structure:

├── API │   ├── app │   │   ├── __init__.py │   │   ├── models │   │   │   ├── __init__.py │   │   │   ├── db.py │   │   │   └── db2.py │   │   ├── routes │   │   │   ├── __init__.py │   │   │   ├── resources.py │   │   └── templates │   │   └── docs │   │   └── swagger.json │   ├── app.db │   ├── config.py │   ├── main.py │   ├── migrations │   ├── requeriments │   └── tests └── README.md 

I need help, to understand the problem of the path to the file and thus correct the problem.

2
  • Did anyone ever figure this out? Commented Nov 14, 2019 at 19:44
  • @vic.py Did you find any solution ? Commented Dec 15, 2020 at 6:25

3 Answers 3

8

I believe that its a restriction on flask's end, but it seems like you must place static files in a folder explicitly named static/ at the root of your flask app.

Try changing

API_URL = 'app/templates/docs/swagger.json' 

to

API_URL = '/static/swagger.json' 

Then create a static/ folder in API/app/, and move your json into it. Like so: API/app/static/swagger.json

If that doesn't work, make sure you have the static/ folder in the correct directory, try print(app.root_path) after defining your flask app variable in order to see what flask considered the root path to be.

Sign up to request clarification or add additional context in comments.

1 Comment

It's also necessary to include the leading '/' in the API_URL
2

We can't provide any custom file path for swagger.json.

But there is one catch which we can do if you require. I was facing the same issue where I need to provide different prefix for swagger URL and swagger.json. If we just want to serve swagger.json file from a different prefix instead of '/static/swagger.json':

Then make changes as below:

APP = Flask(__name__, static_url_path = '/my-prefix/static') API_URL = '/my-prefix/static/swagger.json' 

Comments

0

For me "root_path" solved the problem,

Lets say I have the following path:

"C:\script\mycode\src\swagger.json"

def resource_path(relative_path): base_path = getattr( sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) return os.path.join(base_path, relative_path) new_path = resource_path("swagger.json") new_path = new_path.replace("swagger.json", "") app = Flask(__name__, root_path=new_path, static_folder="") print("app.root_path ===================>>",app.root_path) #prints: "C:\script\mycode\src\" app.register_blueprint(get_swaggerui_blueprint( '/api/swagger', "\\swagger.json", #put starting slash even the app.root_path ends with #slash. and check in "app.root_path" if additional path #need to be added with "\\swagger.json" config=apiConfig), url_prefix=apiPrefixV1+'swagger', name='v1') 

Note: For me resource path is used for pyinstaller build. For local test also works fine!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.