From bd3ac69ac9438728e185f9f4be4acd77b583a569 Mon Sep 17 00:00:00 2001 From: Angoosh Leviocki Date: Tue, 26 May 2026 11:29:28 +0200 Subject: [PATCH] added visit counter --- app.py | 110 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index d5078cb..2332b25 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,4 @@ -#!/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 +from flask import Flask, render_template_string, jsonify import sqlite3 app = Flask(__name__) @@ -15,18 +8,22 @@ 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 + # 1. Setup Button Counter Table + c.execute('CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY, value INTEGER)') 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)') + + # 2. Setup Visit Counter Table + c.execute('CREATE TABLE IF NOT EXISTS visits (id INTEGER PRIMARY KEY, count INTEGER)') + c.execute('SELECT count FROM visits WHERE id = 1') + if c.fetchone() is None: + c.execute('INSERT INTO visits (id, count) VALUES (1, 0)') + conn.commit() -# HTML & CSS template -# The CSS handles making the button blue and centering the layout +# We added an ID to the counter div and a JavaScript block at the bottom HTML_TEMPLATE = ''' @@ -35,10 +32,22 @@ HTML_TEMPLATE = ''' Counter -
{{ count }}
+
+
{{ count }}
+ +
-
- -
+ + + ''' @app.route('/') def index(): - """Fetch the current count from the database and display it.""" + """Update visit count, fetch both counts, and load the page.""" with sqlite3.connect(DB_FILE) as conn: c = conn.cursor() + + # Increment the visit counter every time this route is hit + c.execute('UPDATE visits SET count = count + 1 WHERE id = 1') + conn.commit() + + # Fetch the newly updated visit count + c.execute('SELECT count FROM visits WHERE id = 1') + visit_count = c.fetchone()[0] + + # Fetch the current button count c.execute('SELECT value FROM counter WHERE id = 1') - current_count = c.fetchone()[0] + button_count = c.fetchone()[0] - return render_template_string(HTML_TEMPLATE, count=current_count) + # Pass both variables into the HTML template + return render_template_string(HTML_TEMPLATE, count=button_count, visits=visit_count) @app.route('/increment', methods=['POST']) def increment(): - """Add 1 to the database value and refresh the page.""" + """Add 1 to the database value and return the new number as JSON.""" with sqlite3.connect(DB_FILE) as conn: c = conn.cursor() + # Update the database c.execute('UPDATE counter SET value = value + 1 WHERE id = 1') + + # Grab the new value so we can send it back to the frontend + c.execute('SELECT value FROM counter WHERE id = 1') + new_count = c.fetchone()[0] conn.commit() - # Redirect back to the main page to see the updated number - return redirect(url_for('index')) + # Return JSON instead of redirecting the whole page + return jsonify({'new_count': new_count}) if __name__ == '__main__': - # Initialize the database before starting the server init_db() - # Run the app in debug mode - app.run(host='0.0.0.0', port=5000, debug=True) + # Keeping the host='0.0.0.0' so your outside connections still work! + app.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file