Weather According to Raspberry Pi (WARP)
(Please note that this tutorial is based on a previous on about Flask and edited specifically for the WARP project. If you think you have read it before, then move on to the next one.)
In a previous post I described the WARP mini-project and gave you the files to make it work on your own Raspberry Pi. This post describes how it works by using the Flask framework.
Most web applications are written in PHP, Java, Ruby or one of Microsoft’s languages like C# and use a web server such as Apache or IIS. Flask let’s you do the same thing with Python.
1. You have set up your Pi with the Raspbian operating system and it is connected to your home network.
2. You are reasonably familiar with Raspbian and you have, at least, experimented with programming in Python (maybe you’ve gone though the Just Enough Python tutorials) .
You don’t need a high level of skill to follow this tutorial but you do need to have played around with your Pi and with Python.
One more thing you need to know before we start is that I am using Python 3 in this tutorial. This is important as there are differences between Python 2 and 3, which while not enormous, will mean that the instructions in the tutorial won’t work if you use Python 2.
Getting the Pip
In order to get Flask, we first of all have to make sure that we have pip, the Python package installer. It’s probably there already; to check open a terminal window and type
pip3 into it. If it responds with a whole load of help text, then you’ve already got it, otherwise use
apt-get to get it. Type this in the terminal:
sudo apt-get install python3-pip
When that’s all done we can get Flask simply by typing this is the terminal:
sudo pip3 install flask
This may take a little while but when it is complete Flask is installed and we are ready to begin in earnest.
Your first Flask program
Let’s be organised about this and create a new folder for this tutorial. Either use the file manager to do this or type
mkdir newdirectoryname in the terminal. Now cd to the new directory and create a new Python file called
main.py (touch main.py). Now open an editor, or use IDLE, to start creating your first program. We are going to make this very simple, just to see how Flask works. Type in the following program:
We’ll go through how it works shortly, but first, save the program and run it (either from IDLE or by typing
python3 main.py in a terminal window). The result you will get is this:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now, open a web browser and, for the address, type
What you are doing here is loading the web page on your local server at port 5000. If that doesn’t mean much to you, don’t worry. In simple terms, the Flask program that you are running is a web server and the page that is displayed in the browser is what you have programmed it to be.
Let’s look at it a bit more closely.
The first line imports the Flask library; simple enough. The second line creates a Flask object called app and this is the thing that does all the work.
For the moment skip the line beginning with @.
The next couple of lines define a function called hello that returns a message. And the last couple of lines calls the run function, from the app object, when the program is run.
So what does all that mean?
The app.run() function starts a web server so when you open your browser, and give it the right address, a web page is displayed (it helpfully tells you what the address is). And the content of the web page is defined by the function hello.
That’s where the line
A line beginning with @ is called a decorator and it tells the Flask library that the function that follows is associated to the route “/”. If that line had been
Then the route would have been “/hello/” and you would have had to type
in the browser to to display the right page. You can try this if you like.
If you found this post interesting, please share it with your friends.
Find out more about Flask.
Just Enough Python
An brief introduction that will get you programming quickly