Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
data = data.to_dict('records')
return {'data' : data}, 200
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('name', required=True)
parser.add_argument('age', required=True)
parser.add_argument('city', required=True)
args = parser.parse_args()
data = pd.read_csv('users.csv')
new_data = pd.DataFrame({
'name' : [args['name']],
'age' : [args['age']],
'city' : [args['city']]
})
data = data.append(new_data, ignore_index = True)
data.to_csv('users.csv', index=False)
return {'data' : new_data.to_dict('records')}, 201
def delete(self):
parser = reqparse.RequestParser()
parser.add_argument('name', required=True)
args = parser.parse_args()
data = pd.read_csv('users.csv')
data = data[data['name'] != args['name']]
data.to_csv('users.csv', index=False)
return {'message' : 'Record deleted successfully.'}, 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment