Flask Framework Assignments with Starter Code & Structure
1. Hello Flask App
Project Structure:
- app.py
- templates/
- index.html
- about.html
app.py:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
if __name__ == "__main__":
app.run(debug=True)
2. Simple Login Form
Project Structure:
- app.py
- templates/
- login.html
- success.html
app.py:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
if username == "admin" and password == "123":
return render_template("success.html", user=username)
else:
return "Invalid credentials"
return render_template("login.html")
3. Calculator Web App
Project Structure:
- app.py
- templates/
- calc.html
- result.html
Use form to take two numbers and operation, then return result.
4. Session Tracker
Use Flask session to store and increment a counter each time the user visits.
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'
@app.route("/")
def visit():
session["count"] = session.get("count", 0) + 1
return f"You visited {session['count']} times"
5. Form Validation (Flask-WTF)
Use Flask-WTF with form validation.
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import InputRequired, Email
class RegisterForm(FlaskForm):
email = StringField("Email", validators=[InputRequired(), Email()])
password = PasswordField("Password", validators=[InputRequired()])
6. Todo App (No DB)
Store todos in a list or session and allow add/delete in session.
7. HTML Template Inheritance
Use base.html and extend it using {% extends 'base.html' %}
8. Todo App with SQLite
Use Flask-SQLAlchemy with a Task model. Include routes for CRUD operations.
9. User Authentication System
Use User model with login, logout and session management.
10. Blog Platform
Users can post blogs. One-to-many relationship with SQLAlchemy.
11. Blueprint Modularization
Split app into auth.py, main.py, and register them using Blueprints.
12. Upload and Display Profile Picture
Use request.files and save images to static/uploads.
13. RESTful API with Flask
Create REST API endpoints using Flask or Flask-RESTful.
14. Search Functionality
Use query string (request.args.get) to filter results.
15. JWT Authentication API
Use PyJWT to create login and protected routes.
16. Flask with AJAX (Fetch API)
Project Structure:
- app.py
- static/
- script.js
- templates/
- index.html
Use JavaScript fetch() to send data to Flask backend without page reload.
17. Role-Based Access Control (RBAC)
Add roles to User model and restrict routes using decorators.
@app.route("/admin")
@admin_required
def admin_dashboard():
...
18. Unit Testing Flask App
Use unittest or pytest to test routes.
def test_home(client):
response = client.get('/')
assert b"Welcome" in response.data
19. Deployment on Render or Railway
Export requirements.txt and Procfile.
web: gunicorn app:app
20. Flask + SQLite + Charts
Use Chart.js to visualize data from DB.
Pass data from Flask to JS via template context.