100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Python Flask Cheat Sheet

Python Flask Cheat Sheet

Covers Flask app setup, routing, request and response handling, Jinja2 templates, and organizing routes with blueprints.

1 PageIntermediateMar 30, 2026

Minimal Flask App

The smallest possible Flask application.

python
from flask import Flaskapp = Flask(__name__)@app.route("/")def index():    return "Hello, Flask!"if __name__ == "__main__":    app.run(debug=True)   # Dev server with auto-reload# Run with: flask --app app run --debug# or: python app.py

Routes & Request Handling

Read query strings, JSON bodies, and path parameters.

python
from flask import request, jsonify@app.route("/users/<int:user_id>")def get_user(user_id):    return jsonify({"id": user_id})@app.route("/search")def search():    query = request.args.get("q", "")   # Query string param    return jsonify({"query": query})@app.route("/users", methods=["POST"])def create_user():    data = request.get_json()           # Parse JSON body    name = data.get("name")    return jsonify({"created": name}), 201

Templates (Jinja2)

Render server-side HTML with Jinja2.

python
from flask import render_template@app.route("/profile/<username>")def profile(username):    return render_template("profile.html", username=username)# templates/profile.html# <h1>Hello, {{ username }}!</h1># {% if username == "admin" %}<p>Welcome back</p>{% endif %}# {% for item in items %}<li>{{ item }}</li>{% endfor %}

Blueprints

Organize routes into reusable modules.

python
# blog/routes.pyfrom flask import Blueprintbp = Blueprint("blog", __name__, url_prefix="/blog")@bp.route("/")def index():    return "Blog home"# app.pyfrom blog.routes import bpapp.register_blueprint(bp)

Flask Essentials

Frequently used objects and helpers.

  • app.config- Dict-like object for app configuration (e.g. SECRET_KEY, DEBUG)
  • request.form- Access form-encoded POST data
  • request.args- Access URL query string parameters
  • abort(404)- Short-circuit a view with an HTTP error response
  • url_for('view_name')- Build a URL by endpoint name instead of hardcoding paths
  • flash('message')- Queue a one-time message to show on the next rendered page
  • g- Application context object for storing per-request data
Pro Tip

Never run app.run(debug=True) in production — the interactive debugger can execute arbitrary code from anyone who reaches an error page. Serve with a WSGI server like gunicorn behind a reverse proxy instead.

Was this cheat sheet helpful?

Explore Topics

#PythonFlask#PythonFlaskCheatSheet#Programming#Intermediate#MinimalFlaskApp#RoutesRequestHandling#TemplatesJinja2#Blueprints#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet