06. Backend
Now we will setup the backend so that we can start creating and updating Categories and Products by making requests to the API.
One thing that you should know is that you can create your backend in any directory, since this will be a complete separate server which we serve the data for our frontend application.
Now let's create a folder name back
.
Creating .env
We will create virtual environment for our backend by following commands. Please make sure you create your backend in Linux or WSL(mac users can using bash terminal).
python3 -m venv .env
source .env/bin/activate
Installation
Please install the flask framework by running the following command:
pip install flask
Creating the app
Create a file name main.py and add the following code:
python filename=main.py
# --- Imports Start here--- ###
from flask import Flask
# --- Imports End here--- ###
app = None
# ---- Flask app factory ---- #
def create_app():
app = Flask(__name__)
return app
if __name__ == "__main__":
app.run()
Starts the backend server by running the file main.py
:
python3 main.py
For all the configurations of the application we create a separate file named config.py
where will write all the configuration related with application.
Database Configuration
python filename=config.py
class LocalDevelopmentConfig:
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///database.db'
You can see we have created a LocalDevelopmentConfig which will hold all the configuration related with local development.
Creating the database
After this configuration we will create the database and the tables to store data into the database.
Note We will SQlite database and in the model/table will start with usert table. Flask-SQLAlchemy will help us to all the operations ORM with database.
Flask-SQLAlchemy Installation
pip install flask-sqlalchemy
Creating the database
Create the database in a file named database.py
.
python filename=database.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
Now we will create the User
table in a separate file named database.py:
python filename=models.py
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(1000))
role = db.Column(db.String)
doj = db.Column(db.DateTime)
loginAt = db.Column(db.DateTime)
image = db.Column(db.BLOB)
See all the columns in the user table depends on your application's business logics. As a beginner, you can create any number of columns which you think required for your application.
Importing and Initializing the database
python filename=main.py
# --- Imports Start here--- ###
from flask import Flask
from database import db # [tl! add:start]
from models import (
User
) # [tl! add:end]
# --- Imports End here--- ###
app = None
# ---- Flask app factory ---- #
def create_app():
app = Flask(__name__)
db.init_app(app) # [tl! add:start]
app.app_context().push()
db.create_all() # [tl! add:end]
return app
if __name__ == "__main__":
app.run()
Note >
app_context
is a function which is used to create and push an application context to make Flask's app-specific global variables (like current_app and g) accessible outside a request context.
db.create_all()
method will create all the tables defined in the models.py
file.
Testing it out
Now you can start the backend app using the following command.
python3 main.py
Let's commit the change using the following command.
git add .
git commit -m "database integrated"