# Import Flask, Jsonify and Requests from flask import Flask, jsonify, request # Create the web applicatiion via Flask app = Flask(__name__) # Existing To-Do List # it's easiest to manipulate if this is a dict where key is the id and value is the todo todos = { 1: "Buy Hitman 3", 2: "Play Saints Row IV with friends (Xbox)", 3: "Watch 'Last Kids on Earth'", 4: "Read 'Mastery' by Robert Greene" } # Get, Post and Delete Requests @app.route("/todo", methods=["GET", "POST"]) def todo(): # Post Request, post a todo to exisiting list if request.method == 'POST': new_id = max(todos.keys()) + 1 # generates a new ID todos[new_id] = request.json['todo'] # puts the todo into dict return jsonify({'status': 'created', 'id': new_id}) # returns status # Get Request, Returning a list of all todos and their IDs elif request.method == 'GET': return jsonify(todos) # Delete Request, deletes a single todo by ID @app.route("/todo/", methods=["GET", "DELETE"]) def get_todo(id_): if request.method == 'DELETE': del todos[id_] # deletes from todos dict return jsonify({"status": "deleted"}) # success, let's return status # Get Request, Returning the data of a single todo by ID elif request.method == "GET": # return by ID or delete return jsonify({id_: todos[id_]}) # gets object by ID if __name__ == "__main__": app.run(port=5000, debug=True)