Hi All
More on how Flask works and updates
I haven't seen flask used in the Galileo community, which is a shame as it is an easy way to put the Internet into the Internet of things. Specifically you can display information from you Galileo on a web page or control it from the web page.
I have been busy working on getting Python to run and access GPIO, control servos and display info on LCD screens. See Python, Wiring_x86, I2C LCD, Grove RGB LCD
https://communities.intel.com/thread/57741?sr=inbox, so can now make the Galileo actually do interesting things in Python.
Update
nano is now in AlexT's repository, so you just need to run
#opkg update
#opkg install nano
If flask didn't install correctly you will get errors, most likely something like:
from werkzeug.exceptions import abort
This probably means that you are far from the center of the Internet, like me, and easy_install didn't work. You can try again or you can then install pip, which is another great little tool for getting applications for your system.
Run:
#easy_install pip
then
#pip install flask
(Get a coffee or some other beverage while waiting on werkzeug to download and be installed)
If it still doesn't install you can try to install werkzeug by it self.
#pip install werkzeug
and try again.
--------------
There are a number of tutorials on the web including the official one at:
http://flask.pocoo.org/docs/0.10/
I will try to simplify the information and just cover what I think would normally be used on the Galileo.
First a discussion of how Flask works
#hello.py Just a comment with the name
from flask import Flask Imports the Flask class which allows python to run an WSGI application.
app = Flask(__name__) Creates and names website variable (“app”) that will run an instance of this class.
@app.route('/') The @ is used to declare function definitions.
Route('/') redirects browser requests for the address / (the default, or home address) to the current directory and hello_world function.
def hello_world(): Creates a function in pyton
return 'Hello World!' When called it returns the string “Hello World!” to the web browser.
if __name__ == '__main__': Causes the server to run the script if it is executed directly from the Python interpreter.
app.run(host='0.0.0.0') The “run” function tells the system to run the program specified by the variable app.
host='0.0.0.0' tells the app to listen to all public IP addresses.
Creating web pages in python is annoying. It is much easier to use HTML and flask allows this. Flask expects to see html files (templates) in a templates sub-directory and static files (CSS, Javascript Text and image files) in a static subdirectory.
So you should have the home directory and two subdirectories
www
templates
static
You can now create a simple web page that will be able to communicate with a python program.
If you don't know how to do html, I would suggest you go through the great tutorial that was originally created by Maricopa Community Colleges and is now hosted at
http://www.math.unm.edu/writingHTML/tut/index.html
You can also download the tutorial for off-line browsing
http://www.math.unm.edu/writingHTML/tut/download.html
<soapbox>
Yes I know it is HTML 3.2 and doesn't include all the latest bells and whistles of HTML5, but it is simpler and easier to understand. This tutorial is focused on making a simple web page to display information and control the Galileo, and HTML3.2 is more than what you need. After you understand the basics you can add in all the additional complexity you want.
</soapbox>
First, after making the folders, create a file called GaliStatus1.py
#!/usr/bin/python
"""GaliStatus1.py
Get system information for display in web page
"""
import datetime
from flask import Flask, render_template
import os
app = Flask(__name__)
@app.route('/')
def Gali_Status():
AppName = "GaliStatus"
Now = datetime.datetime.now()
DateNow = Now.ctime()
# Get HostName
fout = open("/etc/hostname","r+") #open file
HostName = fout.read()
fout.close()
# Get IP Address
os.popen("ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'>ipaddr.txt")
fout = open("ipaddr.txt","r")
IPAddr = fout.read()
fout.close()
return render_template('GaliStatus1.html', AppName=AppName, DateNow=DateNow, HostName=HostName, IPAddr=IPAddr)
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=80, debug=True)
You should note some differences from the prior program, aside from the standard Python statements.
>from flask import Flask, render_template
This line tells Flask that it will be using a template (html file).
>return render_template('GaliStatus1.html', AppName=AppName, DateNow=DateNow, HostName=HostName, IPAddr=IPAddr)
This line tells flask to display the file GaliStatus1.html when a browser requests a page and pass on information stored in a set of variables (I used the same name to pass the information as the variable name to keep life easier)
This could also be done as an array: see http://mattrichardson.com/Raspberry-Pi-Flask/ for an example.
> app.run(host='0.0.0.0', port=80, debug=True)
This line tells Flask to, again, respond to requests, use port 80, the default port for html, and to turn on debugging. Debugging is useful doing testing. If you make a mistake in coding you will get a page showing your errors instead of what you expected. After all is running correctly you should set debug=False, as it is a security risk.
Now you can work on the web page.
You should copy your image files into the static folder (in my example I used VGU.jpg, you should use whatever small jpg you have lying around) and create a new web page in templates called GaliStatus1.html as follows:
----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!-- Galileo Status Web Page
(your name)
Shows data received from Galileo on web page -->
<html>
<head>
<table align="center" border="0" width="100%">
<tr>
<th width="25%"><img src="{{ url_for('static',filename="vgu.jpg", height="100")}}"></th>
<th width="50%"><font size="+1">
Vietnamese-German University<br>
Foundation Year Studies<br></font><br></th>
<th width="25%"><img src="{{ url_for('static',filename="vgu.jpg", height="100")}}"></th>
</tr>
</table>
<hr>
<title>{{ AppName }} app</title>
</head>
<body>
<h3 align="center">{{ AppName }}</h3>
<p align="center"> {{DateNow}}<br>
Host Name: {{ HostName }} <br>
IP Address: {{ IPAddr }} <br>
</p>
<address>
<p> Created by Your <u>Name</u> <br>
Lecturer, <a href="http://www.vgu.edu.vn">Vietnamese-German University</a><br>
<a href="mailto:your_email@yourprovider">your_email@yourprovider</a><br>
<tt>Last modified:12 Dec 2014</tt></p>
</address>
</body>
</html>
-------------------------------------------------------------------
If you did the tutorial on web page you should notice some differences from normal html.
<img src="{{ url_for('static',filename="vgu.jpg", height="100")}}">
Everything set off with double curly brackets “{{ }}” is pulled in.
In this case Flask is told to look in the static directory and retrieve the file vgu.jpg
<title>{{ AppName }} app</title>
In this case, Flask is being told to insert the information passed to it from the python program by the variable “AppName”.
The rest is standard HTML which, as you can see, is much easier to work with in a HTML file than trying to do the same thing in python.
Since the python program just uses standard python you can use one of the python libraries for Galileo (such as wiring_x86 I mentioned in the post I mentioned at the start of the message) to read and display information from sensors. You can also display pictures taken with a webcam or control pins from a web page.
More later
rgb