Flask is a package that can be downloaded onto the Pi along with Flask-Admin. Flask ties in your .py scripts with .html and .css scripts for you to interpret the database generated by Peewee and add it to a web server hosted locally by the Pi. Flask-Admin is an extension of Flask that makes it easier to format and add functionality to your web page using built in functions (that are demonstrated in code below) that will automatically generate blocks of html code for you. It is particularly helpful with databases because it lets you group together all of the usual Create, Read, Update, Delete (CRUD) view logic into a single, self-contained class for each of your models.
Further information on Flask and Flask-Admin can be found by clicking on the respective links.
upv_app.py
from flask import Flask , render_template from flask_admin import Admin from flask_admin.contrib.peewee import ModelView import model app = Flask(__name__) admin = Admin(app, name = 'UPV Wind Data',template_mode='bootstrap3',url='/') app.config['SECRET_KEY'] = 'DIT123' # Have to set secret key or else you won't be able to save things later on. app.config['MODEL'] = model.SensorAccess() if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
The code above in ‘upv_app.py’ is the working script that runs your server, it has to be running to access the server. Within the script you can edit the site name and the site template, Bootstrap3 was used as our template style as it has the icest look to it in our opinion. It also points flask in the direction of the SensorAccess object from ‘model.py‘ so that we can include our database on the web page that is generated.
index.html
{% extends 'admin/master.html'%} {% block body %} <h1> UPV Wind/Power Station and Wireless Sensor Network</h1> Using a SQLite database to configure and store the data from wireless sensors {% endblock %} <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
Above is the ‘index.html‘ script that uses a mixture of jinja(a language that flask-admin uses) and html language to enable a simple way of formatting the web page. Our web page is very simple so there is only a heading 1 and paragraph included.
The output of the ‘upv_app.py‘ and ‘index.html‘ comes out with the basic web page below;
Flask-Admin helped generate interactive tabs such as the Turbine_Weather tab which is a page that displays a table containing our database data as can be seen below;