Search code examples
pythonflaskflask-restplus

flask restful: how do I pass python function as a parameter to trigger GET request?


in flask-restplus, I want to pass system call object: subprocess.call() to GET request to print out R session information on server endpoint. To do so, I think I need to add system call object to parser, which I hope api endpoint print out R session information. However, when I hit the code, I didn't see any printed session info on server endpoint. How can I correctly pass system.call() to GET request in order to print something out on endpoint? any idea?

my attempt:

here is the system call which can print out R session on python IDE console.

sess = subprocess.call(['C:/Program Files/R/R-3.6.3/bin/Rscript','--vanilla','-e','sessionInfo()'])

my attempt to pass sess to GET request:

from flask import Flask, jsonify
from flask_restplus import Api, Resource, fields, reqparse, inputs
import subprocess

app = Flask(__name__)
api = Api(app)
ns = api.namespace('sess')

sess = subprocess.call(['C:/Program Files/R/R-3.6.3/bin/Rscript','--vanilla','-e','sessionInfo()'])

@ns.route('/')
class AResource(Resource):
    def get(self):
        parser = reqparse.RequestParse()
        parser.add_argument(sess, type= str, required=True)
        #return parser.parse_args()
        try:
            args = parser.parse_args()
            return jsonify(args)
        except:
            return None, 400

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

when I ran the code, I can't see any printed session information on the server endpoint. I think something wrong when I pass sess object to GET request. How can I pass custom python function or object as a parameter to GET request in order to print out something on the server endpoint? any idea?

I am newbie to flask-restplus, so maybe I should not pass sess to GET request like above. I humbly request for possible help. Thanks


Solution

  • Just to be clear, without flask you should be getting at least the output one.

    For example running a dummy script:

    from flask import Flask
    from flask_restplus import Api
    import subprocess
    
    app = Flask(__name__)
    api = Api(app)
    
    sess = subprocess.call(["echo","I am getting this output!"])
    

    would output

    I am getting this output!
    

    How to call subprocess.call in every get request

    1. Simply move the subprocess.call to the get request.
    2. Be careful to not create your API endpoint on '/' as flask_restplus uses it by default as the api reference page, so it would cause confusion. so make sure to use an endpoint like '/hello'

    Example code

    from flask import Flask
    from flask_restplus import Resource, Api
    from flask_restplus import Api
    import subprocess
    import os
    
    app = Flask(__name__)
    api = Api(app)
    
    @api.route('/hello')
    class HelloWorld(Resource):
        def get(self):
            subprocess.call(["echo","I am getting this output!"])
            subprocess.call(['Rscript','--vanilla','-e','sessionInfo()'])
            return {'hello123': 'world'}
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    When I run the app in on terminal and request http://localhost:5000/hello on my browser I see this output on my terminal

    I am getting this output!
    R version 3.6.3 (2020-02-29)
    Platform: x86_64-apple-darwin18.7.0 (64-bit)
    Running under: macOS Mojave 10.14.5
    
    Matrix products: default
    BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.9/lib/libopenblasp-r0.3.9.dylib
    
    locale:
    [1] en_IE.UTF-8/en_IE.UTF-8/en_IE.UTF-8/C/en_IE.UTF-8/en_IE.UTF-8
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base
    
    loaded via a namespace (and not attached):
    [1] compiler_3.6.3
    127.0.0.1 - - [22/Apr/2020 15:01:16] "GET /hello HTTP/1.1" 200 -