Introduction to Flask
Reviewed & published by Brayan K
Flask is a lightweight Python web framework for building websites and APIs, giving you routing, request handling, and HTML templating in a small, flexible package you control.
Part of the free Flask course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
In this first lesson you'll install Flask, understand how a web framework works, and run your very first Flask application locally.
What You'll Learn in This Lesson
- What Flask is and why it's called a 'micro' framework
- How to install Flask with pip
- The structure of a minimal Flask application
- What the @app.route decorator does
- How to run an app locally with flask run
- Where to view your app in the browser
1 What Is Flask?
Flask is a web framework — a toolkit that handles the repetitive, low-level work of running a website so you can focus on your app's logic.
When someone visits a URL, their browser sends an HTTP request to your server. Flask receives that request, figures out which Python function should handle it, runs your code, and sends back an HTTP response (usually HTML or JSON). You write the functions; Flask wires everything together.
🧩 Flask is a "micro" framework:
- Small core: routing, requests, and templating come built in
- Unopinionated: you choose your database, your project layout, your tools
- Extensible: add features like login or databases through extensions when you need them
- Beginner-friendly: a complete web page is under 10 lines of code
2 Installing Flask
Flask is a Python package, so you install it with pip. It's best practice to do this inside a virtual environment so each project keeps its own dependencies:
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install Flask
pip install flask
# Confirm it installed
flask --version3 Your First Flask App
Here is the smallest complete Flask app. Save it in a file named app.py:
The code below creates an application object, defines a single route at the home URL /, and returns a greeting. Run it and Flask will print a local address you can open in your browser.
# app.py — the classic minimal Flask application
from flask import Flask
# Create the application object.
# __name__ tells Flask where to find templates and static files.
app = Flask(__name__)
# @app.route maps a URL path to the function below it.
# Visiting "/" (the home page) runs home() and shows its return value.
@app.route("/")
def home():
return "Hello, Flask!"
print("Routes registered:", [str(r) for r in app.url_map.iter_rules()])
# Flask gives you a test client, so you can call your own route and see what
# a browser would get — without starting a server at all:
with app.test_client() as client:
response = client.get("/")
print("GET / ->", response.status_code, response.data.decode())
# To serve it on your own machine, end the file with the lines below, then
# run "python app.py" (or "flask run") and visit http://127.0.0.1:5000
# if __name__ == "__main__":
# app.run(debug=True)
# ✅ Expected output:
# Routes registered: ['/static/<path:filename>', '/']
# GET / -> 200 Hello, Flask!When you run flask run, Flask starts a development server and prints something like Running on http://127.0.0.1:5000. Open that address and you'll see "Hello, Flask!" in your browser.
🔬 Anatomy of a Flask App
| Line | What it does |
|---|---|
| from flask import Flask | Imports the Flask class from the package |
| app = Flask(__name__) | Creates your application instance |
| @app.route("/") | Connects the URL path / to a function |
| def home(): | The view function — runs when the route is visited |
| return "Hello, Flask!" | The response body sent back to the browser |
🎯 Your Turn: Fill in the Blanks
Complete the Flask app below. Replace each ___ so it imports Flask, creates the app, defines a home route, and returns a message.
# 🎯 YOUR TURN: replace each ___ to finish the app
from flask import ___ # import the Flask class
app = ___(__name__) # create the application object
@app.route("___") # the home page path
def home():
return "___" # your greeting message
print("Rules:", [str(r) for r in app.url_map.iter_rules()])Common Errors & Quick Reference
❌ ModuleNotFoundError: No module named 'flask'
Flask isn't installed in your active environment. Run pip install flask and make sure your virtual environment is activated.
❌ flask run says "Could not locate a Flask application"
Flask looks for a file named app.py or wsgi.py by default. Either name your file app.py or set FLASK_APP=yourfile.py before running.
| Task | Command / Code |
|---|---|
| Install Flask | pip install flask |
| Run the dev server | flask run |
| Create the app | app = Flask(__name__) |
| Define a route | @app.route("/") |
🏆 Mini Challenge: Add an About Page
Extend the app so it has two pages: the home page and an about page.
- Keep the home route returning a greeting
- Add a second route at /about
- Make the about function return a short description of you or your site
# 🏆 Mini Challenge: a two-page Flask app
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Welcome to my site!"
# TODO: add a route for /about that returns a description
@app.route("/about")
def about():
return "This site was built with Flask."
# Print the registered routes to confirm both exist
for rule in app.url_map.iter_rules():
print(rule)❓ Frequently Asked Questions
Lesson complete — you've built your first Flask app!
You now know what Flask is, how to install it, and how to define a route and run a development server with flask run. That five-line app is the seed of every Flask project.
Practice quiz
What kind of tool is Flask?
- A Python web framework
- A database engine
- A CSS preprocessor
- An operating system
Answer: A Python web framework. Flask is a lightweight Python web framework for building websites and APIs.
Why is Flask called a 'micro' framework?
- It only runs on small computers
- Its core is small and unopinionated, with features added as needed
- It can only serve one page
- It has no routing
Answer: Its core is small and unopinionated, with features added as needed. Flask keeps a small core (routing, requests, templating) and lets you add the rest.
Which command installs Flask?
- flask new
- npm install flask
- pip install flask
- python flask
Answer: pip install flask. Flask is a Python package installed with pip install flask.
What does Flask(__name__) do?
- Starts a database
- Runs the test suite
- Imports Jinja2
- Creates the application instance
Answer: Creates the application instance. app = Flask(__name__) creates the application object that ties everything together.
What does the @app.route("/") decorator do?
- Maps the URL path / to the function below it
- Deletes the home page
- Installs an extension
- Sets the secret key
Answer: Maps the URL path / to the function below it. @app.route binds a URL path to a view function.
What is the function under @app.route called?
- A migration
- A view function
- A template
- A blueprint
Answer: A view function. The function that runs when a route is visited is called a view function.
What does a view function's return value become?
- A log entry
- A database row
- The HTTP response body sent to the browser
- A new route
Answer: The HTTP response body sent to the browser. Returning a string sends it back as the response body.
Which command starts Flask's development server?
- python build
- flask serve
- flask start
- flask run
Answer: flask run. flask run launches the development server.
What default address does the dev server typically print?
- http://127.0.0.1:5000
- http://localhost:8080
- http://0.0.0.0:80
- http://example.com
Answer: http://127.0.0.1:5000. Flask's dev server defaults to http://127.0.0.1:5000.
If you see 'ModuleNotFoundError: No module named flask', what is wrong?
- The route is misspelled
- Debug mode is off
- Flask is not installed in the active environment
- The port is busy
Answer: Flask is not installed in the active environment. That error means Flask isn't installed; run pip install flask in your active environment.
Continue this course
- Next: Your First Route — Map URLs to Python functions with the @app.route decorator
- Quick reference: Flask cheat sheet › Setup & Routes
Frequently asked questions
What is Flask?
Flask is a lightweight Python web framework used to build websites and APIs. It gives you routing, request handling, and templating while staying small and unopinionated.
Do I need to know Python before learning Flask?
Yes — Flask is written in Python and your route functions are plain Python. You should be comfortable with functions, dictionaries, and decorators before starting.
How do I run a Flask app?
Install Flask with 'pip install flask', save your app in a file like app.py, then run 'flask run' (or 'python app.py') in your terminal. Flask serves it locally at http://127.0.0.1:5000.
Is Flask good for beginners?
Yes. Flask's minimal core means you can build a working web page in under 10 lines of code, then add features like databases and authentication as you grow.