Flask 2: Adding a bit more Flask...
This follows on from the basic Flask introduction on the 'Setting up for Flask' page here.
One of the important things to know about Flask is that you can (within the limits of sanity) have as many routes as you like. Simply add a new unique function* with a route decorator* to match the route you want. In this example we are adding a route called /help (so the address would be your website one with /help on the end, for example http://localhost/help )
*Functions are the blocks of code in Python that start with def, and routes (in Flask) relate to the line(s) directly above the function starting with @app.route. In Python terms, this is called a decorator - it 'decorates' the function with a route (in this instance)
The function is the code that will run when that route is browsed, and the route is how you browse it.
from flask import Flask
app = Flask(__name__)
@app.route("/") # ROUTE DECORATOR
def hello(): # ROUTE FUNCTION
return "Hello World!"
@app.route("/help")
def help():
return "help!"
if __name__ == "__main__":
app.run(debug=True)
The next useful thing Flask can do is match wildcards. Imagine we're building a Pizza shop and we have 47 different flavours to add to the site - something like /pizza/hawaiian and /pizza/meatlovers etc. We wouldn't want 47 different routes - for a number of reasons which include not wanting to have to change our program every time we get a new pizza, and not wanting to have that much duplication since each page would be very similar.
To solve this problem, Flask allows us to use wildcards. This will give you an idea how they work:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/help")
def help():
return "help!"
@app.route("/pizza/<string:pizzaname>")
def pizza(pizzaname):
return "you want a {} pizza!".format(pizzaname)
if __name__ == "__main__":
app.run(debug=True)
Its useful to note here that the variable 'pizzaname' in the pizza function contains whatever the user typed into the URL after /pizza/ - this COULD be malicious code and it should be sanitized before you do anything with it (excellence criteria: flexible and robust) - but for now we'll assume its the name of a pizza - and while we're just printing it straight back out to the screen here we could use that value to do a database search and return the actual information relating to that pizza. More on that later.
Here's how it works in detail:
@app.route("/pizza/<string:pizzaname>")
This line tells flask to look out for any URL with /pizza/****something**** and store the something part in a string variable called pizzaname
If a matching URL is found then run the following function, and pass the ****something**** into it. This is why we have to have the 'pizzaname' parameter in the function name:
def help(pizzaname):
now the variable pizzaname can be used in the function, in this case its simply returned in the web page as text. We'll get to what we can do with it later.