Programming

Introduction

Python is a great and popular programming language and it’s widely used in nearly all information technology companies today. In fact, most top-level tech companies are using and leveraging Python as part of their technology stack.

Although Python is mostly used in computational and AI-based projects, some companies are taking it’s powerful yet simple capabilities to develop web applications.

If you have developed web applications with Python, you might have experience working with Django and Flask web frameworks; actually, they are considered the most popular frameworks. There are several successful projects out there that has been developed with these frameworks and I believe the most important factor that these two got so many attentions is their powerful template engine! Django uses its own template engine while Flask makes use of Jinja to render HTML web pages. This powerful feature allow user to easily develop their application logic as well as frontend section at the same time.

But things have changes during past couple of years. Companies have started to separate the backend side of their applications from the frontend section. This separation causes significant benefits as I mention some of them here:

The project codebases will be much lighter, cleaner and as a result more readable.

There would be specific people for specific tasks. precisely, you don’t need to give everyone access to all layers of your codebase.

There would be only one API endpoint for multiple usage. When frontend and backend sections tie together, the frontend section has direct access to application logic! Now assume that you need to develop mobile application(s) for your solution as well. In this case, you’ll need an API set–commonly RESTful APIs– to enable mobile devices to communicate with your server; therefore, your team have to develop new API sets for mobile devices which could leads your project to serious redundancy issues.

To be able to get along these kind of issues, frameworks mentioned above have released other libraries to handle the situation. For example, Django now has a Django REST framework that enables developers to deliver RESTful APIs. On the other side, Flask has the built-in capabilities to return JSON data rather than HTML:

Despite the fact that this will solve the issue in Flask applications, I believe there are better options to use! That’s why this series of posts comes in.

What is Falcon?

Beside frameworks I’ve mentioned in the introduction section, I have also tried some other frameworks so that I can select the best one for my workflow and I think Falcon is the most usable one.

Falcon is a minimalist WSGI library for building speedy web APIs and app backends. We like to think of Falcon as the Dieter Rams of web frameworks.

As the description implies, Falcon is a minimalist web framework. This means that the team behind it developed Falcon with simplicity in mind. In addition to simplicity, Falcon is fast! it’s actually faster than almost all of its competitors:

Enough talking, let get started.

Hello World app

Before getting started, please note that it’s a best practice to use virtualenv; otherwise, packages will be install globally. I also recommend to use Python 3.

To get started, simply install it using pip:

pip install falcon

Then, create a Python file named posts.py in your favorite text editor, add the following and save it somewhere in your computer:

To be able to run Falcon-based programs, you will need a WSGI server. I am pretty comfortable with gunicorn but there’s no limitation and you can use whatever WSGI server you want. To use Gunicorn, install it using pip install gunicorn. Once the installation completed, you will be able to run your program in the terminal:

gunicorn posts:api

Now you may access the API we’ve written above using http://localhost:8000/posts.

Conclusion

Falcon is a great framework to develop RESTful APIs in Python. It’s simple, lightweight and extremely fast and I highly recommend using it. In future posts I will dig deeper into the foundations of Falcon and how you can implement all sorts of RESTful APIs.