Posted on 2020-12-30T06:49:00Z by Kevin Foong


Swagger is a great tool which enables us to document our API structure and allow others to test it out. Here I will provide an overview on how we can set up Swagger on a Flask API project.
We will use the Flask-Swagger-UI library. Note that this library provides Swagger via a blueprint so we will also need to be using blueprints in Flask (which this post will not go through).
Read more ...
Tags:
flask
API
swagger
Posted on 2020-12-29T11:20:11Z by Kevin Foong
I haven't seen many articles on the internet describing how to use Pydantic together with Flask so I thought of providing an overview of what I did to get this to work. For those who don't know, Pydantic is a Python library used to parse and validate data. It is typically used to ensure that data sent to your API enpoints is in the correct format and type.
- First we install a handy library Flask-Pydantic. This library is used to integrate Pydantic with Flask.
pip install Flask-Pydantic
- Next we create Pydantic schema models. These models define the required fields for the endpoint. A model is just a class that inherits from Pydantic's
BaseModel
. I normally have all models in a separate models_schema.py
file.
- Below is an excerpt from my
models_schema.py
file.
Some notes,
Read more ...
Tags:
flask
pydantic
API
Posted on 2020-09-04T02:54:05Z by Kevin Foong
In this blog post we will be exploring the Quasar QUploader component in order to upload photos and how to handle this in the backend via Flask. I assume you already have some basic knowledge about Vue, Quasar and some Flask.

Read more ...
Tags:
flask
quasar
Posted on 2020-06-13T11:37:25Z by Kevin Foong
In this post we will explore some simple SQLAlchemy statements and compare them to their SQL equivalents. This post is just on using Flask SQLAlchemy to query your database. If you want to know how to model your database please see my other post here. Let's get started!
Let's say we have two tables UserDetails
and Country
in a one to many relationship towards Country
. That is one UserDetails
can only have one Country
. But one Country
can have many UserDetails
. Our models look like this.
UserDetails
class UserDetails(db.Model):
__tablename__ = 'user_details'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
first_name = db.Column(db.String(50), nullable=False)
last_name = db.Column(db.String(50), nullable=False)
gender_id = db.Column(db.Integer, db.ForeignKey('gender.id'), nullable=False)
dob = db.Column(db.String(10), nullable=False)
country_id = db.Column(db.Integer, db.ForeignKey('country.id'), nullable=False)
state = db.Column(db.String(30), nullable=False)
city = db.Column(db.String(30), nullable=False)
def __repr__(self):
return '<UserDetails {}>'.format(self.first_name)
Read more ...
Tags:
flask
flask-sqlalchemy
database
Posted on 2020-06-02T06:30:03Z by Kevin Foong
This is the second part of my tutorial to create a fully functioning user registration and login form in Flask. In the first part of the tutorial we looked at the backend side especially Flask SQLAlchemy and the database. I assume you have already set up everything from the first part and this will just be a continuation of it. In this part we will focus on the Flask side, in particular Flask WTF and Flask Login. Part 1 is available here.
1. First install flask-wtf and flask-login in your project's virtual environment. "wtforms[email]" is just an extra package from wtforms which provides email validation which we will use later.
pip install flask-wtf
pip install wtforms[email]
pip install flask-login
Read more ...
Tags:
flask
mysql
flask-sqlalchemy
flask-login
flask-wtf
Posted on 2020-06-01T12:33:45Z by Kevin Foong
In this post we will go through how to create a fully functional login and registration form in Flask. Part 1 (this post) will go through the backend part - setting up Flask, Flask-SQLAlchemy and creating the database. In part 2 we will build the actual forms and templates and see this in action.
In this tutorial we will start from the beginning, assuming that you only have Python installed. This tutorial was created using Windows but should be similar for other operating systems.
Initial setup
1. Create a folder for your project. Then in this folder create a Python virtual environment.
python -m venv env
2. Activate your virtual environment.
env\scripts\activate
3. Install the following packages.
Read more ...
Tags:
flask
flask-migrate
mysql
flask-sqlalchemy