Lab: getting started with Flask
Nothing to hand in
Flask: a web framework
To create the back end of our web application, we're going to do what most web developers do—we're going to use a web framework. That is, we're going to use a library that is designed to simplify many of the tedious or difficult parts of web application implementation.
Because almost all of you have studied python before, we are going to use a web framework written in python. Furthermore, since your web application is going to be quite simple, we will want to use a web framework that requires minimal code to get started. As of now (spring 2025), the most popular light-weight python-based web frameworks are FastAPI and Flask. We will use Flask.
You can read all about Flask online, and we'll talk about many of its features as we go along, but for now, we just want to get going as quickly as possible.
Installing Flask on your own computer
Step 0: for WSL users only
Are you using WSL? Then you probably need to update your python3 installation before proceeding to the next section. This should do it.
Try this:
python3 -m venv venvThat probably didn't work. It also probably gave you an error message that suggested that you should do
apt install python3.12-venvor something along those lines. But if you tried doing that installation, WSL probably gave you yet another error message—something about a lock, maybe. Anyway, if so, then do this:sudo apt install python3.12-venvThat is, do whatever the error message told you, but put a
sudoin front of it. That puts you temporarily into admin mode so you can install thevenvmodule into python.You're also going to need the python module installer known as
pip, so do this:sudo apt update [this will take a while] sudo apt install python3-pipTry this again:
python3 -m venv venvIf it succeeds (no error messages), you should see a folder named
venvinside your current working directory. Sincevenvis in the wrong place for where we're heading for this lab, you should delete it before moving to Step 1, like so:rm -rf venvBe very careful with
rm -rf, by the way. You want to make sure you are deleting the thing you want to delete before hitting Enter on that command.
Step 1: create a python virtual environment
The Flask installation documentation encourages you to use a thing called a virtual environment for your development work. We'll go along with this plan, even though it adds an additional weird new thing for you to think about.
- Update your copy of my cs257 repository.
cd cs257-2025-spring git pull
Create your virtual environment into which you will install Flask.
python3 -m venv venvActivate your virtual environment.
source venv/bin/activateThat runs a bash or zsh program named
venv/bin/activatethat tells your command shell to considervenv/binandvenv/libto be the first places to look for Unix commands and python libraries.Check to make sure you're now using the correct copy of python3.
which python3The answer should be the full path to
venv/bin/python3.
Install Flask into your virtual environment
Install Flask into
venv.python3 -m pip install FlaskDid it work? Go to the python command prompt and see. When you do
import flaskin python like this, if you don't get an error message, you have successfully installed Flask into your virtual environment.(venv) ~/cs257-2025-spring $ python3 Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import flask >>>
Launch the first flask sample
In my repo, there's a folder called flask containing a couple sample programs.
Today, we're going to work with the one called flask_sample.py.
Launch the sample.
cd flask python3 flask_sample.py localhost 9999This will print a few messages in your terminal and then just hang. You can kill it at any time by bringing its terminal window forward and hitting Ctrl-C. But right now, don't do that—just let it run.
- Go to a browser, and navigate to http://localhost:9999/help. This should print out a help document for the API that flask_sample.py is implementing. (Sometimes browsers don't like to let you use "localhost", so if that link doesn't work, try http://127.0.0.1:9999/help.)
- Look back at the terminal window. Do you see any messages that reflect your browser activity? If so, what do you make of them? Reload the browser page you just looked at. Does anything change on the terminal?
- Use the documentation at
/helpto guide you in experiments with/,/actor/..., and/movies/.... Play around with the API, and keep an eye on the terminal as you do so. - Done? Ctrl-C in the terminal to shut down the API server (i.e., to shut
down
flask_sample.py). - Open up
flask_sample.pyin a text editor. Note the hard-coded "database" at the beginning, and then read the code and comments, with particular attention on the "@app.route" lines. Compare what you see there to the behavior described in the /help output, and to whatever you observed when playing with the API. - Make a copy of
flask_sample.pyto mess around with (say,flask_sample_copy.py), and try putting in someprint('blah blah', file=sys.stderr)debugging messages in various places. Relaunch your modified app (python3 flask_sample_copy.py localhost 9999) and try hitting it with your browser a few times. Do the error messages you inserted get printed when you expected them to? Keep playing like this until you're pretty sure you know which functions get called and when. - Try to add a new route, also known as an API endpoint, to enable the client to request a list of actors who were alive during a specified year. For example, set it up the the URL "http://localhost:9999/actors-alive-in/1980" will induce the API to return a JSON list of actors alive in 1980.
- Read through the code more carefully, and take notes about things in the code that you don't understand. Bring your questions to class or ask them on Slack.
- All done!