Skip to content
dreamcode
dreamcode
Map
Flask basics
Lesson 69 of 77
+15 XP on finish
PYTHON APPLIEDChapter 12 · Python for Web Development

Building routes with Flask

Flask is a lightweight Python web framework. You define routes that map URL paths to Python functions. Each route function returns a response, often as JSON data that a frontend can consume.

Worked example
from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/api/stars")
def get_stars():
    stars = ["Vega", "Sirius", "Polaris"]
    return jsonify(stars)

# Run with: flask run
# Visit: http://localhost:5000/api/stars

How it reads

  • @app.route("/api/stars") maps the URL path to the function below it
  • jsonify(stars) converts a Python list into a JSON HTTP response
  • Flask(__name__) creates the application instance
Cloud tip: Flask is called a 'micro' framework because it gives you only the essentials. You add extensions (like Flask-SQLAlchemy) as you need them.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. What does the @app.route decorator do in Flask?
  2. 2. What does jsonify() return?
  3. 3. Why is Flask called a 'micro' framework?
Answer every question to unlock the next lesson.