-
Source: Build a Python Web Server with Flask and
Serving Raspberry Pi with Flask
(New Source: pimylifeup.com)
-
Install Debian
Debian
-
Install Python
sudo apt update
sudo apt install python3 idle3
-
Create WebServer Folder
mkdir myproject
cd myproject
-
Virtual environment
python3 -m venv venv
source venv/bin/activate
-
Install Flask
(venv) ~/myproject $ sudo apt-get install python3-pip
(venv) ~/myproject $ pip install flask
Flask’s documentation
Flask Tutorial
-
App
(venv) ~/myproject $ sudo nano myproject.py
Code:
from app import app
Next:
(venv) ~/myproject $ mkdir app
(venv) ~/myproject $ cd app
Next file:
(venv)~/myproject/app $ sudo nano __init__.py
Code:
from flask import Flask
app = Flask(__name__)
from app import routes
Third file:
(venv)~/myproject/app $ sudo nano routes.py
Code:
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
-
Start Web
(venv)~/myproject $ echo "export FLASK_APP=myproject.py" >> ~/.profile
reboot
(venv)~/myproject $ flask run
-
Browse Web
http://127.0.0.1:5000/
-
Web Forms
(venv)~/myproject $ pip install flask-wtf
-
Web Login
(venv)~/myproject $ pip install flask-login
-
Other Page/ Other Route
@app.route('/cakes')
def cakes():
return 'Yummy cakes!'
-
HTML
mkdir templates
cd templates
New file (with Python 3 IDLE) index.html
sudo nano index.html
<html>
<body>
<h1>My website</h1>
</body>
</html>
-
Modify Imports (import flask)
sudo nano routes.py
from flask import render_template, flash, redirect, url_for, send_file, request ...
Modify index()
@app.route('/')
def index():
return render_template('index.html')
-
CSS
cd ..
mkdir static
cd static
New file style.css
body {
background: red;
color: yellow;
}
include the CSS rules
<head>
<link rel="stylesheet" href='/static/style.css' />
</head>
-
Route to File (general)
@app.route('/path/<name>')
def path(fname):
return send_file('static/path/'+name)
-
Base Page
{% block content %} {% endblock }
in template use:
{% extends "base.html" %}
{% block content %}
{% endblock }
-
Dynamic Content
handover the content (title)
@app.route('/jc/index.html')
def home():
return render_template('/jc/index.html', title='home')
evaluate the content of title
{% if title=='home' %}
<a class="active" href="#home">Home</a>
{% else %}
<a href="home.html">Home</a>
{% endif %}
-
Database
(venv) ~/myproject $ pip install flask-sqlalchemy
(venv) ~/myproject $ pip install flask-migrate
(venv) ~/myproject $ sudo nano config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
# ...
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
(venv) ~/myproject $ cd app
(venv) ~/myproject/app $ sudo nano __init__.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models
(venv) ~/myproject/app $ sudo nano models.py
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
def __repr__(self):
return ''.format(self.username)
cd ..
-
Migration
(venv) ~/myproject $ flask db init
(venv) ~/myproject $ flask db migrate -m "TABLENAME table"
(venv) ~/myproject $ flask db upgrade
-
Flask-Login
(venv) ~/myproject $ pip install flask-login
(venv) ~/myproject $ pip install email-validator
-
Gunicorn
(venv) ~/myproject $ pip install gunicorn
-
.env
sudo nano .env
SECRET_KEY=52cb883e323b48d78a0a36e8e951ba4a
MAIL_SERVER=localhost
MAIL_PORT=25
DATABASE_URL=mysql+pymysql://myproject:@localhost:3306/myproject
MS_TRANSLATOR_KEY=c2c9b294f57d49028fe03c2307f8cc4c
-
Python WSGI HTTP Server
sudo apt-get -y install supervisor
sudo nano /etc/supervisor/conf.d/myproject.conf
[program:myproject]
command=/home/pi/myproject/venv/bin/gunicorn -b 0.0.0.0:8000 -w 4 myproject:app
directory=/home/pi/myproject
user=pi
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
sudo supervisorctl reload
-
Browse Web
http://127.0.0.1:8000/
-
Nginx
sudo apt-get -y install nginx
sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/myproject
server {
# listen on port 80 (http)
listen 80;
server_name _;
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
# proxy_pass http://localhost:8000;
# proxy_redirect off;
}
}
sudo service nginx reload
-
RPi.GPIO
pip install RPi.GPIO
-
Debug
sudo supervisorctl stop myproject
source venv/bin/activate
flask run
debug
sudo supervisorctl start myproject
-
FRITZ!Box
Login >> Internet >> Freigaben >> "Gerät für Freigaben hinzufügen"
Gerät >> IP-Adresse manuell eingeben
IPv4-Adresse: 192.168.2.5
Click
"Neue Freigabe" >> Anwendung: HTTP-Server >> "OK"
"Neue Freigabe" >> Anwendung: HTTPS-Server >> "OK"
"OK"
-
IPFire
Login >> Firewall >> Firewall Rules >> "Newrule"
Source >> Standard networks: >> "RED"
NAT >> Destination NAT (Port forwarding) >> "RED"
Destination >> Hosts >> "Web or Proxy"
Protocol >> TCP
Update