DO NOT COPY OR SHARE - A3Tech / NCTI course material for enrolled students only
A3Tech logo

Introduction to Python Programming · A3Tech

Module 8

Today you will

Build an app with its own database for a real use, and show it to the class.

  1. Pick a real project and plan it.
  2. Build it step by step with project_db and Streamlit, testing every step.
  3. Show it to the class: what it does, how it works, and one problem you solved.
  4. Give and receive useful feedback.

Deep dive A: pick your project

Your final project is an app with its own database, for a real use. Pick something you or someone you know would actually use. Every idea below follows the same pattern: a form to add records, a table to show them, and a report.

ProjectWho it's forWhat each record savesReport question
Club registerA sports club coachname, phone, paidWho still owes fees?
Homework trackerA studentsubject, task, due, doneWhat's not done yet?
Tuck shop stockA school tuck shopitem, price, stockWhat's running low?
Salon bookingsA hair saloncustomer, day, time, serviceWho's booked on Saturday?
Training logAnyone who trainsdate, activity, minutesWhich sessions were over 60 minutes?
Church or community groupA group leadername, phone, teamWho is on the youth team?

Deep dive B: project_db - a database for any project

shop_db only knows about a shop. For your own project there's project_db.py, which works for anything. You don't create tables: the first time you save a record, it makes the table for you.
FunctionWhat it does
project_db.add("members", {...})Saves one record. The record is a dictionary: field name, then value (Module 5).
project_db.get_all("members")Every record, as a list of dictionaries ([] if nothing is saved yet)
project_db.query("SELECT ... WHERE ...")Your own SELECT, exactly like Module 6
project_db.delete_all("members")Throws the whole table away to start again. No undo!
try_project_db.py
import project_db

project_db.add("members", {"name": "Jada", "phone": "555-0101", "paid": "Yes"})
project_db.add("members", {"name": "Marcus", "phone": "555-0102", "paid": "No"})

members = project_db.get_all("members")
print(len(members), "members")
for m in members:
    print(f"{m['name']:<8} {m['phone']}   paid: {m['paid']}")

unpaid = project_db.query("SELECT name FROM members WHERE paid = 'No'")
print("Still to pay:", unpaid)
Output
2 members
Jada     555-0101   paid: Yes
Marcus   555-0102   paid: No
Still to pay: [{'name': 'Marcus'}]

Run it twice and you'll see 4 members: the database keeps everything, just like shop.db. The file is called project.db and sits next to your program, so you can open it in DB Browser.

Deep dive C: plan before you code

Ten minutes of planning saves an hour of fixing. Fill in this plan first. It's a Python file, so it runs too:

my_plan.py
# my_plan.py - fill this in BEFORE you start building
project_name = ""          # e.g. "Lions FC Register"
who_is_it_for = ""         # e.g. "the coach of an under-13 football team"
table_name = ""            # one word, e.g. "players"
fields = []                # what you save for each record, e.g. ["name", "age", "position"]
features = [               # 3 things your app will do
    "",                    # e.g. "add a player with a form"
    "",                    # e.g. "show all players in a table"
    "",                    # e.g. "report: how many players in each position"
]
report_question = ""       # the question your SELECT ... WHERE answers

print("Project:", project_name)
print("For:", who_is_it_for)
print("Saves", fields, "in the", table_name, "table")
for f in features:
    print(" -", f)
print("Report:", report_question)

Deep dive D: build in steps, test every step

Build it the same way as the Module 7 shop app: one step at a time, and run it after every step.

  1. Title and sidebar. Your project's name and what it's for.
  2. The form. Save a record with project_db.add(). Check it in DB Browser.
  3. The table. Show every record with get_all() and st.dataframe().
  4. The report. A st.metric or a chart, plus your SELECT ... WHERE question.
  5. Polish. Comments, clear names, one function of your own (Module 5B), helpful messages.
