Search code examples
pythonflaskurlparse

Adding parameter values to a url in flask python


I have the code for the following url:http://localhost/summary/myfile.csv I want the url to look like this:http://localhost/summary?file=myfile.csv

The code is to be written in flask.

My code for the first url is as follows:

@app.route('/summary/<filename>',methods = ['GET'])
def api_summary(filename):
    url = 'C:\\Users\\Desktop\\myproject\\'
    if os.path.exists(url + filename):
        data = pandas.read_csv( url + filename)
        Numeric_Summary = data.describe().to_dict()
        resp = jsonify(Numeric_Summary)
        resp.status_code = 200
        return resp

Solution

  • You would need to add another route and parse the query string:

    from flask import request
    
    @app.route('/summary',methods = ['GET'])
    def api_summary_2():
        filename = request.args.get('file', None)
        if filename is None:
            abort(404)
        else:
            return api_summary(filename)