1

How can I use webargs or reqparse from flask_restful to pass a numpy array while making a POST call. I am able to pass arguments of other data types (string, int) with ease but numpy arrays get translated to unicode or throws an error.

I want to able to do something like:

from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('url', type=str)
parser.add_argument('id', type=str, required=True)
# Pass in numpy array
parser.add_argument('arr', type=numpy.array, required=True)

I also tried with webargs but haven't found a way to make it compatible with numpy arrays. I have this set up:

from webargs import fields
from webargs.flaskparser import use_args
my_args = {'url': fields.Str(),
           'id': fields.Str(required=True),
           'arr': fields.Field()} # not sure what to put here

Is there a way to efficiently pass in a numpy array using a flask restful API?

Edit: I've tried setting data type as List but get a new error indicating u"'List' object is not callable". I've changed:

# pass in numpy array
parser.add_argument('arr', type=fields.List(fields.Raw()))
2

1 Answer 1

-1

The documentation indicates some other data types than primitives. A numpy array can be interpreted as a list.

If you want to pass a numpy object to be able to use its methods, I think the parser will not work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.