Setup Flask on Localhost (Dev Mode and CGI Mode)

Purpose: Get started with Flask and run it in your dev environment.

Install Flask

Install Python if you haven't. Then run:
pip install flask

Download Sample Flask Project

Download this Sample Flask Project and place it in a subdirectory under your document root.

Run in Dev Mode

There are two ways to run this project in dev mode:

Method #1
python -m flask run --debug
The --debug option makes it so you don't have to re-run this command when you change app.py.

Method #2
python app.py
Method #2 works only when app.run() is called in the Python script. When you use app.run(debug=True), it turns debug mode on.

Run in CGI Mode (through Apache)

To direct Apache run a Flask app in a given location, add the following files in the directory where your app.py resides:


index.cgi (Apache will go here first)

.htaccess (this affects how Apache deals with the URL)

Required Reading: Brief Explanation of Why This Works

You already know from Exercise 2.1 that CGI scripts need to output HTTP headers and content. The overall goal of adding the index.cgi and .htaccess files is (1) to get Apache to redirect everything into index.cgi whenever the URL points at the project directory (including its files and subdirectories), and (2) to have index.cgi output the headers and content that your Flask app wants it to output.

.htaccess files are a type of Apache config file that tells Apache where to redirect requests in a certain directory. We are not necessarily teaching the ins and outs of the .htaccess format in this class, but the .htaccess file above basically tells Apache to run everything through index.cgi whenever the URL points at the project directory.

index.cgi imports two important things: (1) Python's wsgiref.handlers.CGIHandler function and (2) the "app" object from your very own app.py, which you initiated using the line
app = Flask(__name__).
You may notice that index.py imports your app.py the CGIHandler function is called. So the app object is all set using whatever your Flask code needed to do to it, before it gets passed to the CGIHandler function. Flask's app object is said to be "WSGI Compatible," meaning it has all the attributes and methods expected by Python's wsgiref library. The CGIHandler function gets all the information it needs out of your app object, then prints out the HTTP headers and content that a sophisticated CGI program should.

Required Reading with Optional Exercise: Set Up a Virtual Environment with venv

Our webhost uses Python's venv feature behind the scenes to set up virtual environments for each project when use use the "Setup Python App" widget. The purpose of this optional part of the exercise is to help you experience how this is done by doing it on localhost. You may do it with any project directory.

Why venv: It is considered good practice to use Python's venv for each project you work on. The reason is that Python's venv exists to solve a very common problem: You often need to update Python or update one of your pip-installed packages. Often you need to do this for security, sometimes you want to take advantage of new features, or there may be other reasons you need to update. When you do update, your code sometimes breaks, or some part of it does, because even though the people who develop these updates usually try to make the updates reverse-compatible, they rarely do it perfectly. This problem becomes worse when there are (1) multiple projects on your machine or (2) multiple machines you install your project on (such as a development and production environment, like we are doing in this class). You might update Python or a dependency, thinking of a need in one project, and then another project breaks. Python's venv overcomes that problem by installing an isolated copy of Python and its packages inside your project and keeping a record of which versions and dependencies are in it, and ensuring that when you run Python from your project directory, it is that version that runs.

Start by installing venv:
pip install virtualenv
Then, in your project directory, create a venv:
python -m virtualenv venv
You will see that a venv directory is created with the virtual environment installs, which includes an activation script that needs to be run. In Windows, from the project directoy, run it from the venv\Scripts directory:
venv\Scripts\activate
On mac, run it from the venv/bin directory:
source venv/bin/activate
To deactivate a venv, run:
deactivate

Required Reading with Optional Exercise: Setup requirements.txt for pip in your Virtual Environment

Our webhost uses pip's requirements.txt feature behnid the scenes along with venv to ensure dependencies are consistent in each project when you use the "Setup Python App" widget. The purpose of this optional part of the exercise is to help you experience how this is done by doing it on localhost. You may do it with any project directory.

Why: The requirements.txt feature puts all dependency versions in one place and make them easy to install or reinstall as needed. How: First, track dependency versions and ensure the expected ones are installed. There are two commands that tell you what dependencies you already have:

pip list
pip freeze
The main difference is the "pip list" is supposed to be more user-readable, while "pip freeze" is formatted for a requirements.txt file.

If you do not have a requirements.txt file, create one using pip freeze and a command-line redirect as follows:

pip freeze > requirements.txt

You may adjust your requirements.txt as needed. After changing it, install all of its dependency versions with the command:

pip install -r requirements.txt