Flask Framework Assignments with Starter Code & Structure
1. Hello Flask App
Project Structure:
- [Link]
- templates/
- [Link]
- [Link]
[Link]:
from flask import Flask, render_template
app = Flask(__name__)
@[Link]("/")
def home():
return render_template("[Link]")
@[Link]("/about")
def about():
return render_template("[Link]")
if __name__ == "__main__":
[Link](debug=True)
2. Simple Login Form
Project Structure:
- [Link]
- templates/
- [Link]
- [Link]
[Link]:
from flask import Flask, render_template, request
app = Flask(__name__)
@[Link]("/", methods=["GET", "POST"])
def login():
if [Link] == "POST":
username = [Link]["username"]
password = [Link]["password"]
if username == "admin" and password == "123":
return render_template("[Link]", user=username)
else:
return "Invalid credentials"
return render_template("[Link]")
3. Calculator Web App
Project Structure:
- [Link]
- templates/
- [Link]
- [Link]
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'
@[Link]("/")
def visit():
session["count"] = [Link]("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 [Link] 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 [Link] and extend it using {% extends '[Link]' %}
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 [Link], [Link], and register them using Blueprints.
12. Upload and Display Profile Picture
Use [Link] 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 ([Link]) to filter results.
15. JWT Authentication API
Use PyJWT to create login and protected routes.
16. Flask with AJAX (Fetch API)
Project Structure:
- [Link]
- static/
- [Link]
- templates/
- [Link]
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.
@[Link]("/admin")
@admin_required
def admin_dashboard():
...
18. Unit Testing Flask App
Use unittest or pytest to test routes.
def test_home(client):
response = [Link]('/')
assert b"Welcome" in [Link]
19. Deployment on Render or Railway
Export [Link] and Procfile.
web: gunicorn app:app
20. Flask + SQLite + Charts
Use [Link] to visualize data from DB.
Pass data from Flask to JS via template context.