4

I have the following flask code

from flask import Flask, jsonify, request                                                                                                                                                                          
from webargs import fields                                                                                                                                                                                         
from sqlalchemy import create_engine, MetaData, Table, select                                                                                                                                                      
from flask_cors import CORS                                                                                                                                                                                                                                                                                                                                                                                                           
engine = create_engine('...')                                                                                                                                                                                                                                                                                                                                            
////                                                                                                                                                                                                                                                                                                                                                                                                  
app = Flask(__name__)                                                                                                                                                                                                                                                                                                                                                                                                                 
cors = CORS(app, resources={r"/list*": {"origins": "*"}})                                                                                                                                                                                                                                                                                                                                                                             

@app.route('/list-vessels', methods=['GET'])                                                                                                                                                                       
def list_vessels():                                                                                                                                                                                                    
vessel_list = []                                                                                                                                                                                                   
s = select([vesseldetail.c.Vessel])                                                                                                                                                                                
rp = connection.execute(s)                                                                                                                                                                                         
for row in rp:                                                                                                                                                                                                         
    vessel_list.extend(list(row))                                                                                                                                                                                  
return jsonify(vessel_list)                                                                                                                                                                                                                                                                                                                                                                                                       

@app.route('/list-keydates', methods=['GET'])                                                                                                          
def list_keydates(vesselname):                                                                                                                                                                                         
    vesselname = request.args['vesselname']      
    intervention_list = []                                                                                                                                                                                             
    keydate_list = []                                                                                                                                                                                                  
    s = select([keydate.c.Intervention, keydate.c.Date])                                                                                                                                                               
    s = s.where(keydate.c.VESSEL==vesselname)                                                                                                                                                                          
    rp = connection.execute(s)                                                                                                                                                                                         
    for row in rp:                                                                                                                                                                                                         
        intervention_list.append((row[0]))                                                                                                                                                                                 
        keydate_list.append((row[1]))                                                                                                                                                                                  
    return jsonify({"keydates": keydate_list, "intervention": intervention_list})

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

The second endpoint 'list-keydates' returns a TypeError: list_keydates() missing 1 required positional argument: 'vesselname' even when I pass a vesselname in the query string. What am I doing wrong?

spheroid
  • 154
  • 1
  • 4
  • 14

2 Answers2

6

The argument vesselname is missing when defining the url, try the following:

...
@app.route('/list-keydates/<vesselname>', methods=['GET'])                                                                                                          
def list_keydates(vesselname):
    intervention_list = []                                                                                                                                                                                             
    keydate_list = []                                                                                                                                                                                                  
    s = select([keydate.c.Intervention, keydate.c.Date])                                                                                                                                                               
    s = s.where(keydate.c.VESSEL==vesselname)  
    ...

url should be http://.../list-keydates/name-of-the-vessel

This way you don't have to get the vesselname from request args, its value will be placed in the variable function.

Hope it suits you well

Kenny Aires
  • 1,338
  • 12
  • 16
  • Hi. This works, thanks! But I would still like to know why the other thing doesn't. How would I need to edit the code so I could use request args? – spheroid Mar 24 '20 at 14:11
  • 1
    No problem! To use request args, you could remove the argument from your function - e.g: `def list_keydates():` - then call the url in the following way: `http://.../list-keydates?vesselname=name-of-the-vessel`. – Kenny Aires Mar 24 '20 at 14:17
2

you can also do this

when there is a query string in the URL

http://localhost:5000/list-keydates?vesselname=yourdata

@app.route('/list-keydates', methods=['GET'])                                                                                                          
def list_keydates():
    print(request.args['vesselname']                                                                                                                                                                        
    vesselname = request.args['vesselname']