Skip to content

Instantly share code, notes, and snippets.

@pathunstrom
Created Aug 9, 2018
Embed
What would you like to do?
API Demo
from functools import partial
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import request
app = Flask(__name__)
MESSAGE = 'Hello World!'
@app.route('/', methods=["POST", "GET"])
def hello_world():
if request.method == "GET":
if request.headers.get("accept") == "application/json":
response_function = jsonify
else:
response_function = lambda x: render_template("index.html", **x)
elif request.method == "POST":
global MESSAGE
MESSAGE = request.json["message"]
response_function = jsonify
else:
raise Exception
context = {"message": MESSAGE}
return response_function(context)
@app.route("/clone", methods=["POST", "GET"])
def clone_the_world():
if request.method == "POST":
global MESSAGE
MESSAGE = request.json["message"]
context = {"message": MESSAGE}
if request.headers.get("accept") == "application/json":
response_function = jsonify
else:
response_function = partial(render_template, "index.html")
return response_function(context)
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment