Introduction to Flask
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.
Learn Introduction to Flask in our free Flask course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Flask course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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.
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 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:
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.
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.
Complete the Flask app below. Replace each ___ so it imports Flask, creates the app, defines a home route, and returns a message.
❌ 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.
Extend the app so it has two pages: the home page and an about page.
Lesson 1 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.
🚀 Up next: Your First Route — go deeper into @app.route and learn how to handle multiple pages cleanly.
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.