The simplest way to Build REST API with Falcon and Python 3.10: Application in Spatial Geometry | by Louis Brulé Naudet | Towards Data Science

Deploying a REST API in Python on a remote server can be a difficult step in producing a functional application. The purpose of this article is to propose an extremely simplified code, of the configuration of a Falcon module, in particular for the beginners in web programming wishing to put at their disposal tools of remote calculation. In ten minutes, your first server will be operational, despite basic knowledge of Python 3. To make this article even more attractive, let’s look at a real problem, that of positioning in space and calculating distances between two points in a three-dimensional frame of reference. Useful in the context of a modeling or robotics project, the program below will allow the reception of a JSON request, and the sending of a response in the same format, after application of a mathematical theorem on a remote machine, equipped with a Python 3.10 environment with updated libraries.
Recommendations
Compatibility test performed with Python 3.10, executed on MacOS 11.5.1 and Linux Ubuntu Server 20.04 LTS environments.
Libraries Used : Falcon, Gunicorn, Math, Json.
pip3 install falcon gunicorn
The source code will first observe the presence of the libraries necessary to its execution, then, download them in case of absence in order to automatically update the software configuration of the execution environment.
In order to make the program executable with the minimum of human intervention, and to optimize its deployment on the server, an autonomy of upgrade, data download and exception handling has been implemented, notably within the server_SMTP class.
First, it is necessary to initialize our Gunicorn server, on a listening port not used by the system, in our case, port 5000 seems a perfect candidate and should not cause any inconvenience. To do this, enter the following command in a terminal window :
gunicorn -b 0.0.0:5000 main — reload
Note : It is essential to name your file main.py for the execution of Gunicorn, a different name will cause an exception and will not be able to reach a server launch.
Then, you have to configure your application in the main.py file, using a set of variables pointing to the Falcon library.
api = app = application = falcon.API()
From this point on, you are dependent on Falcon’s syntax, especially in handling the requests and responses that your. For more informations about Falcon, please visit the library’s website (https://falcon.readthedocs.io). For example, to interpret a request in JSON format, the use of the “stream” class will be a must :
request = json.loads(req.stream.read())
In order to test the response of our server and the correct execution of the program, we will use Insomnia, an Open Source API Client.
insomnia.rest
Once the software is launched, set the method to “POST” in the drop-down menu on the left pane, then insert the target URL of your server. In our case :
http://0.0.0.0:5000/DistanceProcessing
The final step is to formulate our JSON query. We will, for our example, send in the “body”, a list of lists, containing the coordinates of two points in a 3 dimensions frame. Simultaneously : [x position of point B, y position of point B, z position of point B] and [x position of point A, y position of point A, z position of point A].
{ “body”: [[0, 0, 1], [1.5, 2.5, 1.20]]}
Let’s consider an application in robotics where the localization of an object in space can be an essential function. If we allow the object to be located to have coordinates close to a 3-dimensional reference frame, the formula to be applied remains simple :
On the programmatic level, it will be possible to write a simple function, allowing the application of the above formula :
For the response returned by the POST method, we will use the standard status 200, and the “dumps” function of the Json library.
The construction of API can be used for many applications, you now know the basics of its development, and this introduction allows you to build your first architectures, especially in the perspective of creating programming interfaces dedicated to Machine Learning, or remote computing.
Louis Brulé Naudet, holds a double degree in Law and Economics/Management from the University of Paris-Saclay.