July 2017
Beginner to intermediate
340 pages
7h 43m
English
Another feature provided by the routing system is variables.
You can use variables using the <VARIABLE_NAME> syntax. This notation is pretty standard (Bottle uses the same), and allows you to describe endpoints with dynamic values.
For example, if you want to create a function that handles all requests to /person/N, with N being the unique ID of a person, you could use /person/<person_id>.
When Flask calls your function, it converts the value it finds in the URL section as the person_id argument:
@app.route('/api/person/<person_id>') def person(person_id): response = jsonify({'Hello': person_id}) return response $ curl localhost:5000/api/person/3 { "Hello": "3" }