Python26 min read

Python Flask Basics

Build your first web app using Flask: routing, templates, forms, and JSON APIs. Great for learning backend fundamentals and creating small production apps.

David Miller
September 16, 2025
3.5k82

Flask is a lightweight Python web framework.

        Why people use Flask:
        - easy to learn
        - flexible
        - great for APIs and small web apps
        - lets you understand how backend works (routes, requests, responses)
        
        ## Install Flask
        
        ```bash
        pip install flask
        ```
        
        ## Your first Flask app
        
        Create `app.py`:
        
        ```python
        from flask import Flask
        
        app = Flask(__name__)
        
        @app.route("/")
        def home():
            return "Hello from Flask!"
        
        if __name__ == "__main__":
            app.run(debug=True)
        ```
        
        Run:
        ```bash
        python app.py
        ```
        
        Open:
        `http://127.0.0.1:5000/`
        
        ## Routing (multiple pages)
        
        ```python
        @app.route("/about")
        def about():
            return "About Page"
        ```
        
        ## Dynamic routes (very common)
        
        ```python
        @app.route("/user/<name>")
        def user(name):
            return f"Hello {name}!"
        
        @app.route("/post/<int:post_id>")
        def post(post_id):
            return f"Post ID: {post_id}"
        ```
        
        ## Templates (HTML pages)
        
        Install Jinja is included with Flask.
        
        ```python
        from flask import render_template
        
        @app.route("/")
        def home():
            users = ["Tom", "Sarah", "Mike"]
            return render_template("index.html", users=users)
        ```
        
        Create file: `templates/index.html`
        
        ```html
        <ul>
          {% for u in users %}
            <li>{{ u }}</li>
          {% endfor %}
        </ul>
        ```
        
        ## Forms (GET and POST)
        
        ```python
        from flask import request
        
        @app.route("/login", methods=["GET", "POST"])
        def login():
            if request.method == "POST":
                username = request.form["username"]
                password = request.form["password"]
                return f"Logged in as {username}"
        
            return """
              <form method="post">
                <input name="username" />
                <input name="password" type="password" />
                <button type="submit">Login</button>
              </form>
            """
        ```
        
        ## JSON API (backend for frontend/mobile)
        
        ```python
        from flask import jsonify
        
        @app.route("/api/users")
        def get_users():
            users = [
                {"name": "Tom", "city": "Austin"},
                {"name": "Sarah", "city": "Miami"}
            ]
            return jsonify(users)
        ```
        
        ## Graph: Flask request flow
        
        ```mermaid
        flowchart LR
          A[Browser/API call] --> B[Flask route]
          B --> C[Your function logic]
          C --> D[Response: HTML/JSON]
        ```
        
        ## Remember
        
        - debug=True only for development
        - templates go in templates/
        - static files go in static/
        - Flask is great for APIs and small apps
        
#Python#Advanced#Flask