Flask Reviewer
Flask Reviewer
http://127.0.0.1:5000/
Port is always 5000
🔹 Environment Setup
Activate a virtual environment: Flask – Core class to create the Flask web
application.
For macOS/Linux: render_template – Loads and displays
source venv/bin/activate HTML files from the templates folder.
For Windows:
venv\Scripts\activate Step 2: Creating a Flask App
app = Flask(__name__)
🔹 How Flask Routes Work
📂 Project Folder
📄 app.py – Main Python file
📂 templates/ – Folder for HTML files 🔹 Views in Flask
📄 index.html
📄 about.html Views are functions that handle
requests and generate responses.
Flask looks for HTML files inside the
templates folder by default.
static/
├── css/
│ ├── styles.css 🔹 Passing Data to Templates
├── img/
│ ├── logo.png Pass variables using render_template():
├── js/
│ ├── script.js @app.route('/')
def home():
Linking Static Files in HTML return render_template('index.html',
title="Welcome")
<link rel="stylesheet" href="{{ url_for('static',
filename='css/styles.css') }}"> Inside index.html:
<img src="{{ url_for('static',
filename='img/logo.png') }}" alt="Logo"> <h1>{{ title }}</h1>
{% extends "base.html" %}
🔹 Jinja2 Template Syntax
{% block title %}Home Page{% endblock %}
{% block content %}
<h1>Welcome to My Website</h1>
{% endblock %}