After installing the Flask package, let's read the Flask Quick Start Guide and see what it wants us to do.
We find an example like the one below. Let's temporarily create a new file called "hello.py" in the root directory of the repo, and place the following contents inside:
NOTE: We are using a function decorator to specify the route (i.e. the path we visit in the browser).
NOTE: Flask route function names like hello_world() must be unique.
Later when we move and re-organize our application we'll need to run it differently, but for now with the "hello.py" file we can run it from the root directory like this:
# Mac OS:FLASK_APP=hello.pyflaskrun# Windows OS:# ... if `export` doesn't work for you, try `set` insteadexport FLASK_APP=hello.pyflaskrun
This command starts a local web server on port 5000. Preview the app by visiting http://localhost:5000 in the browser. Nice, the web server is running and responding to our request.
NOTE: whenever we modify the code we'll need to stop the web server via "ctrl+c", and start it again before previewing again.
More Routes
Let's add more routes.
After restarting the server, visit the following URLs in the browser:
FYI: the %20 character acts as a space character in the URL params
Review the code that handles the "hello" route. Notice the "hello" route referencing the request object from Flask, specifically looking at its args property (i.e. request.args), a dictionary-like object, to access the URL params.
Nice, our app is dynamically handling different URL parameters!
Review the code that handles these "book" routes. Notice how we are using the jsonify method from Flask to convert a Python list or dictionary to a JSON response.