I thought I would start off with some tutorials on python using my raspberry pi. I should mention I am far from an expert on the Raspberry Pi, flask or even python. I am just learning and these are my notes (in tutorial form) up here for everyone to see. My hope is that that anyone correcting my python will better my own coding, and help others too.
First up we are going to start with setting up flask on a standard Raspbian install.
I am going to assume that you have a raspberry pi with the latest version of Raspbain installed. If your not make sure you have python 2.7 or better installed on your system at the very least.
Ok here we go!
We will install flask using a virtual environment. This is so that your main Python installation is not affected by any of the extensions or flask itself in the event anything goes wrong. This is only my preference to work this way. I could also add that you don’t need to have root access to get this running!
So to start you want to make a folder to house your website/app. Open up the terminal window, choose where you want to store it. My example will be in ‘/home/pi/website’
Now download the virtualenv.py and put it within your new folder ‘/home/pi/website/’
Install the python virtualenv package
apt-get install python-virtualenv
Move into that website folder if you have not already done so.
Next create the virtual environment by entering the following command:
This will create a complete Python environment inside a folder called flask! Closer and closer to having our site done!
There are 2 ways to do things now. You can either activate the virtual environment and then run the next commands without the path names (which I don’t like to do). Or you can instead just invoke the interpreter you want by typing the pathname. The benefit of invoking the interpreter is that if you have more than one environment running you don’t have to keep activating and deactivating when working on more than one project.
So finally lets download and install flask
flask/bin/pip install flask
Assuming everything is ok you can just go ahead and remove the ‘virtualenv.py’ file now we won’t need it anymore.
“Hello, World” in Flask
Now you have a project folder ‘website’ and you have a sub-folder with flask and a Python interpreter. We can now get started with a Hello World. Just to prove its all working.
Within your website folder lets start making some folders.
The templates sub-folder is for templates… ok self explanatory.
Why make all this… Well later when I give more tutorials you will want to have these folders in place and ready to use.
We need to create a init script in our app folder ‘app/__init__.py’ which will create the application object (class of Flask) and then will import the views module (going to take care of that in a moment)
from flask import Flask
app = Flask(__name__)
from app import views
The views are handlers that respond to requests from web browsers. In Flask views are written as Python functions.
Each view function will be mapped to one or more requested URLs.
So lets write the views function ‘app/views.py':
from app import app
return "Hello, World!"
There it is hello world! But we are not done yet. This view is just returning a string that will be displayed in the web browser.
The two lines @app.route(‘/’) and @app.route(‘/index’) are route decorators to create mappings from the URLs ‘/’ and ‘/index’ to the function below it.
Finally we want to get this serving to the web so we need a script to get the development web server running.
We need to make the file run.py in the website folder ‘website/run.py’
from app import app
app.run(debug = True, host="0.0.0.0")
The script will import the app variable from our app package and then invokes its run method to start the server.
Remember that the app variable has to hold the Flask instance we created before.
The ‘host=0.0.0.0′ is optional. Basically if you don’t have it flask will only accept requests from localhost or 127.0.0.1 (basically the browser on the raspberry pi itself) having the 0.0.0.0 will mean that flask will accept any request from anywhere. This is better to have if you are running the pi as a headless system and you want to test on another computer.
Just before we can run the app we need to indicate that this is an executable file:
chmod a+x run.py
Then the script can simply be executed as follows:
You will see that the server is now running and listening on port 5000. Now you can fire up your web browser and enter the following URL (changing the IP to your raspberry pi’s IP):
YEAH!!! You did it! Hello World!
Remember those route mappings from before? This means that after the port number you can enter ‘/index’ and you should also get to your hallo world page, neat huh!
Then when your done mucking around with the server you you can just hit Ctrl-C to stop it.
Whats awesome about this setup is that you can leave the server running and just simply edit any file and it will reload live to the server.. so make a change and hit refresh and you should see the change. But with one exception…. Exceptions… If you code something wrong and save it, when flask takes the new version of the file and reads it. It might crash.
Next time I want to start on some basic templating with Bootstrap and Jinja2
See you later!