Flask : [documentation]
Rest API with Python and Flask : REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. REST API stands for Restful API, which allows the integration of applications or interaction with RESTful web services. New : FastAPI and use POSTMAN to test API. We know that there are six commonly used HTTP request methods, which are : GET
, POST
, PUT
, DELETE
, PATCH
, HEAD
.
@app.route('/', methods=['GET'])
@app.route('/', methods=['POST'])
@app.route('/', methods=['DELETE'])
@app.route('/', methods=['PUT'])
I have some experience with Flask and did few projects (example-work). I recently gave a workshop at AI-Makerspace, Digital Product School, Unternehmer Technische Universität München on flask, maps and ML. Tech Stack : [ Flask, JavaScript, Bootstrap, Mapbox, MongoDB, PyTorch ] and here is the code repository.
code ✔️ |
code ✔️ |
code ✔️ |
code ✔️ |
---|---|---|---|
code ✔️ |
code ✔️ |
code ✔️ |
Deployment of Flask server : PythonAnywhere, Heroku, Digital Ocean, Virtual Machine from Google Cloud : Google App Engine, Google Cloud Run, AWS : Elastic Beanstalk, Azure IIS etc.
import json
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET'])
def query_records():
name = request.args.get('name')
print name
with open('/tmp/data.txt', 'r') as f:
data = f.read()
records = json.loads(data)
for record in records:
if record['name'] == name:
return jsonify(record)
return jsonify({'error': 'data not found'})
@app.route('/', methods=['PUT'])
def create_record():
record = json.loads(request.data)
with open('/tmp/data.txt', 'r') as f:
data = f.read()
if not data:
records = [record]
else:
records = json.loads(data)
records.append(record)
with open('/tmp/data.txt', 'w') as f:
f.write(json.dumps(records, indent=2))
return jsonify(record)
@app.route('/', methods=['POST'])
def update_record():
record = json.loads(request.data)
new_records = []
with open('/tmp/data.txt', 'r') as f:
data = f.read()
records = json.loads(data)
for r in records:
if r['name'] == record['name']:
r['email'] = record['email']
new_records.append(r)
with open('/tmp/data.txt', 'w') as f:
f.write(json.dumps(new_records, indent=2))
return jsonify(record)
@app.route('/', methods=['DELETE'])
def delte_record():
record = json.loads(request.data)
new_records = []
with open('/tmp/data.txt', 'r') as f:
data = f.read()
records = json.loads(data)
for r in records:
if r['name'] == record['name']:
continue
new_records.append(r)
with open('/tmp/data.txt', 'w') as f:
f.write(json.dumps(new_records, indent=2))
return jsonify(record)
app.run(debug=True)
Resources: Flask Mega Tutrial, Build a blog platform with Flask: writing and showing posts, Flask documentation : a minimal application, The Ultimate Flask Course, REST APIs with Flask and Python, Flask RESTful, Python REST API Tutorial - Building a Flask REST API, Postman API Test Automation.