from flask import Blueprint, render_template, request, g, url_for, redirect from datetime import datetime, date import sqlite3 keep = Blueprint('keep', __name__,template_folder='templates') def get_db(): db = getattr(g, "_database", None) if db is None: db = g._database = sqlite3.connect("app/db/keepalive.db") return db @keep.route('/keepalive/') def dispkeepalive(): conn = get_db() c = conn.cursor() items_from_db = c.execute("""SELECT i.id, i.location, i.time, i.date FROM keepalive2 AS i ORDER BY i.id DESC """) #db_teardown(keep) items = [] for row in items_from_db: item = { "id": row[0], "location": row[1], "time": row[2], "date": row[3] } items.append(item) return render_template("keepaliveshowpage.html", items=items) @keep.route('/loc//', methods=["POST"]) def getalive(location): conn = get_db() c = conn.cursor() c.execute("""CREATE TABLE IF NOT EXISTS keepalive2 ( id INTEGER PRIMARY KEY AUTOINCREMENT, location TEXT, date TEXT, time TEXT)""") c.execute("""INSERT INTO keepalive2 (location, date, time) VALUES (?, ?, ?)""", (location, date.today().strftime("%m/%d/%y"), datetime.now().strftime("%H:%M:%S"))) conn.commit() conn.close() # Redirect to the same route with GET method return f"Keepalive recorded for {location}" @keep.route('/loc//', methods=["GET"]) def show_location(location): return f"

Received keepalive from: {location}

" @keep.route('/delete//', methods=["POST"]) def delete_keepalive(item_id): conn = get_db() c = conn.cursor() c.execute("DELETE FROM keepalive2 WHERE id = ?", (item_id,)) conn.commit() return redirect(url_for('keep.dispkeepalive'))