diff --git a/app.py b/app.py new file mode 100644 index 0000000..827a654 --- /dev/null +++ b/app.py @@ -0,0 +1,101 @@ +#!/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 = '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 = ''' + + + + + + Counter + + + +
{{ count }}
+ +
+ +
+ + +''' + +@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) \ No newline at end of file