MCS 260 Fall 2020
Emily Dumas
Flask is a Python web framework. It lets you build APIs and web sites with Python.
Competitors include:
Using pip
to install, if the interpreter name is python
:
python -m pip install flask
Confirm installation by testing import in the REPL:
>>> import flask
>>>
If Flask is not installed, this will produce an error.
Flask uses a lot of decorators, a Python feature we haven't covered yet.
Basically,
@name
def f(x,y,z):
means: After defining f
, it should be replaced with name(f)
. Here name
should be a higher-order function that modifies the operation of f
.
We might write a function that returns a string:
We might write a function that returns a string:
A Flask decorator lets us link it directly to a URL:
A Flask decorator lets us link it directly to a URL:
Build API so that
/
returns
"Hello World"
.
Build API so that
/apple/random
returns a JSON object with info about an apple variety selected at random from a list.
Build API so that
/metal/random
returns a JSON object with info about a randomly-selected metal.
Build API that checks whether a given word is a palindrome, so
/palindrome/test?word=banana
would return False
in JSON.
localhost
refers to the host a request originates from.127.0.0.1
is an address for localhost using the loopback network interface.http://domain.com:1234/rest/of/URL/
means to use port 1234 instead of the default (which is 80 for HTTP).app
is a flask.Flask
object, the decorator @app.route("/path/part/of/URL/")
makes a function into a URL handler.flask.jsonify
converts a Python value into a suitable return value from a URL handler.