Part 1: Building your first backend
Introduction
For this course we will be using the Flask framework. Flask is a web framework built in Python that allows you to easily build web applications. It provides nice functionality to make a simple application quickly, and has many extensions to scale your app should you so require. If you are interested in it, definitely check it out.
We will start by creating a folder where we will host the backend.
mkdir backend
Creating a virtual environment
This is where all the backend stuff will be hosted. We will move into this directory and then create a virtual environment.
It is nice to have a virtual environment, because this creates an isolated area where all the packages and requirements that we need will be installed. You’re probably familiar with dependency hell, where your current application makes use of some package of which a newer version is already installed. By using a virtual environment, everything that you install here will not conflict with other applications and you will thus not face this issue. Very nice!
cd backend
python -m venv venv
The above command runs the python syntax -m venv venv. The first venv command initializes the virtual environment which then creates the folder with all the relevant files with tne name venv (the second venv). To create a venv with a different name just run python -m venv <your-venv-name>
. I find it easy to just use the name venv, so that later it is clear what the folder does and I can easily find it no matter which project I’m using a venv in.
We now activate the virtual environment by running:
venv\Scripts\activate (Mac: source venv/bin/activate)
Our venv is now running! Let’s install Flask.
pip install flask
We now have Flask installed and are ready to build our first application!
Building our first application
While still in the backend folder, let’s create a file called backend.py
In this file, we’ll write the following code.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def main():
return {
"message": "Well done!"
}
What happens above is that from the flask package we import the class Flask. The variable app is then an instantiation of this. The decorator @app.route adds a route (in this case the route ‘/’) to this instance to which the app listens. Everytime this route is visited, the function below it (in this case main) is run.
Run the code by using:
flask --app backend run
This way it invokes the flask program to run, using backend as the value for the app variable.
It will show the following:
Well done, you have now created your first Flask application!
Since at some point we want to run our application with many environment variables, the command will become bloated. Let’s change that.
Within our virtual environment, we run the command:
(venv) $ pip install python-dotenv
In our backend folder we then create a file called .flaskenv
with the following code:
backend/.flaskenv
FLASK_APP=backend.py
This way, everytime time flask is run it will check the .flaskenv file and load all the variables in the environment, saving us from having to type the environment variables every time on startup. Nice!
We will add another variable, since later we will be running our backend on port 5001, not 5000.
backend/.flaskenv
FLASK_APP=backend.py
FLASK_RUN_PORT=5001