Download this Sample Flask Project and place it in a subdirectory under your document root.
There are two ways to run this project in dev mode:
To direct Apache run a Flask app in a given location, add the following files in the directory where your app.py resides:
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 lineOur 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.
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:
If you do not have a requirements.txt file, create one using pip freeze and a command-line redirect as follows:
You may adjust your requirements.txt as needed. After changing it, install all of its dependency versions with the command: