Skip to content

Instantly share code, notes, and snippets.

@nithyadurai87
Created November 23, 2020 16:52
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 nithyadurai87/5617efd02f1e2b3aaef934e11ddac6bb to your computer and use it in GitHub Desktop.
Save nithyadurai87/5617efd02f1e2b3aaef934e11ddac6bb to your computer and use it in GitHub Desktop.
from flask import Flask, request, jsonify
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient('mongodb',27017)
db = client.forest
collection = db.flowers
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
data = request.get_json()
collection.insert_one(data).inserted_id
#with open('./tmpfiles/data.log','a') as f:
#f.write(str(data)+'/n')
return ('', 204)
if request.method == 'GET':
data = collection.find()
response = []
for i in data:
i['_id'] = str(i['_id'])
response.append(i)
return jsonify(response)
if __name__ == '__main__':
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment