- Getting Started
- How to make a Website with Python
- HTML Templates
- Template Inheritance
- HTTP Methods
- Session
- Message Flashing
- Using SQLAlchemy Database
- Handling Static Files
- Blueprints and handling multiple python files
Flask is a micro web framework written in Python. It's used to build web applications quickly and with minimal code.
- Routing: Define URLs and what happens when someone visits them.
- Templating: Uses Jinja2 templating engine to render HTML dynamically.
- Built-in development server & debugger.
- RESTful request handling.
- Easily extendable with plugins (e.g., for databases, authentication, etc.).
pip install flaskfrom flask import Flask
app = Flask(__name__)
# Models and routes
if __name__ == "__main__":
app.run()Visit localhost:5000 in your browser.
You need to restart the server manually for every change to display it in webpage. If you want the server to detect the change and restart itself, you can use a parameter debug in run.
app.run(debug=True)In this section we'll learn how to create routes and redirection.
@app.route("/")
def home():
return "<h1>This is the Homepage</h1>"You're telling Flask:
"Hey Flask, when someone visits the root URL /, run the home() function and return whatever it outputs as the response."
@app.route("/user/<name>") # Dynamic routing
def user(name):
return f"Hello {name}"<variable_name> is used in dynamic routing in Flask
For redirection you need to import redirect and url_for from flask.
from flask import redirect, url_for@app.route("/test")
def test():
return redirect(url_for("home"))@app.route("/another-test")
def test():
return redirect(url_for("user", name = "Samm")) # Redirection to dynamic routesIn this section we'll learn how to render proper HTML template.
Create a directory in the root folder where your main.py is, name that directory templates. Inside templates create your HTML files.
Jinja (specifically Jinja2) is the template engine that Flask uses under the hood.
Jinja’s main job is to dynamically generate HTML by embedding Python-like expressions inside your HTML files.
To render HTML pages, we need render_template
from flask import render_templateHow to use render_template
@app.route("/")
def home():
return render_template("index.html")You're telling Flask:
"Hey Flask, when someone visits the root URL /, run the home() function, and send back the rendered HTML content from the index.html file inside the templates folder as the response."
We can also pass template variables
@app.route("/user/<name>")
def user(name):
return render_template("user.html", name = name, role = "admin", x = 10)We can pass any datatype as template variables.
How to use template variable in HTML
<h1>Hello {{name}}</h1>
<h4>You are {{role}}</h4>{% for i in range(x) %}
{% if i % 2 == 0 %}
<p>{{i}}</p>
{% endif %}
{% endfor %}{{ }} is used to show the value.
{% %} is used to write python code.
In this section we'll learn the concept of Template inheritance.
Template inheritance in Flask (via Jinja2) allows you to create a base HTML structure that other templates can extend. This promotes reusability and consistency across your web pages.
- Create a base template
<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
{% block content %}
<!-- Child templates inject content here -->
{% endblock %}
</main>
<footer>
<p>© 2025</p>
</footer>
</body>
</html>- Extend the base in a child template
<!-- templates/home.html -->
{% extends "base.html" %}
{% block title %}Home Page{% endblock %}
{% block content %}
<p>This is the home page content.</p>
{% endblock %}{% extends "base.html" %} tells Jinja to use the layout from base.html.
{% block %} tags in the base file are placeholders that the child templates fill in.
{% block block_name %} a block name is an identifier you define inside the {% block ... %} tag. It's like a placeholder section in your base template that child templates can override or fill in.
In this section we'll learn about HTTP methods in Flask.
HTTP methods determine the type of request the client sends to the server.
| Method | Use Case |
|---|---|
| GET | Retrieve data from the server |
| POST | Submit data to the server |
| PUT | Update existing data |
| DELETE | Delete data from the server |
| PATCH | Partially update data |
Import request from flask to access the data sent by the client.
from flask import request@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form['name']
return f"Hello, {name}!"
return '''
<form method="post">
<input type="text" name="name" placeholder="Enter your name">
<input type="submit">
</form>
'''GET shows the form.
POST handles the form submission and greets the user.
In this section we'll learn about session in Flask
In Flask, a session is used to store information across requests for a single user—like login status, user preferences, or shopping cart items.
It’s basically a temporary storage mechanism that persists between different pages/views for the same user.
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'your_secret_key_here' # needed to use sessionsfrom flask import Flask, session, redirect, url_for, request
app = Flask(__name__)
app.secret_key = 'your_secret_key_here'
@app.route('/')
def home():
if 'username' in session:
return f"Welcome back, {session['username']}!"
return "You are not logged in. <a href='/login'>Login</a>"
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('home'))
return '''
<form method="post">
<input type="text" name="username" placeholder="Enter name">
<input type="submit">
</form>
'''
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('home'))| Operation | Description |
|---|---|
session['key'] = value |
Set a session value |
session.get('key') |
Get a session value |
session.pop('key', None) |
Remove a specific key from the session |
session.clear() |
Clear the entire session |
session stores data in dictionary (key-value pair).
By default, Flask sessions last only until the browser is closed (they are temporary).
If you want a session to persist even after closing the browser, you can make it permanent.
You just need to set:
session.permanent = TrueYou can also set the duration for how long the session should last. By default it is 31 days
from flask import Flask, session, timedelta
app = Flask(__name__)
app.secret_key = 'your_secret_key_here'
# Set session lifetime (optional)
app.permanent_session_lifetime = timedelta(days=7)
@app.route('/login')
def login():
session.permanent = True # Mark this session as permanent
session['user'] = 'Alice'
return 'Logged in with a permanent session!'
@app.route('/get')
def get():
user = session.get('user')
return f'Hello, {user}' if user else 'No user logged in.'In this section we'll learn about Message Flashing in Flask.
Flashing is a way to send a message to the next request, usually used to display status messages like:
- ✅ “You have successfully logged in.”
- ❌ “Invalid password.”
⚠️ “Please fill all required fields.”
from flask import Flask, flash, redirect, render_template, request, session, url_for
app = Flask(__name__)
app.secret_key = 'your_secret_key_here' # required for flashing
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
user = request.form['username']
if user == 'admin':
flash('Login successful!', 'success')
return redirect(url_for('home'))
else:
flash('Invalid username!', 'error')
return redirect(url_for('login'))
return render_template('login.html')
@app.route('/home')
def home():
return render_template('home.html')flash(message) - Stores a message for the next view
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul>
{% for category, message in messages %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}get_flashed_messages() - Retrieves and clears flashed messages
| Category | Purpose | Example Usage |
|---|---|---|
success |
For positive feedback | flash("Logged in successfully!", "success") |
error |
For error messages | flash("Invalid password!", "error") |
warning |
For caution or alerts | flash("Your session is about to expire.", "warning") |
info |
For general information | flash("New feature launched!", "info") |
flash("Something went wrong!", "error")In this section we'll learn how to integrate SQLAchemy with Flask.
To integrate SQLAchemy, we need to install Flask-SQlAlchemy
pip install flask-sqlalchemyfrom flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db' # or PostgreSQL/MySQL URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return f'<User {self.username}>'column_name = db.Column(column_datatype, ...optional_arguements)
__init__: You can define an__init__method, but you don't need to if you're just assigning attributes that match the model fields. SQLAlchemy auto-generates a default constructor unless you want custom behavior.
__repr__: Defines how the object is represented as a string (for debugging/logs).
if __name__ == "__main__":
with app.app_context():
db.create_all() # Creates tables based on the models
app.run()# CREATE
new_user = User(username='john', email='[email protected]')
db.session.add(new_user)
db.session.commit()
# READ
user = User.query.filter_by(username='john').first()
# UPDATE
user.email = '[email protected]'
db.session.commit()
# DELETE
db.session.delete(user)
db.session.commit()In this section we'll learn how to handle static files in Flask.
We create a separate folder for static files and name it "static":
your_project/
│
├── main.py
├── static/
│ ├── styles/
│ │ └── style.css
│ ├── scripts/
│ │ └── script.js
│ └── images/
│ └── logo.png
│
├── templates/
│ └── index.htmlNow how to access the static files
<!-- CSS files -->
<link rel="stylesheet" href="{{ url_for('static', filename='styles/style.css') }}"><!-- JavaScript files -->
<script src="{{ url_for('static', filename='scripts/script.js') }}"></script> <!-- Images -->
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">In this section we'll learn about Blueprints in Flask.
Flask Blueprints are a powerful way to organize your application into modular components, especially helpful as your app grows. Think of blueprints like “mini-apps” that can be plugged into the main Flask app.
- Cleaner, modular code
- Separate concerns (routes, templates, static files)
- Easy to reuse and scale
your_project/
│
├── main.py
├── static/
├── templates/
│
├── admin/
│ ├── admin.py
│ ├── static/
│ └── templates/from flask import Blueprint, render_template
admin = Blueprint("admin", __name__, static_folder="static", template_folder="templates")
@admin.route('/')
def admin_page():
return render_template('admin_page.html')Blueprint("blueprint_name",
__name__, static_folder="path_to_static_folder", template_folder="patgh_to_template_folder")
from flask import Flask
from admin.admin import admin
app = Flask(__name__)
app.register_blueprint(admin, url_prefix="/admin")
if __name__ == '__main__':
app.run(debug=True)register_blueprint("blueprint_name", url_prefix="/prefix_route")
url_prefix is optional
For better clarity checkout the code given in the 09 - Blueprints and Handling multiple Python files