In this tutorial, we will check how to start a very simple Flask server on the Raspberry Pi and return a “Hello World” message to clients that send a request to it. This tutorial was tested on a Raspberry Pi 3 model B+, running version 4.9 of Raspbian, installed using NOOBS.
In this tutorial, we will check how to start a very simple Flask server on the Raspberry Pi and return a “Hello World” message to clients that send a request to it.
Flask is a micro web framework for Python which we can use to develop simple web applications very easily and quickly. You can check this previous post for a more detailed introduction to Flask.
Being able to run Flask on the Raspberry Pi opens the possibility for very interesting applications. For example, we can use this powerful mini computer as a web server to receive data from nearby IoT devices and do some pre-processing before sending it to the cloud.
Flask is a module that doesn’t come by default with Python installations, so we need to install it before starting to write the code.
One of the easiest ways to install Python modules is by using a package manager. In our case, we are going to use pip, which makes the task of installing Flask as simple as sending a command in the command line.
In version 4.9 of Raspbian, pip comes installed by default. If your version doesn’t include it, you can check here a guide on how to install pip.
You can check if you have pip installed and what is its version by sending the following command on the command line:
If you have it installed, it should output the version, as shown in figure 1.
Figure 1 – Checking pip version.
Assuming that you already have pip, to install the Flask module simply send the following pip command on the command line:
pip install flask
After the installation finishes, you should be able to import Flask on Python programs.
The first thing we need to do is importing the Flask class from the flask module we have just installed. You can read more about this class in the module documentation.
from flask import Flask
Next we will create an instance of the Flask class, which we will call app. As argument, the constructor receives the name of the package or module of our application. You can read more about the importance of this parameter in the documentation, on the “About the First Parameter” section.
In our case, since our application is very simple, this parameter doesn’t have impact. Thus, we can pass as argument the value __name__, which is a global variable that holds the current module’s name as a string .
app = Flask(__name__)
Next, we need to define a route for our server. A route is a decorator that allows us to bind a URL to a Python function. When a request is made to that route, then the function is executed.
Python decorators are a more advanced concept that we don’t need to worry about for this tutorial. You can read more about them here.
Note that we can declare multiple routes for a server. In our case, since this is an introductory tutorial, we will just use one, called “/hello”.
Routes are a very important concept when developing Flask applications and there are many configurations we can leverage, such as specifying the HTTP methods allowed on a specific route, amongst many other options.
Right after the decorator, we need to declare our function. It will take no arguments and we will call it helloWorldHandler. Nonetheless, you can name it differently if you want.
@app.route('/hello') def helloWorldHandler(): #function code
The implementation of the function will simply consist on returning a “Hello World” message. This message will be returned to clients who make HTTP requests on the “/hello” route of the Flask server.
@app.route('/hello') def helloWorldHandler(): return 'Hello World from Flask running on the PI!'
So far we have jst configured the server route, but the server is still not running. In order for the server to start listening to HTTP incoming requests, we need to call the run method on the Flask object we have instantiated in the beginning of the code.
As arguments of this method, we can specify both the host and the port where the server will be listening to requests. The method arguments are called host and port, respectively.
As host, we will pass the loopback address, which is the 127.0.0.1 IP. This means that the server will only be available locally in our machine. The host is passed as a string.
As port, we will use 8090. This argument is passed as an int.
app.run(host='127.0.0.1', port= 8090)
The final source code can be seen below.
from flask import Flask app = Flask(__name__) @app.route('/hello') def helloWorldHandler(): return 'Hello World from Flask running on the PI!' app.run(host='127.0.0.1', port= 8090)
Testing the code
To test the code, run it on your Python environment of choice. If you are using IDLE, you can check this previous post on how to run Python scripts.
Once you run the code, you should get an output similar to figure 2. Note that the debugging messages indicates that the server is listening on the 127.0.0.1 IP, on port 8090. This is precisely as we have configured on our code.
Figure 2 – Running the code on the Raspberry Pi.
As mentioned before, the server is only working locally on the same machine that is running the server. So, our testing client can only reach the server if it is also running on the Raspberry Pi.
The easiest way to test the server without the need to write code for a client is by using a web browser to make a HTTP GET request on the route we have defined.
So, open a web browser of your choice on the Raspberry Pi and type the following on the address bar:
You can check the expected result on figure 3. As can be seen, you should get the “Hello World” message we have defined in the code.
Figure 3 – Making a request to the Flask server using Chromium.
- Raspberry Pi 3 Raspbian: Running Python scripts on IDLE
- Raspberry Pi 3 Raspbian: Running Python from the command line
- Raspberry Pi 3 Raspbian: Python Hello World with IDLE
- Flask: Hello World
- Flask: Controlling HTTP methods allowed
- Flask: Basic authentication
- Flask: Parsing JSON data
- Python anywhere: Deploying a Flask server on the cloud
- ESP8266: Posting JSON data to a Flask server on the cloud
- LinkIt Smart Duo: Running a Flask server