from flask import Flask app = Flask(__name__) # at this point, app is ready to respond to all requests with a 404 error # we will register paths with other responses below. # run it either with the command line invocation # python3 -m flask --app flask-demo.py run # or by adding code to this file as follows: # if __name__ == '__main__': # app.run() # register a path for the default method (GET) @app.route("/") def any_name_you_want(): return "<p>Hi from 340!</p>" # register a path for the GET method # note: the paths are exact; /code/ and /CODE do not match this @app.get("/code") def a_different_name(): return "<p>You asked for /code</p>" # register a path for the POST method @app.post("/code") def a_third_name(): return "<p>Thanks for sending us your code</p>" # register a path with wildcards; argument must match angle-bracketed part of path @app.get("/welcome/<user>/<greeting>") def get_welcome(user, greeting): return "<p>Hello, "+user+", thanks for telling us "+repr(greeting)+"!</p>" # register several methods to the same function with a request body @app.route("/echo", methods=["GET","POST"]) def echo(): from flask import request return f'''<p>You used method {request.method}</p> <p>You send the following form data: {request.form}</p>'''