Creating A Flask REST API

In this tutorial we would be learning how to create a REST API in Flask. For those who might not know what REST is, REST is an acronym for REpresentational State Transfer. REST is an architectural style for providing standards between computer systems …


This content originally appeared on DEV Community and was authored by DEV Community

In this tutorial we would be learning how to create a REST API in Flask. For those who might not know what REST is, REST is an acronym for REpresentational State Transfer. REST is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. Like other architectural styles, REST has its guiding principles and constraints.
API is an acronym for Application Programming Interface, it is a set of defined rules that explain how computers or application communicate with each other. API provide a means for different entity of code (which may always not be written in the same language), to communicate with each other without any need to understand how they both go about carrying out their operations. APIs that comply with REST architectural style guidelines and constraints are called RESTful APIs.
To create our RESTful APIs in this tutorial, we would be using Flask framework. Flask is a Python web application framework, it is a microframework. The "micro" in framework does not mean that entire mean that your whole web application has to fit into a single Python file (although it certainly can), nor does it mean that Flask is lacking in functionality. The “micro” in microframework means Flask aims to keep the core simple but extensible. Flask won’t make many decisions for you, such as what database to use. Those decisions that it does make, such as what templating engine to use, are easy to change. Everything else is up to you, so that Flask can be everything you need and nothing you don’t.

Some of the extension we would use in this tutorial includes:

  • flask-restful - extension for building RESTful APIs.

  • flask-sqlalchemy - extension that adds support for SQLAlchemy (Python's ORM)

  • flask-migrate - extension that adds handles SQLAlchemy database migration.

  • flask-jwt-extended - extension that handles authentication and authorization of users.
    In this tutorial we would be building a Library API where the users can search for books, borrow available books and return borrowed book, search for books based on genre, authors and publishers. Users would be able to carry out CRUD operations (i.e create, read, update and delete) on their profiles. Our program would have an admin user who can carry out CRUD operation on the library database (i.e the admin user would be able to create, read, update and delete books from our library database).

To begin our project we would create a directory called library_api, then change our working directory to the library_api directory. This is done by entering the command in our command line;

mkdir library_api
cd library_api

The next thing to do is create a virtual environment in our library_api directory for our project. A virtual environment is an isolated Python environment where a project's dependencies are installed in a different directory from those installed in the system's default Python path and other virtual environments. In order to create our virtual environment we would run the venv module as a script with the directory path.

python3 -m venv venv

This would create a venv directory if it doesn't exist before, and also create directories inside it containing a copy of the Python interpreter and various supporting files. Next we have to activate the virtual environment.
On Windows, run:

venv\Scripts\activate.bat

On Mac, run:

source venv/bin/activate

Now we install flask into our virtual environment using pip. We use the pip command:

pip install flask

We create an app.py file in our current working directory were we would create our first Flask Hello World app. Enter the following code into the app.py.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

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

In the code above, first we import the Flask class from the flask package. Next we create an instance of the Flask class that take in the __name__ as its only argument. The we make use of a built in route decorator to register the root URL "/" to the index function. The app.route decorator is used to register a url to a view function. A view function is simply a function that handles client's request.


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-22T16:08:23+00:00) Creating A Flask REST API. Retrieved from https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/

MLA
" » Creating A Flask REST API." DEV Community | Sciencx - Tuesday February 22, 2022, https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/
HARVARD
DEV Community | Sciencx Tuesday February 22, 2022 » Creating A Flask REST API., viewed ,<https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/>
VANCOUVER
DEV Community | Sciencx - » Creating A Flask REST API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/
CHICAGO
" » Creating A Flask REST API." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/
IEEE
" » Creating A Flask REST API." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/. [Accessed: ]
rf:citation
» Creating A Flask REST API | DEV Community | Sciencx | https://www.scien.cx/2022/02/22/creating-a-flask-rest-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.