Implementing Flask RESTful To Create Easier Backend Flask Applications

George Irizarry
6 min readJun 19, 2024

--

Flask Image

Flask, an extremely well-known backend microframework, is known to be lightweight, easy to implement, and relatively easy to understand. Flask works with Python in order to create backend databases and form responses to HTTP web requests. How is this done?

Flask Brief Overview

Flask takes a more imperative approach when it comes to making databases and forming responses, similar to how the React extension is implemented in Javascript projects, which makes Flask popular. Let’s go over some of these features on a surface level, using a recent coding challenge I had just completed recently!

The top of the file are all of our imports, we won’t go over these in this article, or even most of the code in the subsequent files I’m gonna be showing. But there are many videos, blogs, tutorials, and of course the documentation that can lead you to use Flask in your projects today! But, for now, in this file we’re building out the database table named “restaurants”. This table will have three columns, the ID column, the name column, and the address column. From a glance and a little SQL knowledge, you can probably guess what’s going on here. You may have noticed other key features in this model, however. ID is set to a primary key! Normally you would have to specify in your class that you want whatever instance you’re passing into a table that you’d want the ID attribute of that instance row to be tied to the row it was in theory assigned to, making the ID unique!

And what about db.Relationship? In a normal one-to-many SQL relationship that’s held in two classes, you would first have to assign the ID attribute of the instance in the relationship as a foreign key of the other instance that belongs to another class in a seed file, the “many” would hold the foreign key. Then you would have to use an instance method to access a list you would have had to make that has all of the “one” and “many” instances. Then you would have to map that list, access the every one of the ID attributes of the instances included, and compare them to the instance that’s trying to grab that relationship. Whew! That’s a of work… db.Relationship does all of that for you in two lines of code.

I think it’s clear to see why a microframework like Flask is loved within the programming community. Now while I do refer db.Relationship as “db.Relationship”, that’s just in accordance with my specific file. “db” is assigned to a sqlalchemy class that has many methods programmers use to form those relationships and SQL shortcuts, we just gave two examples above!

Setting Up Routes

The reason we make classes, add methods to them, and provide relationships between them is to store information our users will gain use from, forming a server. This is done by HTTP requests the user will send through your web application. Our server needs to not only be able to handle these requests but also give informational errors when a request has gone bad, no matter the fault. Let’s take a look at one way of performing that task.

In this example, we import all of the classes we’ve made (not the classes listed above) located in models.py. We then assign routes that correlate to our backend server using a decorator. Underneath, the function we’re decorating holds the task of whatever we want to be handled when the user sends a request to this URL route. In this case, it’s a GET request, we know this by the Game.query.all(), grabbing all of the instances tied to the class, grabbing its attributes, and making a dictionary out of them, utilizing a for loop to do so. We then append said dictionaries to an empty list we have set beforehand. And finally, we return a response of that list with a status code of 200 to signify success.

Python Philosophy

This accomplishes the task, certainly, but we have to go back to the philosophy of the programming language Python and see if this truly fits. While this file is a simple example, how would this look when we want a URL to handle all HTTP requests, contain constraints, and multiple classes? A main point in Python’s philosophy is “simple is better”, which is actually why you’ll see a lot of human-readable language implemented in Python code! No matter what language, this idea holds true but it’s Python’s inherent goal to uphold this belief, and many programmers believe in this mission too.

Flask RESTful

Flask RESTful, REST standing for Representational State Transfer, is an extension that was made to not only make making RESTful APIs easier but to also adhere to Python’s philosophy of “simple is better”. Let’s show an example of this by returning back to the code challenge example.

We first pass in our app, which is our flask application instance, into API. This allows us to use all of the API methods. We then set up our classes, before we would have to decorate a function and define our responses within that, now we make classes that have METHODS that define our responses, making this method more Pythonic. These classes will be tied to specific routes, which we’ll discuss later on. We then inherit Resource, making our classes a subclass and inheriting all of Resource class methods. Both Resource and API are imported from flask_restful.

The major difference in this method of making RESTful API is the way we form our methods. Before we would have to grab the request method, check what type of HTTP verb it was, and design a response to that request. And while, as we said before, this is fine, with the code becoming hundreds of lines deep, it’ll be helpful to have easier-to-read code. Flask_resful allows us this luxury by having us define request methods directly by name! No if, elif statements, no grabbing the request verb, but directly running whatever method you defined by name! We then tie this class to a route by using api.add_resource(class_name, url_route)

Let’s use Postman to send a request to the ‘/pizzas’ route, which is tied to a class containing a GET method.

It works!

Conclusion

As programmers, an important job of ours is to make our code not only work as intended but as clean, DRY, and understandable as possible. Flask_restful achieves this by not only making setting up routes in a more Pythonic way, but by also defining responses in an easier to understand way by directly naming the methods responsible for those responses by the name of the HTTP verb itself! When a web application gets hundreds of lines of code deep, this can be extremely helpful and makes the code relatively simple to understand.

Resources

Flask RESTful

Flask SQLAlchemy

Flask

SQLAchemy

Python

--

--