带有Swagger的Python Flask Rest API-如何发布自定义模型

问题描述

我正在尝试使用Swagger构建Flask Rest API,该API需要2个输入参数(BodyTemperature和CoughSeverity)并生成输出JSON。

如果我通过URL提供2个参数,则代码可以正常工作。我的代码看起来像这样。

from flask import Flask
from flask_restplus import Resource,Api,fields

app = Flask(__name__)
api = Api(app = app,version = "1.0",title = "Patient Predictor",description = "Takes patiens symptoms as input and provides a estimated health condition of the patient")

name_space = api.namespace('PatientCondition',description='Get patient condition by symptoms')

@name_space.route("/GetPatientCondition/<float:BodyTemp>/<int:CoughSeverity>")

class PatientCondition(Resource):

def post(self,BodyTemp,CoughSeverity):
    
    return {
        "status": "Success","PatientCondition": "Patient temp is " + str(BodyTemp),"CoughSeverity": "Patient cough severity is " + str(CoughSeverity)
    }

if __name__ == '__main__':
    app.run()

现在的问题是现实生活中输入参数的数量会更多,所以我想通过自定义模型传递它们,而不是通过URL顺序传递它们。我为此创建了一个模型。

PatientSymptomModel = api.model('PatientSymptom',{
          'BodyTemp': fields.Float(required = True,description="Body Temparature of the person"),'CoughSeverity': fields.Float(required = True,description="Cough Severity of the person")
      }
    )

但是我不确定如何在POST方法中使用此模型,以便可以在svagger UI中通过JSON输入模型。任何帮助将不胜枚举。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)