Flask Tutorial: Setup and First Route
Flask is a lightweight Python web framework. Its core pattern is straightforward: connect a URL to a Python function, then send the value returned by that function to the client as a response. That client might be a browser, a mobile app, a JavaScript frontend, or another service.
We will begin with the smallest useful Flask application. There is no database, template, or long folder structure yet. The goal is simple: isolate the project, start the development server, and follow a request until it becomes text in a browser.
Set Up a Folder and Virtual Environment
Make sure Python 3 is available. You can check it from a terminal with python --version, python3 --version, or py --version on Windows.
Create a dedicated folder so this exercise does not mix with other projects. Then create a virtual environment named .venv. A virtual environment keeps Flask and the other packages inside this project, preventing version conflicts with unrelated Python projects.
On macOS or Linux, run:
# File: terminal (macOS/Linux)
mkdir flask-first-route
cd flask-first-route
python3 -m venv .venv
source .venv/bin/activate
python -m pip install flask
The commands are slightly different in Windows PowerShell:
# File: terminal (Windows PowerShell)
mkdir flask-first-route
cd flask-first-route
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install flask
After activation, (.venv) usually appears at the start of the terminal prompt. You can also confirm that Flask is installed by running python -m flask --version.
Write the First Flask Application
Create app.py inside the project folder and add the following code:
# File: app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
Here is what each part does:
from flask import Flaskimports theFlaskclass from the installed package.app = Flask(__name__)creates the application object. The nameappis not required, but it is conventional and matches the command we will run.@app.route("/")registers/, the root URL of the application.home()is the function Flask runs when that URL is requested.- The returned value becomes the response. This example sends plain text.
The @app.route() decorator sits directly above the function because the two belong together. Without that decorator, home() is still a normal Python function, but Flask has no URL that can reach it.
Start the Development Server
Make sure the virtual environment is still active, then run:
# File: terminal
python -m flask --app app run --debug
The --app app option tells Flask to find the application object in the app module, which is app.py. If you name the file main.py, change the command to python -m flask --app main run --debug.
The --debug option provides clearer error pages and reloads the server when the code changes. It is useful for local development, but it must not be enabled in production because it can expose application internals.
The terminal prints an address such as http://127.0.0.1:5000. Open it in a browser. If everything is working, the page displays Hello, Flask!.
What Happens When the URL Opens?
When you open http://127.0.0.1:5000/, the browser sends a GET request to the development server. Flask inspects the / path, finds the matching route, and runs home(). The returned string is converted into an HTTP response and sent back to the browser.
The flow is always similar: a request arrives, Flask selects a route, a function runs, and a response leaves. Later, that response might contain HTML, a redirect, JSON, a file, or an error page, but the underlying flow remains the same.
Add One More Address
To make the connection between URLs and functions more visible, add an /about route below home():
# File: app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
@app.route("/about")
def about():
return "My first Flask application"
Save the file, then open http://127.0.0.1:5000/about. Because debug mode is enabled, the server should pick up the change without a manual restart. An unregistered URL such as /contact returns 404 Not Found.
Try Changing It
- Change the
/response into a longer sentence. - Create a
/contactroute with an example email address. - Rename the file to
main.py, then update the value after--app. - Open an unregistered URL and watch the
404status in the terminal.
If the Application Does Not Start
- Do not name the file
flask.py. That name conflicts with the Flask package and often causes a confusing import error. - Run the command from the folder that contains
app.py. Flask cannot find theappmodule from an unrelated directory. - Make sure the virtual environment is active before installing or running Flask.
- Check the quotes, the colon after
def home():, and the indentation beforereturn.
This small project only returns one line of text, but all the essential pieces are already present: an application object, a route, a request, and a response. Once those four pieces make sense, adding a page or endpoint is an extension of the same pattern.
Related Articles
Keep reading within the same topic.
Flask Jinja Templates: Layouts, Variables, and Loops
Use Jinja templates in Flask to pass data into HTML, build loops and conditionals, and reuse a base layout without repeating markup.
Flask Routing: URLs, Requests, and Responses
Learn Flask routing through URLs, path parameters, query strings, HTTP methods, and status codes for clear requests and responses.
Flask CRUD App: Build with MySQL, Auth, and Caching
Build a Flask CRUD app with MySQL, authentication, testing, caching, environment setup, and deployment-ready project structure.
Flask Tutorial: Build a Python Website from Scratch
Learn how to build a simple Python web application with Flask, routes, templates, form handling, and a clean starter structure for beginners.