0

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

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

3 Comments

How so? Do you get an error? I changed it so that it will accept any filename, not just myfile.csv.
how can you show an input form named 'file' in the web interface and link it to the correct query argument?
is there a way to insert parameters in request.args (in code, not in the request) ?

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.