Blog - Trusted Team Extension Partner For Europe & USA

Flask with SQLAlchemy and Marshmallow - Best Software Development Team Extension Partner for Nordics

Written by Md. Nahidur Rahman | 20/04/2024

Using this guide master the combination of Flask with SQLAlchemy and Marshmallow to build and manage scalable web applications with ease.

In the article we will build a simple REST API using Flask SQLAlchemy and Marshmallow. We will be building a note taking application where these two API endpoints /note/ and/note/<id>/ will be available.

We will be able to create new note or get all the notes by doing a POST or a GET request to /note/ api endpoint. Not only that we will be able to get the details of the note or update the note or delete the note with a GET or a PATCH or a DELETE request to /note/<id>/ api endpoint.

First of all, we will give a brief descriptions of the libraries that we will be using in this article.

Flask is a lightweight WSGI web application framework in Python. It is designed to make getting started very quickly and very easily.

marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes.

Flask-Marshmallow is a thin integration layer for Flask and marshmallow that adds additional features to marshmallow.

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask.

marshmallow-sqlalchemy An SQLAlchemy integration with the marshmallow (de)serialization library.

We will be using pipenv as our dependency manager. We are assuming that pipenv is already installed in your system.

Follow the steps bellow and at the end you will have a simple note application running on http://localhost:5000/.

 

Step 1: Setup Project Environment

 

 

Step 2: Install Flask and Other Dependencies

Run the following commands to install all the dependencies including Flask.

pipenv install flaskpipenv install flask-marshmallowpipenv install flask-sqlalchemypipenv install marshmallow-sqlalchemy

After running those commands marshmallow and SQLAlchemy will be installed internally as dependency.

 

Step 3: Create app.py File

 

Step 4: Add This Code to app.py File

 

Step 5: Start pipenv Shell

 

Step 6: Run Flask Server

 

Step 7: Go to the Browser

Start your favorite browser and go to the http://localhost:5000/ url and you will see Hello, World! printed.

 

Step 8: Integrate SQLAlchemy & Marshmallow

Add the following imports into the app.py file.

import osfrom flask_sqlalchemy import SQLAlchemyfrom flask_marshmallow import Marshmallow

And add the following code after the creation of Flask app instance.

 

Step 9: Create Model

Declare the model like following.

 


Step 10: Create Schema

Generate marshmallow schema from the model.

 

 

Step 11: Build Out API Actions

 

Step 12: Setup Database

Enter in python shell

 

 

Step 13: Test APIs

We can use the tool Postman to test our APIs. But before that please ensure your development server is up and running using this command python app.py from Step 6.

The complete code of this article can be found in this repository.