DEV Community

Cover image for Creating A Flask REST API (Part 1):Setting Up a Basic Flask App
Joshua Oladokun
Joshua Oladokun

Posted on • Edited on

Creating A Flask REST API (Part 1):Setting Up a Basic Flask App

In this tutorial, we would be learning how to create a REST API using 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. APIs provides 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.
Creating our RESTful APIs in this tutorial, we would be using Flask framework. Flask is a Python web application microframework. The "micro" in framework does not mean that your entire 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:

In this tutorial we would be building a catalogue 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 application 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 catalogue_api, then change our working directory to the catalogue_api directory. This is done by entering the command in our command line;

mkdir catalogue_api
cd catalogue_api
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

On Mac, run:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

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

pip install flask
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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, Flask uses the __name__ argument to find templates and static files. Then 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, and values returned are known as response. Whenever a request is sent to the root URL "/" endpoint, the index function is automatically called to handle the request.
The app.run() function is used to run the Flask application on a local development server.

To run the our Flask application, we enter the following code in the terminal.

python app.py
Enter fullscreen mode Exit fullscreen mode

The output looks like this:
Image description

We can see our app is running on http://127.0.0.1:5000/. If we enter the URL into any browser of our choice. We would get a response looking like this :

Image description
This shows that our app is running well.
But we can see that there is a warning when running our server that says this is a development server and shouldn't be run in a production deployment. The reason for this warning is because Flask thinks we are running our app in a production environment on a development server. To remove the warning we have to let Flask know we are running our app in a development environment, to do this we have to create a new file called .flaskenv in our catalogue_api directory. Enter the following code into the .flaskenv

FLASK_APP = app.py
FLASK_ENV = development
Enter fullscreen mode Exit fullscreen mode

The .flaskenv file is used to store Flask specific configurations. In the first line, we tell Flask to check the app.py file to run our application, and in the second line we set our environment to a development environment. In order for Python to read our .flaskenv file we would need to install a Python library python-dotenv. To install the library into our virtual environment, we enter the following code into our terminal.

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

The library reads information from .env files or .flaskenv as key-value pairs. To run our flask app we write the following code into the terminal.

flask run
Enter fullscreen mode Exit fullscreen mode

The output will look like this
Image description
We haves successfully set up our Flask app in a the development environment. In the next tutorial we would be looking at creating models and persisting data into our database using Flask-SQLAlchemy, Flask_Migrate and Postgres.

Top comments (0)