微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FlaskRestPlus RESTAPI - 如何将文件附件和有效负载作为参数?

如何解决FlaskRestPlus RESTAPI - 如何将文件附件和有效负载作为参数?

我在使用 Flask RestPlus 时遇到了一些问题。我可以在 POST 请求中单独应用 json 有效负载文件上传没问题,但是一起,它似乎不起作用。有人可以请告知问题是什么吗?

我得到的错误是:

{
    "errors": {
        "": "None is not of type 'object'"
    },"message": "Input payload validation Failed"
}

这是我的代码示例:

class LdamodelDto:
    api = Namespace('Ldamodel',description='apply topic modeling')
    topic_modeling = api.model('topic_modeling',{
        'column_name': fields.String(required=True,description='name of column that you want to analyze'),'number_clusters': fields.Integer(required=True,description='number of categorizations you expect')
    })

api = LdamodelDto.api
_topic_modeling = LdamodelDto.topic_modeling

file_upload = reqparse.RequestParser()
file_upload.add_argument('file',type=FileStorage,location='files',required=True,help='data file to be uploaded'
                         )

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.',1)[1].lower() in allowed_file_types

@api.route('/topic_modeling')
class TopicModeling(Resource):

    @api.expect(_topic_modeling,file_upload,validate=True)
    @api.doc('return CSV file containing LDA model output and HTML for LDA visualization')
    def post(self):
        """run LDA model on text data and return attachments files"""
        for k,v in request.json.items():
             if not v:
                abort(400,f"{k} cannot be null")

        args = file_upload.parse_args()
        file = args['file']

        if file.filename == '':
            abort(400,'No file selected for uploading')

        if allowed_file(file.filename):
            filename = secure_filename(file.filename)
            print(pd.read_csv(file))
            return filename

        else:
            return abort(400,'Allowed file types are %s' % ','.join(allowed_file_types))

以下是我尝试执行 api 调用的示例:

payload = {
    'column_name': 'test','number_clusters': 5
}

r = requests.post('http://127.0.0.1:5000/Ldamodel/topic_modeling',json=payload,files={
                      'file': open('../topics_dataset.csv')}
                  )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?