0

I have built an api which accepts any filepath passed to it in the url and displays it in the webpage. Example: http://12.389.798.220:5000/C:/MYES/ALL.pdf

The webpage will display

C:/MYES/ALL.pdf

Now I want to take this output and pass it to the function 'convert' in my code which needs a filepath to process a file and return an output.

Code till now:

app = Flask(__name__)
api = Api(app)   
class ectdtext(Resource):
    def get(self, result):
        return {'data': ectd.convert(result)}

categories=convert('/FILES/5cf9-5b67-45ef8-9DD69c-ae571431c665.pdf')   
@app.route('/')
def returnResult():
  return categories



@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_dir(path):
    return path



if __name__ == '__main__':
     app.run(host="0.0.0.0", port=5000)

So, the line

categories=convert('/FILES/5cf9-5b67-45ef8-9DD69c-ae571431c665.pdf)

was previously hardcoded. Now I want to pass the path entered in the webpage to be passed as a parameter to this convert function for each new request. How can I solve this?

1 Answers1

1

Just call the needed function within the respective route:

...
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_dir(path):
    categories = convert(path)
    return categories
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I am trying with the same and to test this, I am entering example: 12.334.198.190:5000/home/files/xyx.pdf and in my linux vm the address of the file is /home/files/xyz.pdf so the error is cannot find file home/files/xyz.pdf. I think the '/' before home is not being picked up and thus the error. Any idea how to fix this? – sehwagbiltu Jul 13 '19 at 21:06