My presentation today on Python Web Frameworks is in some ways the final form of investigation into IoT and the Raspberry Pi.
My interest in the subject was initially sparked by the presentation Deid gave back in 2016 on how to turn a light bulb on and off using a relay being driven by a Raspberry Pi.
This amazing display ended up leading me to my first presentation on the subject back in October 2016. Because while I was amazed at the ability to trigger hardware functionality through a self-hosted webpage, I found the way Apache is able to interact with python code way too clunky.
In that presentation I showed how you could leverage a tool like Adafruit.io to manage the state of a number of IoT-connected devices. This method leveraged their python client library to be able to interact directly with other python code you have on a device. And while this is a very powerful tool, it’s reliance on an internet-based third-party tool is a bit of a tough pill to swallow from a security perspective.
With this presentation I hope to address these security concerns, while also providing an improved way for people to manage and control their IoT devices.
This improvement comes in the form of Python Web Frameworks. The advantage these tools provide is that not only are the self-hostable, meaning there is no reliance on a web-based service, but they also can interact directly with other code outside the web server.
What is a Python Web Framework?
The description of a Python Web Framework from python.org describes it as “a collection of packages or modules which allow developers to write Web applications or services without having to handle such low-level details as protocols, sockets or process/thread management.”
The idea behind this being that web frameworks should be able to handle all of the standard things you would want to do with a webserver like:
- Interpreting requests (getting form parameters, handling cookies and sessions)
- Producing responses (presenting data as HTML or in other formats)
- Storing data persistently (in a database)
On this same page on python.org there is an exhaustive list of available Python-based Web Frameworks for you to choose from.
The Flask Web Framework
With so many available options for Python Web Frameworks which one I ended up using was really a matter of the one I was introduced to first. This turned out to be Flask.
This simple, but powerful, Python Web Framework provides basically everything I would ever need for my hobby projects, while also being well documented and easy to use. It supports connecting to databases, making and receiving RESTful requests, IPv6, both Python 2 and 3.3+, and makes use of Jinja2 for templating.
The simplicity of Flask can be easily demonstrated with the following Hello World example:
In the file hello.py:
from flask import Flask
app = Flask(__name__)
return “Hello World!”
if __name__ == “__main__”:
All you need then is to install flask `pip install Flask`, and then run the script `python hello.py`
Structure of Flask
Now, while that “hello world” example was a good demonstration of just how quickly you can be up and running with Flask, if you are building anything more complex, you will want to add a bit of structure to the project.
This structure and organisation comes in a number of parts, and there is a great beginner guide for Flask which walks through each of these sections in details. As a quick overview though, these sections are:
- Run.py – Starts the Flask server and launches the application
- /app – contains all the files for the application
- /app/templates – contains the template files that views will call
- Views.py – contains the application routes. What to display on which path
All in all, Flask makes for a a very straightforward and easy to understand way of displaying/serving web content.
Having covered the basics of what Python Web Frameworks are it is time to look at a couple of real-world examples of projects that have been created using them.
I have put together three projects that I have worked on which use a Python Web Framework.
This Flash-based website isn’t limited to being run on a pay (basically anything that can run python) and doesn’t need to be connected to the web as it can still run code over LAN.
In this specific example I am using the webpage to update the state of a relay status in adafruit.io as I didn’t want to have to rebuild all the code I was using to drive the relay directly rather than over the web.
The code for this project can be found on github.
This project was one inspired by my brother’s interest in ship, and the movement of the ones in the Lower mainland. I used this idea for a proof of concept project to organise and display data.
In this case I am using “publicly” available Vancouver Port ship data (intended for use in their mobile app) which I am scraping, formatting, and then making available on a webpage.
The code for this project can be found on github.
My battlesnake project was the the 2017 edition of Battlesnake in Victoria.
The idea of the event is that you run a run a client webserver to power your snake. This webserver needs to accept POST requests:
– `/game route` and return snake name, colour, and taunt
– /turn route and return direction and taunt
Technical details about the game rules and snake requirements can be found in the battlesnake docs.
The main goal of this project is to see who can program the best AI for their snake, not who can make a working webserver. So while I don’t have a publicly available link to the code that I can publish, the example python snake provides a good example of what the webserver code would look like before you start writing your AI.