Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Last active September 21, 2020 07:28
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/cca7a08699ed892457be55a69c5c52a8 to your computer and use it in GitHub Desktop.
Save jamescalam/cca7a08699ed892457be55a69c5c52a8 to your computer and use it in GitHub Desktop.
Example snippet showing additional logic added to Flask API POST method.
...
# read our CSV
data = pd.read_csv('users.csv')
if args['userId'] in list(data['userId']):
return {
'message': f"'{args['userId']}' already exists."
}, 401
else:
# create new dataframe containing new values
new_data = pd.DataFrame({
'userId': args['userId'],
'name': args['name'],
'city': args['city'],
'locations': [[]]
})
# add the newly provided values
data = data.append(new_data, ignore_index=True)
data.to_csv('users.csv', index=False) # save back to CSV
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