DEV Community

Cover image for Flask Routes vs Flask-RESTful Routes
Cristian Alaniz
Cristian Alaniz

Posted on

Flask Routes vs Flask-RESTful Routes

You probably have experience with basic flask routes if you are reading this. Perhaps you are new to the idea of routes and are curious about the differences and similarities.

In this article i'd like to compare flask routes and flask-restful routes on a syntactical level.


What are Routes?

Routes are made up of 3 key components:

  • URL Path
  • Server Resource
  • HTTP Methods

They are used as communication channels between a client and a server.

URL Anatomy

URL Path

In both regular flask routes and restful routes the URL path is the specific address on the server that the client requests. An example could be '/home'.

The difference lies in how they are defined.

Flask Routes

Defining a URL path in a flask route using the home example would look like this:

@app.route('/home')

The actual URL path is passed an argument to the route method.

The decorator is used to bind a function (which is declared under this line and will be discussed shortly) to the URL path.

Flask-RESTful Routes

Defining a URL path via a restful route would look like:

api.add_resource(Home, '/home')

The URL path is passed as the second argument to the add_resource method.

The first argument is the server resource associated with the URL path.

Servers

Server Resource

This is where the magic happens. The logic and actions are defined here. Each resource receives a request and returns the appropriate response.

The key difference lies in the way flask and flask-restful think about the server resource.

Flask Routes

With a regular flask route, the "server resource" is a function.

def home():
    return "Welcome to the homepage!"
Enter fullscreen mode Exit fullscreen mode

Flask-RESTful Routes

Flask-RESTful takes a class-based approach to defining a "server resource". The class inherits from the RESTful class Resource.

class Home(Resource):
    def get(self):
        return "Welcome to the homepage!"
Enter fullscreen mode Exit fullscreen mode

HTTP Methods

HTTP Methods

Last but not least, both flask routes and restful routes take HTTP methods. These methods help to further specify a clients request, ensuring the request is handled by the correct server resource.

Once more, the route types differ in how they define HTTP methods.

Flask Routes

Flask routes takes the approach of defining HTTP methods in the same space where it defines the URL Path.

@app.route('/home', methods=['GET'])

Flask-RESTful Routes

Flask-RESTful takes the approach of defining each HTTP method as a method of the server resource class.

def get(self):

You may have caught this in the server resource example.


All Together

Flask Route Example

@app.route('/home', methods=['GET'])
def home():
    return "Welcome to the homepage!"
Enter fullscreen mode Exit fullscreen mode

Flask-RESTful Route Example

class Home(Resource):
    def get(self):
        return "Welcome to the homepage!"

api.add_resource(Home, '/home')
Enter fullscreen mode Exit fullscreen mode

Conclusion

Flask routes and Flask-RESTful routes are both viable options to establish communication between a client and a server.

Beware as there are still a lot of differences we didn't cover. One big difference is that regular flask routes do not follow REST principles but can be made to follow them unlike their counterpart that naturally do. Other differences include regular Flask compared to Flask-RESTful and configuration, among other things.

Check out these articles to dive deeper into the differences:
What is an API
What RESTful Actually Means
Setting up Flask
Setting up Flask-RESTful
HTTP methods

Top comments (0)