Flask Tutorial
About Lesson

In this Flask Tutorial we are going to learn how to Build REST API with Python Flask & SQLAlchemy.



What is SQLAlchemy ?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.



What is Flask-SQLAlchemy ?

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks. See the SQLAlchemy documentation to learn how to work with the ORM in depth.



What is Marshmallow ? 

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


Marshmallow schemas can be used to:

  • Validate input data.
  • De-serialize input data to app-level objects.
  • Serialize app-level objects to primitive Python types. The serialized objects can then be rendered to standard formats such as JSON for use in an HTTP API.



What is Flask-Marshmallow ?

Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.



First of all we need to install these libraries.




So now this is the complete code for Build REST API with Python Flask & SQLAlchemy




OK in the above code, this is our database configuration iam using MySQL database, and also iam using Wamp Server for this.  we are initializing our flask app, also we need to create the object for SQLAlchemy and Marshmallow.




After that we are going to make one Post class for our posts model which has id , title, description and author as their columns and below we have defined structure of response of our endpoint. We have defined post_schema as instance of PostSchema and posts_schema as instance of PostSchema with multiple fields.