Skip to main content

Intro to Python Flask Microframework

The ever growing internet of things provides expanding opportunities for software engineers to leverage their skills. If you are not writing software for the devices themselves there are still ample opportunities to build backend systems to capture and analyze the avalanche of data that these devices produce. 

In this post we will be looking at creating a backend system for capturing connected device data for a hypothetical IOT device. We will be using Python and a lightweight framework called Flask to create a REST based endpoint that our device calls to submit location data. Our service will store its data in a NOSQL repository powered by Azure Cosmos DB. You can find the repository for this post here.

One of the big trends in software engineering is the rising popularity of Python. This multipurpose language has a dominant position in the machine learning space. If you are looking to build some skills in Python but are not currently working on a machine learning project you may want to look at some other areas that leverage python. One of these is building web backends - an area that you may already have experience. The Flask framework makes getting started building REST based API’s a snap.

Flask

Flask is a popular Python web framework. It has an easy learning curve particularly for anyone coming from another language that is already familiar with REST based services. Setting up routing and adding API endpoints is a straightforward process. Below is a snippet for the endpoint we are creating. You’ll notice several similarities with Web API attribute based routing.


Couple things to note about Flask. Firstly it is very modular for example by default you don’t have access to the request object. In order to get access to the request object you need to add a reference to the request library. This capability allows you to add overhead for only the features your application requires. Secondly, initial compiles of Python in Visual Studio can be slow. Microsoft is continually improving performance of the tool and with the recent hire of the founder of Python I would expect the rate of improvements to accelerate.


Cosmos DB

Azure Cosmos DB is a fully managed NoSQL database for modern app development. Single-digit millisecond response times, and automatic and instant scalability, guarantee speed at any scale.
There is a python library provided by Microsoft that makes working with Cosmos a straightforward process. For our project we will create a repository to contain all or Cosmos Db integration. Below is the code. If you have worked with Cosmos dB in another language like C# this code will probably look at least somewhat familiar.


You will need to provision a Cosmos db container and database. Fortunately, you can Try Azure Cosmos DB for Free without an Azure subscription, free of charge and commitments or use the Azure Cosmos DB free tier to get an account with the first 400 RU/s and 5 GB of storage free. The free R/U level represents the amount of allowed throughput for your free instance; it is adequate for basic development.

Once you have your Cosmos db provisioned we are ready to test our endpoint. You will need to add the configuration data for your instance into the config.cfg file found in the service folder.


Testing

Common REST based testing tools like Postman work great for testing a Flask application. This will allow you to leverage your current skills with these tools.  The image below shows setting up the call with Postman.  Our endpoint expects three parameters the deviceid, location (longitude, latitude) and the time stamp. 

Data Explorer

Once you have sent you first request to the end point you can see the data in the Cosmos dB data explorer in the Azure Portal .



Learn More

Want to learn more here are a couple of courses that I found useful.


Flask

https://app.pluralsight.com/library/courses/flask-micro-framework-introduction/table-of-contents 

Python Machine Learning

https://www.udemy.com/course/python-for-data-science-and-machine-learning-bootcamp/



Comments