How to turn your Raspberry Pi into a Flask App Server

Published on: April 20, 2019

Welcome to another Raspberry Pi Tutorial! In the last tutorial, I showed you How to install Apache on a Raspberry Pi. In today's tutorial, we will take another step and install Flask on our Pi. Flask will serve as an application server that will allow us to run Python. And with Python, we can control an LED. 

I showed you how to turn on an LED using two different libraries in a previous tutorial. At the end of this mini-series, we'll turn our LED on and off with our browser. 

In the next tutorial, we'll learn how to run Flask behind Apache, then finally, we'll create a quick application that we can access and control and LED on a Pi. 

So first thing is first. let's install Apache. If you want to skip the video below for the steps, then feel free to follow these steps. This tutorial assumes you already know how to run your Pi in Headless mode.

Before we continue with installing Flask, we want to set up a Virtual Environment. Why?

Virtual Environments allow us to keep our applications separate from one another. It also lets us keep what we install in this environment separate from the global set up. So if we mess anything up, just destroy this virtual environment and create another!

To install a virtual environment, run this command:

sudo apt install python3-venv

This will install the python3-venv package on our Pi. Then we want to create a virtual environment. You can install this anywhere but I decided to install the virtual environment in the www path created by apache. So let's create a quick folder:

cd /var/www
sudo mkdir piApp

This creates a blank folder called piApp. You can call this anything you want. Now we'll install venv:

sudo python3 -m venv venv

You can name the virtual environment folder anything you want, I called it venv. We need to then activate our virtual environment with this command:

. venv/bin/activate

To deactive, you can run deactive and you will exist the virtual environment. 

Your terminal should now be prevised with (venv), which means that any action you take here, will only happen in your virtual environment. There's one quirk that I ran into while installing flask. You may also run into a weird permission error when trying to install flask. When your venv is created, the environment is owned by root. So if you run into this issue, deactivate your virtual environment then run this command to change the owner of the environment before proceeding:

sudo chown -R pi:pi venv

Then reactivate your venv and continue. Now let's install Flask:

pip install flask

If everything goes well, you'll get a success message at the end. Now let's create a simple Python script that will serve as our root Flask app. I called it hello.py but you can call it anything you want. 

sudo nano hello.py

In the file, add this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

The code is taken directly from the Flask documentation found here: http://flask.pocoo.org/docs/1.0/quickstart/

For more information on how Flask works, I'd recommend reading their documentation. If you are familiar with any other MVC framework, this should be similar. I won't go too deep into the code. 

Save the file and exit. We need to then assign our hello.py script to a variable called FLASK_APP because this is the variable that Flask looks at when you run. To do this type:

export FLASK_APP=hello.py

If you named your Python script anything else, change the name. This will serve as your main point of entry for your Flask app. When done, do:

flask run

After a few seconds, the app should run successfully and let you know that it's running on http://127.0.0.1:5000. If you try to access this URL from the Pi, you will be able to run it. But if you want to access it from anywhere in your network, you won't be able to. So ctrl+c out of the run and type this:

flask run --host=0.0.0.0

This will run the Flask app publicly within the network. You can then access your flask app using the IP address for your Pi. In my case it was http://192.168.1.196:5000

You can go back to the Python file and add other routes and do so many things with it. You may already have an idea on how to control and LED from your browser by combining this tutorial and the tutorial showing you how to control an LED! 

In the next tutorial, I will show you how to run Apache in front of Flask so that we can create a simple web page to control our lights!

That's all! Have questions about the tutorial? Ask below, I welcome all questions and comments.Come back for the next few tutorials to learn more. And be sure to subscribe in order to stay up to date. On Pi Day (3.14.19), i gave away 4 Raspberry Pi Zero Ws to my subscribers! I didn't announce this anywhere else. So subscribe to stay up to date on all news. 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:

No resources from Easy Programming for this video! Follow me on Github for other resources https://github.com/naztronaut

But be sure to check out the Flask documentation here for a better understanding of how to use Flask: http://flask.pocoo.org/docs/1.0/quickstart/



Comments: