我运行这个示例:https://flask-restx.readthedocs.io/en/latest/example.html (A Python REST-API with Flask-RESTX)
代码片段
app = Flask(__name__)
api = Api(app, ...)
ns = api.namespace('todos', ...)
@ns.route('/')
...
@ns.route('/<int:id>')
...结果
我得到了REST-API的以下URL:
http://127.0.0.1:5000 -> Swagger文档
http://127.0.0.1:5000/swagger.json
http://127.0.0.1:5000/todos/
http://127.0.0.1:5000/todos/{id}
问题
我想用Javascript实现一个Webclient,它可以通过以下URL访问:
http://127.0.0.1:5000 -> index.html
http://127.0.0.1:5000/style.css
http://127.0.0.1:5000/app.js
REST-API的URL应更改为:
http://127.0.0.1:5000/api -> Swagger文档
http://127.0.0.1:5000/api/swagger.json
http://127.0.0.1:5000/api/todos/
http://127.0.0.1:5000/api/todos/{id}
如何扩展示例以生成所需的URL?
发布于 2021-01-18 08:41:20
我不确定你现在是否得到了答案。我经常克隆下面的github项目作为flask restx样板。它被设计为rest样板,以帮助您启动和运行。
https://github.com/cosmic-byte/flask-restplus-boilerplate
可以将模板和静态文件添加到应用程序和控制器中,以承载页面。为静态页面添加了以下文档。每一个这应该是为了让你运行一个完整的网站
发布于 2021-02-16 21:34:28
您可以使用flask.Blueprint包装flask-restx应用程序,以便在特定的url_path下移动swagger逻辑。
以下示例显示了如何实现此目标:
import flask
import flask_restx
APP = flask.Flask("my-app")
api_bp = flask.Blueprint("api", __name__, url_prefix="/api")
API = flask_restx.Api(api_bp)
APP.register_blueprint(api_bp)
NAMESPACE = API.namespace("todos")
@NAMESPACE.route("/")
class TODOSAPI(flask_restx.Resource):
def get(self):
return ['todo-1', 'todo-2']https://stackoverflow.com/questions/65562035
复制相似问题