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}'