102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Wed May 20 12:52:00 2026
|
|
|
|
@author: angoosh
|
|
"""
|
|
from flask import Flask, render_template_string, redirect, url_for
|
|
import sqlite3
|
|
|
|
app = Flask(__name__)
|
|
DB_FILE = '/db/counter.db'
|
|
|
|
def init_db():
|
|
"""Initialize the database and set the starting number to 0 if it doesn't exist."""
|
|
with sqlite3.connect(DB_FILE) as conn:
|
|
c = conn.cursor()
|
|
# Create a table to hold our counter
|
|
c.execute('CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY, value INTEGER)')
|
|
|
|
# Check if the counter row already exists
|
|
c.execute('SELECT value FROM counter WHERE id = 1')
|
|
if c.fetchone() is None:
|
|
# If not, insert the initial value of 0
|
|
c.execute('INSERT INTO counter (id, value) VALUES (1, 0)')
|
|
conn.commit()
|
|
|
|
# HTML & CSS template
|
|
# The CSS handles making the button blue and centering the layout
|
|
HTML_TEMPLATE = '''
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Counter</title>
|
|
<style>
|
|
body {
|
|
font-family: sans-serif;
|
|
text-align: center;
|
|
margin-top: 100px;
|
|
}
|
|
.counter-display {
|
|
font-size: 72px;
|
|
font-weight: bold;
|
|
margin-bottom: 20px;
|
|
}
|
|
.blue-button {
|
|
background-color: #007BFF;
|
|
color: white;
|
|
border: none;
|
|
padding: 15px 32px;
|
|
text-align: center;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
|
transition: background-color 0.3s ease;
|
|
}
|
|
.blue-button:hover {
|
|
background-color: #0056b3;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="counter-display">{{ count }}</div>
|
|
|
|
<form action="/increment" method="POST">
|
|
<button type="submit" class="blue-button">Add One</button>
|
|
</form>
|
|
</body>
|
|
</html>
|
|
'''
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Fetch the current count from the database and display it."""
|
|
with sqlite3.connect(DB_FILE) as conn:
|
|
c = conn.cursor()
|
|
c.execute('SELECT value FROM counter WHERE id = 1')
|
|
current_count = c.fetchone()[0]
|
|
|
|
return render_template_string(HTML_TEMPLATE, count=current_count)
|
|
|
|
@app.route('/increment', methods=['POST'])
|
|
def increment():
|
|
"""Add 1 to the database value and refresh the page."""
|
|
with sqlite3.connect(DB_FILE) as conn:
|
|
c = conn.cursor()
|
|
c.execute('UPDATE counter SET value = value + 1 WHERE id = 1')
|
|
conn.commit()
|
|
|
|
# Redirect back to the main page to see the updated number
|
|
return redirect(url_for('index'))
|
|
|
|
if __name__ == '__main__':
|
|
# Initialize the database before starting the server
|
|
init_db()
|
|
# Run the app in debug mode
|
|
app.run(debug=True)
|