Navigation

Flask Web Applications with SQLite3 Image Upload and View Example

Flask Web Applications with SQLite3

Flask is a lightweight web framework in Python. It provides powerful tools for building web applications, including routing, template rendering, and integration with databases like SQLite3. It allows for the use of SQLite3 to store data manually or data read from files. Alevel often has questions where you are expected to create a web app with sqlite3 to store data and display it in a table format.


1. Library Management Example

Python Backend (main.py)

import sqlite3 import flask app = flask.Flask(__name__) @app.route('/', methods=['GET', 'POST']) def home(): db = sqlite3.connect('LIBRARY.db') cur = db.execute("""SELECT FamilyName, GivenName, Title from Book, Member, Loan WHERE Book.BookID = Loan.BookID and Member.MemberNumber = Loan.MemberNumber and Loan.Returned = 'FALSE'""") data = cur.fetchall() return flask.render_template('index.html', data=data) app.run()

HTML Template (index.html):

<!DOCTYPE html> <html> <head> <title>Library System</title> </head> <body> <table> <tr> <th>Family Name</th> <th>Given Name</th> <th>Book Title</th> </tr> {% for item in data %} <tr> <td>{{ item[0] }}</td> <td>{{ item[1] }}</td> <td>{{ item[2] }}</td> </tr> {% endfor %} </table> </body> </html>

Explanation:

2. Sanitiser Management Example

Python Backend (main.py)

import sqlite3 import flask app = flask.Flask(__name__) @app.route('/') def home(): conn = sqlite3.connect('sanitisers.db') cur = conn.cursor() cur.execute('''SELECT product_name, active_ingredient, alcohol_based FROM sanitisers''') data = cur.fetchall() return flask.render_template('index.html', data=data) @app.route('/process_form/', methods=["POST"]) def process_home(): ingredient = flask.request.form['ingredient'] conn = sqlite3.connect('sanitisers.db') cur = conn.cursor() cur.execute('''SELECT product_name, alcohol_based FROM sanitisers WHERE active_ingredient = ?''', (ingredient,)) sanitisers = cur.fetchall() return flask.render_template('result.html', sanitisers=sanitisers) app.run()

HTML Template (index.html)

<!DOCTYPE html> <html> <head> <title>Sanitisers</title> </head> <body> <table> <tr> <th>Product Name</th> <th>Active Ingredient</th> <th>Alcohol Based</th> </tr> {% for item in data %} <tr> <td>{{ item[0] }}</td> <td>{{ item[1] }}</td> <td>{{ item[2] }}</td> </tr> {% endfor %} </table> <form action="/process_form/" method="POST"> <p> Active Ingredient: <input type="text" name="ingredient"> </p> <p> <input type="submit" value="Search"> </p> </form> </body> </html>

Explanation:


Image Upload and View Example

Python Backend (main.py)

import os from flask import Flask, render_template, request, send_from_directory from werkzeug.utils import secure_filename app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def home(): if request.method == 'POST' and request.files and 'image' in request.files: image = request.files['image'] filename = secure_filename(image.filename) path = os.path.join('uploads', filename) image.save(path) with open('all.txt', 'a') as file: file.write(filename + '\n') return render_template('index.html', status='ok') return render_template('index.html') @app.route('/view') def view(): with open('all.txt', 'r') as file: filenames = file.readlines() images = [filename.strip() for filename in filenames] return render_template('view.html', images=images) @app.route('/images/') def get_file(filename): return send_from_directory('uploads', filename) app.run()

Explanation:

  1. Import Statements:
  2. Flask Application Setup:
  3. Route: / (Home Page)
  4. Route: /view (Gallery Page)
  5. Route: /images/<filename> (Serve Images)
  6. Run the App:

HTML template (index.html)

<!DOCTYPE html> <html> <head> <title>Upload</title> </head> <body> <form method="post" enctype="multipart/form-data"> Image: <input name="image" type="file"><br/> </form> <p><a href="{{ url_for('view') }}">View</a></p> {% if status %} {{ status }} {% endif %} </body> </html>

Explanation:

  1. Purpose:
  2. Structure:

HTML template (view.html)

<!DOCTYPE html> <html> <head> <title>View</title> </head> <body> {% for image in images %} <img src="{{ url_for('get_file', filename=image) }}" alt="{{ image }}"> {% endfor %} <p><a href="{{ url_for('home') }}">Home</a></p> </body> </html>

Explanation:

  1. Purpose:
  2. Structure: