[英]Api endpoints do not register in Flask-Restx
I've been following some tutorials( basic examples ), trying to create a RESTful API with Flask-Restx.我一直在学习一些教程( 基本示例),尝试使用 Flask-Restx 创建一个 RESTful API。 However, when I try to register endpoints with a more complex project structure( following this example ), no API endpoints get registered.
但是,当我尝试使用更复杂的项目结构注册端点时( 按照此示例),没有 API 端点被注册。 I defined the namespace of an endpoint and tried to register that in app.py:
我定义了端点的命名空间并尝试在 app.py 中注册它:
from flask import Flask, Blueprint, url_for, jsonify
from flask_login import LoginManager
import settings
from database import db
from BE.api.__init__ import api
from BE.api.endpoints.images import ns as image_api
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
#flask_restx API supports prior flask_restplus api
def configure_app(app):
app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_EXPANSION
app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VAL
app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
app.config['SQLALCHEMY_DATABASE_URI'] = settings.SQLALCHEMY_DATABASE_URI
app.config['UPLOAD_FOLDER'] = settings.UPLOAD_FOLDER
app.config['MAX_UPLOAD_LENGTH'] = 24 * 1024 * 1024 # limits filesize to 24 mb
def init_app(app):
configure_app(app)
api.add_namespace(image_api)
app.register_blueprint(api, url_prefix='/api/1')
api.init_app(app)
db.init_app(app)
[....main etc]
The API init .py looks as follows: API init .py 如下所示:
from flask_restx import Api
api = Api(version='0.1', title='Backend API', description='Backend API for sticker generation')
and the endpoint I want to register:和我要注册的端点:
from flask import request, flash, Blueprint, redirect, send_file
from flask_restx import Resource, Namespace
from BE.database import db as database
from BE.database.models import Image as dbImage
from BE.api.api_definition import image
[....]
ns = Namespace('image', path='/image', description='Available Ops')
@ns.route('/test')
class TestClass(Resource):
def get(self):
return "Hello World"
[....]
While @app.route('/') works in app.py, the @ns.route('/') remains unsuccessful and I'm at a loss as to why.虽然 @app.route('/') 在 app.py 中工作,但 @ns.route('/') 仍然不成功,我不知道为什么。
Answering my own question:回答我自己的问题:
Turns out that running app.py from PyCharm does not run via main()
or was configured in a wrong way, but rather appears to just launch the Flask server from the initial app = Flask(__name__)
.事实证明,从 PyCharm 运行 app.py不会通过
main()
运行或配置错误,而是似乎只是从初始app = Flask(__name__)
启动 Flask 服务器。 Therefore configure_app() or init_app() never get called.因此 configure_app() 或 init_app() 永远不会被调用。 Transforming the project layout to something more closely resembling Flask's intended layout resolves that in part, resulting in the intended endpoints to work.
将项目布局转换为更类似于Flask 的预期布局的东西可以部分解决这个问题,从而使预期的端点工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.