In flask-restplus
, or alternatively flask
, I want to call my custom function which is called by POST
request, where POST method passes the parameter to a custom function, then function take a parameter and do calculation then returns the output to the server endpoint. I am not quite sure how to make this happen, but the whole picture of the process I kinda get it. Can anyone show me how to do this? Is there any way to do this in flask or flask-restplus
? any thought?
minimal flask app
here is the minimal flask app that I wrote:
from flask import Flask, jsonify
from flask_restplus import Api, Resource, fields, reqparse, inputs
app = Flask(__name__)
api = Api(app)
ns = api.namespace('ns')
payload = api.model('Payload', {
'num': fields.Integer(required=True)
})
@ns.route('/')
class AResource(Resource):
@ns.expect(payload)
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('num', type=str, required=True)
try: # Will raise an error if date can't be parsed.
args = parser.parse_args()
param = args['num] ## todo: how to pass params to my custom function
## output from custom function, where function would consum param
return jsonify(output)
except:
return None, 400
if __name__ == '__main__':
app.run(debug=True)
custom function
here is the custom function that POST method is going to call and trigger the function to run and return its output to server endpoint:
primest <- function(num){
p <- 2:num
i <- 1
while (p[i] <= sqrt(num)) {
p <- p[p %% p[i] != 0 | p==p[i]]
i <- i+1
}
p
}
update
whenever api post method is used, post method will pass the parameter to custom function then custom function will do a calculation and return its output as json object to the server endpoint. how to elaborate this process easier? any thought?
and I can call R function from python using this:
@ns.route('/')
class Rsession(Resource):
def get(self):
output = subprocess.call(['Rscript','--vanilla','-e','primest.R'])
return {'call function': output}
I got stuck how am I gonna pass and parse argument in order to call my custom function. How to make this happen in flask or flask-restplus? any possible thought? how can I call a function which is called by POST request? any idea?
as far as I understood your question correctly, you just made things complicated. instead of using arg_parse
, let's do like this:
from flask import Flask, jsonify, request
from flask_restplus import Api, Resource, fields
app = Flask(__name__)
api = Api(app)
ns = api.namespace('ns')
payload = api.model('Payload', {
'num': fields.Integer(required=True)
})
@api.route('/')
class AResource(Resource):
@ns.expect(payload)
def post(self):
param = request.json['num']
try:
res = primes_method1(param)
return jsonify(res)
except:
return None, 400
def primes_method1(n):
out = list()
for num in range(1, n+1):
prime = True
for i in range(2, num):
if (num % i == 0):
prime = False
if prime:
out.append(num)
return out
if __name__ == '__main__':
app.run(debug=True)
this solution is totally working fine for me, hope would resolve your doubt. If you want to call R function from python, subprocess.call()
would do the jobs. hope this is what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.