TestTry thisIt should...
EmptyOpen the app before saving anythingShow a friendly message, not crash
Bad inputSave with an empty nameShow a warning, not save
Lots of dataAdd 10 recordsStill show everything clearly
Close and reopenStop the app, run it againStill have all the records
A friendLet a classmate use it without helpMake sense to them

Deep dive E: the showcase

Each project gets a 3-minute demo:

  1. What it does and who it's for (30 seconds)
  2. Show it working: add a record, show the table, show the report (90 seconds)
  3. How it works: point at one part of your code (30 seconds)
  4. One problem you solved, and how (30 seconds)

Then the class gives feedback: two stars and a wish. Two things that were good, and one thing you'd add or change.

1. The project template

A working starting point: a club register. Put it in a folder with project_db.py, run streamlit run project_template.py, then change it to fit your project.

project_template.py
# project_template.py - Module 8 final project starter: a club register
# Run it with:  streamlit run project_template.py   (project_db.py in the same folder)
#
# Make it YOURS: change the title, the table name ("members") and the fields
# in the form and in the add() line to fit your own project.
import streamlit as st
import project_db

st.sidebar.title("My Club Register")                 # your project's name
st.sidebar.write("Keeps track of members and fees.")  # what it's for

st.title("My Club Register")

# ---- 1. a form to add a record ----
st.header("Add a member")
with st.form("add_member"):
    name = st.text_input("Name")
    phone = st.text_input("Phone")
    paid = st.checkbox("Fees paid")
    saved = st.form_submit_button("Save")

if saved:
    if name.strip() == "":
        st.warning("Please type a name.")
    else:
        if paid:
            status = "Yes"
        else:
            status = "No"
        project_db.add("members", {"name": name.strip(), "phone": phone, "paid": status})
        st.success(f"Saved {name.strip()}")

# ---- 2. a table of everything saved ----
members = project_db.get_all("members")
st.header("Members")
if members:
    st.dataframe(members)
else:
    st.info("No members yet - add one above.")

# ---- 3. a report ----
st.header("Report")
if members:
    paid_count = 0
    for m in members:
        if m["paid"] == "Yes":
            paid_count += 1
    st.metric("Members", len(members))
    st.metric("Fees paid", paid_count)
    st.bar_chart([{"fees": "Paid", "members": paid_count},
                  {"fees": "Not paid", "members": len(members) - paid_count}],
                 x="fees", y="members")

    unpaid = project_db.query("SELECT name, phone FROM members WHERE paid = 'No'")
    for m in unpaid:
        st.warning(f"{m['name']} still owes fees - call {m['phone']}")
Output
See the browser. Before anything is saved:
No members yet - add one above.   (blue)

Add Jada, 555-0101, tick Fees paid, Save:  Saved Jada
Add Marcus, 555-0102, leave it unticked, Save:  Saved Marcus
Members: a table with both
Report:  Members 2   Fees paid 1   a bar chart: Paid 1, Not paid 1
Marcus still owes fees - call 555-0102   (yellow)

Save with an empty name:  Please type a name.

Practice: project steps

Final project (40%)

An app with its own database, for a real use. Marked out of 40:

PartMarksWhat we look for
It works10Runs, and does the 3 features in your plan
Database10Saves records with project_db.add(), shows them with get_all(), and answers one question with SELECT ... WHERE
App8A form to add data, a table, and a metric or chart
Code quality6Clear names, comments, and at least one function of your own (Module 5B)
Demo6What it does, how it works, one problem solved

Submit your project folder zipped: your .py files, project_db.py and project.db, plus your demo video if you are online.

What's next?

You've built a real app with its own database. Here's where you can go from here:

  • Level 2: Python Foundations. Git, error handling with try/except, writing your own modules (like project_db.py), classes and objects, testing, APIs, and databases in depth: CREATE TABLE, updating records and joining tables.
  • Then pick a specialty: Data Analytics, Apps, SQL and databases, or Robotics.