Build Real Apps
From Scratch

Learn. Practice. Ship.

Starter App Build Python Flask SQLite

🐍 Flask + SQLite CRUD Starter

A minimal Task Manager demonstrating Create, Read, Update, and Delete operations. Test the live interactive preview below, then copy the 55-line Python starter code to run it locally.

🖥️ Live Browser Preview

Simulated database using your browser's localStorage

⚡ Last Executed SQLite Query:
SELECT id, title FROM tasks ORDER BY id DESC;

🧠 Step-by-Step Beginner Guide

  1. Install Flask: Open your terminal and run pip install flask. Python already includes sqlite3, so no database setup is needed.
  2. Create Project File: Create a new folder, add a file named app.py, and paste the starter code below.
  3. Understand the 4 Routes:
    • / (GET) — Reads all tasks from SQLite and renders the page.
    • /add (POST) — Creates a new row using INSERT INTO.
    • /edit/<id> (POST) — Updates an existing row using UPDATE.
    • /delete/<id> (POST) — Deletes a row using DELETE FROM.
  4. Run Your App: Execute python app.py in your terminal and open http://127.0.0.1:5000 in your browser.

Security Tip: Notice the ? placeholders in the SQL queries (e.g., VALUES (?)). Always use placeholders instead of Python f-strings to prevent SQL injection attacks.

📄 Starter Source Code (app.py)

Complete single-file Flask + SQLite app ready to customize

import sqlite3
from flask import Flask, request, redirect, render_template_string

app = Flask(__name__)

def get_db():
    conn = sqlite3.connect("tasks.db")
    conn.row_factory = sqlite3.Row
    return conn

# Create table automatically on startup
with get_db() as db:
    db.execute("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, title TEXT NOT NULL)")

HTML = """
<!doctype html>
<body style="font-family:sans-serif; max-width:480px; margin:40px auto;">
  <h2>My Task App</h2>
  <form action="/add" method="POST" style="display:flex; gap:8px; margin-bottom:20px;">
    <input name="title" placeholder="New task..." required style="flex:1; padding:8px;">
    <button type="submit">Add</button>
  </form>
  {% for t in tasks %}
  <div style="display:flex; gap:6px; margin-bottom:8px;">
    <form action="/edit/{{ t['id'] }}" method="POST" style="display:flex; flex:1; gap:6px;">
      <input name="title" value="{{ t['title'] }}" required style="flex:1; padding:6px;">
      <button type="submit">Save</button>
    </form>
    <form action="/delete/{{ t['id'] }}" method="POST">
      <button type="submit" style="color:red;">Delete</button>
    </form>
  </div>
  {% endfor %}
</body>
"""

@app.route("/")
def index():
    tasks = get_db().execute("SELECT * FROM tasks ORDER BY id DESC").fetchall()
    return render_template_string(HTML, tasks=tasks)

@app.route("/add", methods=["POST"])
def add():
    with get_db() as db:
        db.execute("INSERT INTO tasks (title) VALUES (?)", (request.form["title"],))
    return redirect("/")

@app.route("/edit/<int:id>", methods=["POST"])
def edit(id):
    with get_db() as db:
        db.execute("UPDATE tasks SET title = ? WHERE id = ?", (request.form["title"], id))
    return redirect("/")

@app.route("/delete/<int:id>", methods=["POST"])
def delete(id):
    with get_db() as db:
        db.execute("DELETE FROM tasks WHERE id = ?", (id,))
    return redirect("/")

if __name__ == "__main__":
    app.run(debug=True)

🚀 Build Your Own App With This Template

Change the table columns in app.py to turn this starter into any app:

📒 Notes App
Add a content TEXT column and a <textarea> input.
💰 Expense Tracker
Add amount REAL and sum totals with SELECT SUM(amount).
🔖 Bookmark Manager
Store title and url to save your favorite links.
← Back to MAK UI Studio