Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask_app.py #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion merhaba.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
#!/usr/bin/python
from flask import Flask, request
from flask_restful import Api, Resource
import pandas as pd

print "Merhaba Dunya"
app = Flask(__name__)
api = Api(app)

class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
data = data.to_dict('records')
return {'data' : data}, 200

def post(self):
json = request.get_json()
req_data = pd.DataFrame({
'name' : [json['name']],
'age' : [json['age']],
'city' : [json['city']]
})
data = pd.read_csv('users.csv')
data = pd.concat([data, req_data], ignore_index=True)
data.to_csv('users.csv', index=False)
return {'message' : 'Record successfully added.'}, 200

def delete(self):
name = request.args['name']
data = pd.read_csv('users.csv')

if name in data['name'].values:
data = data[data['name'] != name]
data.to_csv('users.csv', index=False)
return {'message': 'Record successfully deleted.'}, 200
else:
return {'message': 'Record not found.'}, 404

class Cities(Resource):
def get(self):
data = pd.read_csv('users.csv',usecols=[2])
data = data.to_dict('records')
return {'data' : data}, 200

class Name(Resource):
def get(self,name):
data = pd.read_csv('users.csv')
data = data.to_dict('records')
for entry in data:
if entry['name'] == name :
return {'data' : entry}, 200
return {'message' : 'No entry found with this name !'}, 404


# Add URL endpoints
api.add_resource(Users, '/users')
api.add_resource(Cities, '/cities')
api.add_resource(Name, '/<string:name>')


if __name__ == '__main__':
# app.run(host="0.0.0.0", port=5000)
app.run()