Api端点未在Flask Restx中注册



我一直在学习一些教程(基本示例(,试图用Flask-Restx创建一个RESTful API。然而,当我尝试使用更复杂的项目结构注册端点时(遵循此示例(,不会注册API端点。我定义了一个端点的名称空间,并试图在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]

APIinit.py如下所示:

from flask_restx import Api

api = Api(version='0.1', title='Backend API', description='Backend API for sticker generation')

以及我想要注册的端点:

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"
[....]

虽然@app.route('/'(在app.py中有效,但@ns.route('/'(仍然不成功,我不知道为什么。

回答我自己的问题:

事实证明,从PyCharm运行app.py并不是通过main()运行,或者配置错误,而是似乎只是从初始app = Flask(__name__)启动Flask服务器。因此,永远不会调用configure_app((或init_app(。将项目布局转换为与Flask的预期布局更相似的布局在一定程度上解决了这一问题,从而使预期的端点发挥作用。

相关内容

  • 没有找到相关文章

最新更新