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.

Setting

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: https://pip.pypa.io/en/latest/installing/

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 __init__.py 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 config.py 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 __init__.py file

In the wood directory of the project you create a file called manage.py 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 user.py file with the following content

In manage.py file you need to import this user.py 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 manage.py test

Controller

In the service folder, you create a new file user_service.py 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 dto.py with the following content:

To understand more about what DTO is, please refer to the following link https://en.wikipedia.org/wiki/Data_transfer_object This file will help us to respond api, validate or write docs

Create user_controller.py 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 __init__.py file inside the app the following content:

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

You need to update the manage.py file with the following content to register the route

You run the server: python manage.py run and the link http://127.0.0.1:5000/ 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.

Document

Articles translated from

Share the news now

Source : Viblo