Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using basic Flask vs Flask-RESTful for API development

Tags:

python

flask

api

I'm about to develop a REST API for our upcoming application. I have decided to use Python Flask for it. But at this point, I don't know which option to use. Should I be using the basic Flask package or Flask with Flask-RESTful extension. I've found some advantages and disadvantages in both.

Below is an example of two APIs doing the same thing but in Flask and Flask-RESTful:

Flask version:

from flask import Flask, jsonify  app = Flask(__name__)  usersList = ['Aaron', 'Bianca', 'Cat', 'Danny', 'Elena']  @app.route('/users', methods=['GET']) def users():     return jsonify({ 'users': [user for user in usersList] })  @app.route('/user/<int:id>', methods=['GET']) def userById(id):     return jsonify({ 'username': usersList[id]  })  @app.route('/user/<string:name>', methods=['GET']) def getUserByName(name):     # Show some user information     return "Some info"  @app.route('/user/<string:name>', methods=['POST']) def addUserByName(name):     usersList.append(name)     return jsonify({ 'message': 'New user added'  })  app.run() 

Flask-RESTful version:

from flask import Flask from flask_restful import Resource, Api  app = Flask(__name__) api = Api(app)  usersList = ['Aaron', 'Bianca', 'Cat', 'Danny', 'Elena']  class UsersList(Resource):     def get(self):         return { 'users' : [ user for user in usersList  ] }, 200   class UserById(Resource):     def get(self, id):         return { 'username': usersList[id] }   class UserByName(Resource):     def post(self, name):         usersList.append(name)          return { 'message': 'New user added'}   api.add_resource(UsersList, '/users') api.add_resource(UserById, '/user/<int:id>') api.add_resource(UserByName, '/user/<string:name>')  app.run() 

Using Flask-RESTful, I cannot get a single resource to serve multiple related endpoints like GET /user/<int:id>, GET /user/<string:name>, GET /user/<int:id>/friends etc. And I don't know if creating new classes for simple sub-resources is a good practice because I'll probably end up with many classes. Due to this reason, I'm more inclined towards using just Flask since only functions are defined and the endpoints can be freely defined as per my needs.

Keeping the above in mind, is it okay to create many classes for sub-resources in Flask-RESTful? Or am I better of using Flask? Or Flask-RESTful provides some really good advantages over Flask?

like image 254
Shahlin Ibrahim Avatar asked Dec 14 '18 10:12

Shahlin Ibrahim


People also ask

Is flask RESTful better than Flask?

Flask is a highly flexible Python web framework built with a small core and easy-to-extend philosophy. Flask-Restful is an extension of Flask that offers extension to enable writing a clean object-oriented code for fast API development.

Is Flask GOOD FOR REST API?

Flask does not have a good Browseable API option. The creator of Django REST Framework created a similar library for Flask called Flask-API, but it is not under active development nor is it ready for production use.

What is the difference between Flask and REST API?

Django REST framework is a powerful and flexible toolkit that makes it easy to build Web APIs. On the other hand, Flask is detailed as "a microframework for Python based on Werkzeug, Jinja 2 and good intentions". Flask is intended for getting started very quickly and was developed with best intentions in mind.

Can you build APIs with Flask?

A Comprehensive Guide for building Web APIs with FlaskFlask is a widely used micro web framework for creating APIs in Python. It is a simple yet powerful web framework which is designed to get started quick and easy, with the ability to scale up to complex applications.


2 Answers

REST is a pretty flexible architecture, but there's a few points worth thinking about in your approach using just Flask, which Flask-RESTful is encouraging you away from:

  1. By convention, a GET on a single resource (e.g. /users/1234) is by a unique identifier for the resource. The name of a user can't be guaranteed to be unique, so it would be risky to have it as an identifier within the URI (e.g. /users/joe).

  2. When you access users within a collection, it's better to stick with the plural noun throughout (not, as you show in your Flask example, /user/...).

  3. When you're creating and using POST, unless your client is specifying the id (in which case it has to be able to guarantee uniqueness, so pretty much the only valid id would be a UUID), you can post to just the collections URI (e.g. /users/).

Either will work, but with Flask-RESTful you'll find that when you follow those guidelines, your class matches your resources much more closely and you won't see the proliferation of classes you describe.

A very similar use case is demonstrated at https://flask-restful.readthedocs.io/en/latest/quickstart.html#full-example.

like image 102
rich carter Avatar answered Oct 07 '22 08:10

rich carter


I would recommend you to use Flask-RESTplus instead, that will give you full Swagger support.

In regards of just using Flask, I would say getting the Swagger functionality is a big one to choose Flask-Restplus also.

like image 31
StefanE Avatar answered Oct 07 '22 08:10

StefanE