Creating a Flask App with Python
This is a quick setup guide for creating a basic Flask/Python web application. We’ll be running this locally, but it will be ready to deploy on Amazon Web Services (AWS) with Flightcontrol.
We’ll start by setting up a new project locally, and then we will push the Python code to a GitHub repository.
Flask is a lightweight Python web framework. If you are looking to build a simple web application or API, Flask is a great choice. For more complicated applications, you might want to consider using the Django framework. Both run great with Flightcontrol!
Prerequisites
Before you begin, you will need to have the following installed on your computer:
- Python 3.8 or higher
If you don’t know what version of Python you have installed, or you don’t have Python installed, you can check by running the following command from the command line:
python --version
Your python installation might also be python3
instead of python
. If you get a version number of 2.7 or an error when running python --version
, try running python3 --version
instead.
If you don’t have Python installed, you can download it from the Python Downloads page. Choose the most recent version. If you’re on a Mac, you can also install Python using Homebrew.
Setting up a Python Virtual Environment
Let’s get started by setting up a Python virtual environment. We want to install all of our Python dependencies in a virtual environment so that they don’t conflict with other Python projects we might be running locally.
Create a new folder for your project. We’ll call our project folder books
.
Open up a command line terminal, and change directories into the books
folder.
We can create a virtual environment using the venv
module that comes with Python. We’ll call our virtual environment venv
. The commands differ slightly depending on your operating system.
For Linux or Mac (could be the python
command instead of python3
):
python3 -m venv venv
For Windows:
py -m venv venv
After creating the Python virtual environment, we need to activate the virtual environment. If we don’t do this step, we’ll inadvertently install our Python dependencies into the global Python installation instead of the virtual environment.
Again the commands differ somewhat based on your operating system:
For Linux or Mac:
. venv/bin/activate
For Windows:
venv\Scripts\activate
In either case, your command line will change to show that the virtual environment is active. It will look something like this:
(venv) user@computer:~/books$
Now that we’ve got the Python virtual environment activated, we can use the virtual environment for our Flask web application.
Installing Flask and Gunicorn
Python includes a package manager, pip
. We can use pip
to install Flask and Gunicorn. Gunicorn is a Python web server that we will use to serve our Flask application.
pip install flask gunicorn
Several Python packages will be installed.
You will need to freeze the Python packages into a requirements.txt
file that can be used to recreate the environment if you run this application elsewhere. You can do this by running the following command:
pip freeze > requirements.txt
Inside requirements.txt
will be a list of your Python dependencies with their version numbers.
Creating a Flask Application
Now that we have Flask setup, we need to write a couple of Python files. We aren’t going to do anything very fancy in our Flask application, just enough to get it running on AWS with Flightcontrol.
Both of these files will go into the root directory of our project.
The first Python file we create will be named app.py
and will contain the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Books Index Page"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=3000)
Most of this code is required for any Flask application. We do have one route, /
, which will return the string Books Index Page
when the route is visited.
We also have a line that tells Flask to run the application on port 3000
, and to listen on all IP addresses. This is important for when we deploy our application on AWS ECS and make it generally available.
The second Python file we create will be named wsgi.py
and will contain the following code:
from app import app
if __name__ == "__main__":
app.run()
We need this wsgi.py
file because the Gunicorn web server uses it to determine which Python code to run. In our case, it’s pointing to the code in our app.py
file.
Now you’ve got a small Flask application ready to run locally with Gunicorn. On Mac or Linux, we can test it by running the following command from the command line:
gunicorn --bind 0.0.0.0:3000 wsgi:app
On Windows, you will need to be using the Windows Subsystem for Linux (WSL) to run the gunicorn
command. If you’re running this on a normal Windows Command Prompt, you can run just the Flask application by running the following command:
python app.py
If you’re having trouble running gunicorn
, try running it with the path to the gunicorn
binary in your virtual environment, for instance:
venv/bin/gunicorn --bind 0.0.0.0:3000 wsgi:app
Open http://localhost:3000/
in your web browser, and you should see the “Books Index Page” text.
Setting up a Procfile
Now that we know what command to use to run our application, we need to configure Flightcontrol to use the same one.
We can do this in one of two ways - adding a custom start command for our environment, or by creating a Procfile
in the root directory of our project.
We’ll use the Procfile
method in this guide. A Procfile
is a simple file that contains a one line command, along with the name of the service to use. In our case, we’ll use the web
service, and the command will be gunicorn --bind 0.0.0.0:3000 wsgi:app
.
The contents of the Procfile
will be:
web: gunicorn --bind 0.0.0.0:3000 wsgi:app
Adding the Flask Project to GitHub
Flightcontrol uses GitHub repositories to deploy your code base. If you don’t have a GitHub account, you can create one at https://github.com.
Create a new, empty repository on your GitHub account and then push your Flask project to GitHub.
You can push your code from the command line with the following commands:
git add *.py requirements.txt Procfile
git commit -m "Initial commit"
git remote add origin YOUR_GITHUB_REPO_URL
git push -u origin main
Flightcontrol can work with either public or private GitHub repositories. If you authorize the Flightcontrol Ops GitHub application to access your repository, Flightcontrol listens for changes on that repository to automatically deploy environments.
Deploying a Flask Application with Flightcontrol
To run this application on AWS, see our Getting Started with Flask guide.