Setting up flask framework for back-end development

Reading Time: 2 minutes

Flask is an  micro framework used to build web apps with just a few lines of code, and we are going to look at the steps invoved in setting up your python environment to work with flask in the following steps below>

Update and upgrade your system packages using the commands below as a super user

sudo apt update

sudo apt -y upgrade

Check the version of your python3 using: python3 -V

Then install pip the package manager tool for python responsible for installing and managing programming packages you might want to install for your projects:

sudo apt install -y python3-pip

Then install a few more packages and development tools to ensure that you have a robust set-up for our programming environment:

sudo apt install build-essential libssl-dev libffi-dev python3-dev

Virtual environments enable you to have an isolated space on your server for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects. While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

sudo apt install -y python3-venv

With this installed, we are ready to create our flask project. Create a new directory with mkdir, giving your project folder the name you prefer:

mkdir rentals

cd rentals

Once in the rentals folder, create your virtual environment using: python3.7 -m venv myvenv

Now activate  your myvenv environment to start working

. myvenv/bin/activate

Your can install flask using: pip install flask

Go ahead to create a python file that will house your application

touch myapp.py

Use your favorite text editor to add the code below to your myapp.py file

from flask import Flask
app = Flask(__name__)

@app.route(“/”)
def hello():
    return “hello world”

Once done, safe changes and export there two environment variables:

export FLASK_APP=myapp.py

export FLASK_ENV=development

Now you can your app using: flask run

By Default your app will be running on: http://127.0.0.1:5000/

Happy Coding

Credits: Digital Ocean, Flask Website, python3 tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *