Build base project API using Flask

Tram Ho

In this article, I will guide you to build an api base project in python using the Flask framework, the project will also generate documents for api.


First check to see if your machine has installed pip yet:

If not installed you can see this link to install pip on your computer:

Install python and virtualenv:

Build base project

Create folder

In the directory you just created, create folders in the init file at the following level:

In that file you leave blank, this file lets flask understand that it is a module only

Next install the flask framework and necessary packages

After the installation is completed, you need to update the requirements.txt file to save the installed packages

The requirements.txt will have this content after running the command

In the main folder, create a file with the following content:

This file is to set the environment and database, in this section the default database is sqlite , if you use mysql or postgree then need to install PyMySQL or psycopg2 packages. then place the line SQLALCHEMY_DATABASE_URI you need to change the content for it, assuming mysql will change to SQLALCHEMY_DATABASE_URI=mysql+pymysql://root: [email protected] /flask_test (root is users, 123456 is password, localhost is host url, flask_test is the name of the database), postgree is the same, just change mysql+pymysql to postgresql+psycopg2 .

In the main directory, add the following content to the file

In the wood directory of the project you create a file called with the following content:

Finally run the following command to initialize the python server

everything is fine it will show like this

So we have finished creating the app for the flask, and then we will process the next parts such as model, controller, service or unit test.

Database Models and Migration

In the package model you create a file with the following content

In file you need to import this file to be able to run the migration

Next run the migration

Kiểm TRA

In the test module, you create a file with the following content:

This file is intended to test the config file you just created above

Run the test with the following command: python test


In the service folder, you create a new file with the following content:

This service part is quite simple, it has functions like create users, get all users or get detail a user

In package main you create a new package named util

In util you create a file with the following content:

To understand more about what DTO is, please refer to the following link This file will help us to respond api, validate or write docs

Create in the controller

This controller will function get list users or get detail users, and there are also sections to write docs for api in code, this at first looks a bit confusing code.

Add the file inside the app the following content:

This file is like route registration for api packages using blueprint packages

You need to update the file with the following content to register the route

You run the server: python run and the link will show the docs for api you can just write or list users get tested but above api

In the next part, I will translate the end of the article.


Articles translated from

Share the news now

Source : Viblo