Flask Cheatsheet
⚙️ Flask Setup & Basics # Install Flask
pip install flask
# hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
๐ Routing from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Home Page"
@app.route("/about")
def about():
return "About Page"
# Dynamic routes
@app.route("/user/<username>")
def user(username):
return f"Hello, {username}!"
๐จ Templates (Jinja2) from flask import render_template
@app.route("/hello/<name>")
def hello(name):
return render_template("hello.html", name=name)
# templates/hello.html
<h1>Hello, {{ name }}!</h1>
{% if name == "Maxon" %}
<p>Welcome back, admin!</p>
{% endif %}
๐ผ️ Static Files # Directory structure
/project
/static
style.css…