Stop 13 · Choose a Path: Web, Data or AIWeb & APIs · 1/6

Flask Basics

Part of the Python Roadmap

In short

Every website you use is a program that waits for a URL and sends something back. Flask lets you build that program in a single Python file, so you can see exactly how the web works before adding anything fancy.

Flask is a lightweight web framework where you map URLs to Python functions with @app.route. It is simple to learn and good for small apps, APIs and understanding how web frameworks work.

The jaadu stands behind a small hotel reception desk, ringing a brass bell and handing a neatly wrapped parcel to a waiting guest from a wall of numbered pigeonholes.

Think of it like this

The hotel front desk

A guest walks up and says a room number. The receptionist looks at the board, finds which staff member handles that room and sends the guest's request to them. Flask is the front desk: each URL is a room number, and @app.route tells Flask which Python function looks after it.

Watch it happen

One request through a Flask app

Browser
Flask
View function

GET /books/1

1/4The browser asks for a URL. Flask receives the HTTP request.

The big ideas

What you need to know

Routes map URLs to functions

@app.route("/") or the shortcuts @app.get and @app.post connect a URL and HTTP method to one Python function, called a view.

Variables in the URL

<int:book_id> captures part of the path and passes it as an argument, already converted to the right type.

The request object

request.args holds query parameters, request.get_json() reads a JSON body and request.form reads HTML form fields.

Return text, HTML or JSON

Return a string for HTML, a dict or list for JSON, and add a status code like (data, 201) when needed.

How it works

From empty folder to running app

  1. 1

    Install

    Create a virtual environment and run pip install flask.

  2. 2

    Create the app

    In app.py, write app = Flask(__name__).

  3. 3

    Add routes

    Decorate functions with @app.route or @app.get / @app.post.

  4. 4

    Run in debug mode

    Run flask --app app run --debug. The server reloads when you save.

  5. 5

    Test

    Open http://127.0.0.1:5000 in the browser, or call the API with curl.

See it in code

Walk through the code

python
1from flask import Flask, abort, jsonify, request2 3app = Flask(__name__)4 5books = {1: {"id": 1, "title": "Wings of Fire", "author": "A. P. J. Abdul Kalam"}}6 7@app.route("/")8def home():9    return "<h1>Namaste from Flask!</h1>"10 11@app.get("/books/<int:book_id>")12def get_book(book_id: int):13    book = books.get(book_id)14    if book is None:15        abort(404)16    return book17 18@app.post("/books")19def add_book():20    data = request.get_json()21    new_id = max(books) + 122    books[new_id] = {"id": new_id, "title": data["title"], "author": data["author"]}23    return jsonify(books[new_id]), 20124 25if __name__ == "__main__":26    app.run(debug=True)

1/6Import Flask and create the app. __name__ tells Flask where to find templates and static files.

A tiny book API in Flask 3. Run it with flask --app app run --debug, then visit http://127.0.0.1:5000/books/1.

Save yourself the pain

Common mistakes

Oops

Running the app with debug=True on a real server.

Do this instead

Debug mode shows an interactive debugger that can run code. Use it only on your laptop and run production apps with a WSGI server like Gunicorn.

Oops

Putting all routes, database code and HTML strings in one huge app.py.

Do this instead

Once an app grows, move HTML into templates/ with Jinja and group related routes into Blueprints.

Oops

Trusting request.get_json() blindly.

Do this instead

Check that required keys exist and values make sense, and return a 400 error with a clear message when they don't.

Your mission

A to-do API in 30 minutes

~30 min

Quick check

What does <int:book_id> in a Flask route do?

Go deeper

  1. 1Build a Flask app with a home page and one route that returns JSON.
  2. 2Use route parameters like /students/<int:roll_no>.
  3. 3Render an HTML template with Jinja2.

More web & apis at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge