How to Create a Basic CRUD API using Python Flask

Hillary Wando
5 min readAug 25, 2022

CRUD is a common acronym used in software development. It stand for the 4 services provided by a REST API, that is: Create, Read, Update and Delete. These services are usually provided by the POST, GET, PUT and DELETE endpoints of a RESTful API. In this article, we are going to create a CRUD API for a book library server using Python Flask and an SQLite database.

Building the Flask Server

Flask is a micro web framework for building application servers using Python. Before we begin creating our web app, we need to create a Python Virtual Environment. You can learn more about Python virtual environments here.

To begin, please enter the following into your CLI.

$ mkdir flask-book-api
$ cd flask-book-api
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install flask

The first two commands make a new app folder called ‘flask-book-api’ and enter the folder. The next two commands serve to create a new Python virtual environment called ‘venv’ and to activate it. We then use the pip command to install the Flask Python package.

To make a ‘Hello World’ app using Flask create a file named app.py in your app folder and enter the following into the file.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
app.run()

Now enter the following command on your CLI while in your app folder to run the server.

$ flask run

You should be able to see the following response on your CLI.

CTRL + click the ‘http://127.0.0.1:5000’ link on your CLI to go to your browser and see your working server.

Building the SQLite DB

SQLite is a simple and fast, open source SQL engine that can be used with Python to store and manipulate application data. The SQlite3 module is usually shipped with the Python distribution so we can use it to create our database without having to install anything else. However, we first have to create a Book model class to represent a book object in our code that will correspond to a book table in our database. Create a ‘models.py’ file in your app folder and enter the following code into it.

Next, we need to create a ‘db.py’ file that will have the code that does the CRUD operations to the database. For this project we will also add some initial data to the database every time we connect to it from an array of objects. Create the ‘db.py’ file and enter the following into it.

We can now add code to our ‘app.py’ file to access the corresponding database method for the POST, GET, PUT & DELETE endpoints. Open the ‘app.py’ file and enter the following code.

With that, our CRUD server will now be complete. You should be able to test all the server’s endpoints using Postman and find them working as expected.

Building the Front-End Templates

Flask makes use of the Jinja2 templating engine to build dynamic web pages. Special placeholders in the template allow writing code similar to Python syntax. Create a folder named ‘templates’ in the app folder and a file named ‘index.html’ in the templates folder. The index html file will be our front-end’s starting point so copy the code from here and paste it into the file.

We also need to add some JavaScript code to have interactivity in our web page along with some CSS to make the page prettier. So create the following folder structure in our app folder.

static
|- css
|-- style.css
|- js
|-- main.min.js
templates
|- index.html
app.py
models.py
db.py

Now add the CSS and JS code from here and here respectively.

The server should reload itself for you such that you should now be able to see the following in your browser when you go to the ‘http://localhost:5000’ url.

index.html

You can now use the buttons on the page to send the CRUD requests to our Flask server and test our Book library API.

Dockerizing the App for Production

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises. You can learn more about it here. In our case, we want to create a docker image of the app and push it to DockerHub so that we can always pull it from there and deploy it to a web app or virtual machine on the cloud. So create a file named ‘Dockerfile’ in the app folder and enter the following into it.

Also create a file named ‘boot.sh’ in the app folder and enter the following into it.

If you haven’t noticed yet, we are using gunicorn to serve the Flask web app in the docker image because Flask does not provide a production server. See more information about this here. To build the docker image and run the docker container locally run the following commands.

$ docker build -t flask-book-api .
$ docker run --name flask-book-api -d -p 8000:5000 --rm flask-book-api:latest

Your server should be running on localhost on port 8000. Now we can run the following command to push our docker image to DockerHub and have it ready to be pulled down in a remote server/VM.

$ docker build -t dockerhub-username/flask-book-api .
$ docker push dockerhub-username/flask-book-api:latest

Remember to replace your DockerHub username in the above commands.

That’s all folks!

Conclusion

So there you have it. We have built a Python Flask CRUD web app from scratch. Starting with a bare bones Flask app, we went through the process of building an SQLite database, a Books model class and a few endpoints for the CRUD operations on the web app. Finally, we were also able to create a docker image of the web app and push it to DockerHub where it can be pulled and deployed on a live production server. You can find the full Github repo of the web app here and the live site here. Remember to like this post if you enjoyed it.

--

--