Python - RESTful server with Flask
30 Oct 2017The Flask library for Python is a great microframework for setting up simple web servers. Larger sites or REST interfaces might want to tend towards the Django framework instead, but I’ve found Flask excellent for putting together small sites or a couple endpoints with next to no effort. The API is very Pythonic so of course you can get up and running with very few lines of code. I currently use Flask for the backend API services for this site - which powers the search page, contact page and automated Jekyll builds using Github hooks.
Installation
To install and start using Flask, just use pip
:
$ pip install Flask
Simple Example
The most basic endpoint looks like:
We imported the main Flask class and created a new instance passing in the name of the current module as an identifier (so Flask knows where to look for static files and templates). A simple function, which in this case just returns a String
, can be decorated with route
to define the URL which will trigger the function.
Running on a development server
There are a couple ways to run the above example. The first is the way recommended by the Flask team:
$ export FLASK_APP=hello.py
$ flask run
* Running on http://127.0.0.1:5000/
This is fine on Linux boxes (you can also use set
instead of export
on Windows), but setting an environment variable on Windows is a bit of a pain, so instead you can start the server via code. Apparently this might cause issues with live reload, but Flask starts up so quickly it’s not too much of an issue:
You can then navigate to http://localhost:5000
and you will see the return value of the hello_world
function. You can easily return HTML
or a JSON
objects as needed depending on what services you wish to build.
Handling GET Requests
I have focused mainly on using Flask to create basic RESTful web endpoints instead of serving HTML - which Flask can do very well using the Jinja2
templating engine. The below snippet shows how to create a simple endpoint to handle GET
requests to retrieve a user by their unique id. The returned object from our dummy service is converted into a JSON response via the built in jsonify
function:
Handling POST Requests
The below snippet shows how we can handle a POST request
, taking in a JSON
object and returning a response from our service:
As you can see, setting up simple endpoints is very quick and easy using Flask. The framework also offers a ton of other useful features including:
- built-in development server and debugger
- integrated unit testing support
- RESTful request dispatching
- Jinja2 templating
- support for secure cookies (client side sessions)
- great documentation