Web Development with Python
Flask
Integrative Programming Module
Lessons 1–6 + Cheatsheets
Lesson 1: Introduction to Flask
• Definition: Flask is a lightweight Python web
framework.
• Install: pip install flask
• Hello World Example:
• from flask import Flask
• app = Flask(__name__)
• @app.route('/')
• def home(): return 'Hello, World!'
• app.run(debug=True)
Lesson 2: Templates & Jinja
• Use Jinja2 templates to embed Python in
HTML.
• Project structure: templates/ folder.
• base.html + index.html
• render_template('index.html', var=value)
• {{ var }} → prints variable
• {% block content %}{% endblock %} → layout
inheritance
Lesson 3: Forms & User Input
• HTML forms send data via GET or POST.
• Flask handles input with request.form.
• Example:
• @app.route('/', methods=['GET','POST'])
• if request.method == 'POST':
• name = request.form['name']
• return f'Hello {name}'
Lesson 4: Database Integration
• SQLite built-in with Python.
• Steps: connect, cursor, execute SQL, commit,
close.
• Example:
• conn = sqlite3.connect('db.db')
• c.execute('CREATE TABLE students ...')
• INSERT / SELECT / UPDATE / DELETE
Lesson 5: CRUD System
• CRUD = Create, Read, Update, Delete.
• Routes:
• • Create → POST form insert
• • Read → SELECT and display list
• • Update → form + SQL UPDATE
• • Delete → route with SQL DELETE
Lesson 6: Final Project
• Build a small CRUD system:
• Examples: Student Records, Inventory, Contact
Book.
• Requirements:
• • Flask + SQLite
• • CRUD complete
• • Documentation (ERD, Use Case, screenshots)
• • Group demo & defense
Flask Cheatsheet
• Flask Route Skeleton:
• @app.route('/path', methods=['GET','POST'])
• def func(): return render_template('file.html')
• Jinja:
• {{ var }}, {% if ... %}, {% for ... %}
• {% extends 'base.html' %}, {% block content %}
• SQL CRUD:

Flask_Module_Lessons_Python_Webdev1-6.pptx

  • 1.
    Web Development withPython Flask Integrative Programming Module Lessons 1–6 + Cheatsheets
  • 2.
    Lesson 1: Introductionto Flask • Definition: Flask is a lightweight Python web framework. • Install: pip install flask • Hello World Example: • from flask import Flask • app = Flask(__name__) • @app.route('/') • def home(): return 'Hello, World!' • app.run(debug=True)
  • 3.
    Lesson 2: Templates& Jinja • Use Jinja2 templates to embed Python in HTML. • Project structure: templates/ folder. • base.html + index.html • render_template('index.html', var=value) • {{ var }} → prints variable • {% block content %}{% endblock %} → layout inheritance
  • 4.
    Lesson 3: Forms& User Input • HTML forms send data via GET or POST. • Flask handles input with request.form. • Example: • @app.route('/', methods=['GET','POST']) • if request.method == 'POST': • name = request.form['name'] • return f'Hello {name}'
  • 5.
    Lesson 4: DatabaseIntegration • SQLite built-in with Python. • Steps: connect, cursor, execute SQL, commit, close. • Example: • conn = sqlite3.connect('db.db') • c.execute('CREATE TABLE students ...') • INSERT / SELECT / UPDATE / DELETE
  • 6.
    Lesson 5: CRUDSystem • CRUD = Create, Read, Update, Delete. • Routes: • • Create → POST form insert • • Read → SELECT and display list • • Update → form + SQL UPDATE • • Delete → route with SQL DELETE
  • 7.
    Lesson 6: FinalProject • Build a small CRUD system: • Examples: Student Records, Inventory, Contact Book. • Requirements: • • Flask + SQLite • • CRUD complete • • Documentation (ERD, Use Case, screenshots) • • Group demo & defense
  • 8.
    Flask Cheatsheet • FlaskRoute Skeleton: • @app.route('/path', methods=['GET','POST']) • def func(): return render_template('file.html') • Jinja: • {{ var }}, {% if ... %}, {% for ... %} • {% extends 'base.html' %}, {% block content %} • SQL CRUD: