Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created August 28, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescalam/a85a178fd257387b7140046a5ceea0ed to your computer and use it in GitHub Desktop.
Save jamescalam/a85a178fd257387b7140046a5ceea0ed to your computer and use it in GitHub Desktop.
Example snippet of Flask API POST method.
class Users(Resource):
def post(self):
parser = reqparse.RequestParser() # initialize
parser.add_argument('userId', required=True) # add args
parser.add_argument('name', required=True)
parser.add_argument('city', required=True)
args = parser.parse_args() # parse arguments to dictionary
# create new dataframe containing new values
new_data = pd.DataFrame({
'userId': args['userId'],
'name': args['name'],
'city': args['city'],
'locations': [[]]
})
# read our CSV
data = pd.read_csv('users.csv')
# add the newly provided values
data = data.append(new_data, ignore_index=True)
# save back to CSV
data.to_csv('users.csv', index=False)
return {'data': data.to_dict()}, 200 # return data with 200 OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment