sudo apt update
sudo apt install python3 idle3
mkdir myproject
cd myproject
python3 -m venv venv source venv/bin/activate
(venv) ~/myproject $ sudo apt-get install python3-pip (venv) ~/myproject $ pip install flaskFlask’s documentation
(venv) ~/myproject $ sudo nano myproject.pyCode:
from app import appNext:
(venv) ~/myproject $ mkdir app (venv) ~/myproject $ cd appNext file:
(venv)~/myproject/app $ sudo nano __init__.pyCode:
from flask import Flask app = Flask(__name__) from app import routesThird file:
(venv)~/myproject/app $ sudo nano routes.pyCode:
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
(venv)~/myproject $ echo "export FLASK_APP=myproject.py" >> ~/.profilereboot
(venv)~/myproject $ flask run
(venv)~/myproject $ pip install flask-wtf
(venv)~/myproject $ pip install flask-login
@app.route('/cakes')
def cakes():
return 'Yummy cakes!'
mkdir templates
cd templates
New file (with Python 3 IDLE) index.html
sudo nano index.html
<html>
<body>
<h1>My website</h1>
</body>
</html>
sudo nano routes.py
from flask import render_template, flash, redirect, url_for, send_file, request ...
@app.route('/')
def index():
return render_template('index.html')
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>
@app.route('/path/<name>')
def path(fname):
return send_file('static/path/'+name)
{% block content %} {% endblock }
in template use:
{% extends "base.html" %}
{% block content %}
{% endblock }
@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 %}
(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 ..
cd myproject source venv/bin/activateis correct!
(venv) ~/myproject $ flask db init (venv) ~/myproject $ flask db migrate -m "TABLENAME table" (venv) ~/myproject $ flask db upgrade
rm -rf migrations/versions/* flask db revisionget revision number
flask db upgrade
nano migrations/versions/[NEWREVISION]change to new revision
revision = "[NEWREVISION]"change "upgrade" and "downgradw"
def upgrade():
pass
def downgrade():
pass
(venv) ~/myproject $ pip install gunicorn
sudo nano .env
SECRET_KEY=52cb883e323b48d78a0a36e8e951ba4a MAIL_SERVER=localhost MAIL_PORT=25 DATABASE_URL=mysql+pymysql://myproject:@localhost:3306/myproject MS_TRANSLATOR_KEY=c2c9b294f57d49028fe03c2307f8cc4c
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
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
pip install RPi.GPIO
sudo supervisorctl stop myproject source venv/bin/activate flask rundebug
sudo supervisorctl start myproject
restic snapshots \
--repo /mnt/nas-backup/restic \
--password-file ~/.restic-pass
mkdir -p /mnt/nas-backup/restore-tests/myproject
restic restore latest \
--target /mnt/nas-backup/restore-tests/myproject \
--repo /mnt/nas-backup/restic \
--password-file ~/.restic-pass
ls -lah /mnt/nas-backup/restore-tests/myproject/mnt/nas-backup/staging/myproject
sudo rsync -a \
/mnt/nas-backup/restore-tests/myproject/mnt/nas-backup/staging/myproject/ \
/home/pi/myproject/
sudo chown -R pi:pi /home/pi/myproject
cd /home/pi/myproject
python3 -m venv venv
./venv/bin/pip install -r requirements.txt
restic find app.py \
--repo /mnt/nas-backup/restic \
--password-file ~/.restic-pass
restic restore latest \
--include "/mnt/nas-backup/staging/myproject/app.py" \
--target /mnt/nas-backup/restore-tests/single-file \
--repo /mnt/nas-backup/restic \
--password-file ~/.restic-pass
ls -lah /mnt/nas-backup/restore-tests/single-file
cp .../app.py /home/pi/myproject/
Restore immer zuerst in einen Testordner durchführen.